summaryrefslogtreecommitdiffstats
path: root/aconf/model/binary.lua
blob: fcff3ea6705c168a873905fa634d0c283870cdd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
--[[
Copyright (c) 2012-2014 Kaarle Ritvanen
See LICENSE file for license details
--]]

local M = {}

local object = require('aconf.object')
local class = object.class
local super = object.super


local b64 = require('b64')

local magic = require('magic')
magic = magic.open(magic.MIME_TYPE)
magic:load()


M.Data = class()

function M.Data:init(path, data)
   self.path = path
   self.data = data
   self.type = magic:buffer(data)
end

function M.Data:encode()
   return 'data:'..self.type..';base64,'..b64.encode(self.data)
end


M.Audio = class(require('aconf.model.field').Field)

function M.Audio:init(params)
   super(self, M.Audio):init(params)
   self.dtype = 'binary'
   self.widget = 'audio'
end

function M.Audio:load(context)
   local value = super(self, M.Audio):load(context)
   return type(value) == 'string' and M.Data(context.path, value) or value
end

-- not yet implemented
function M.Audio:save(context, value) end


return M