PageRenderTime 102ms CodeModel.GetById 41ms app.highlight 6ms RepoModel.GetById 54ms app.codeStats 0ms

/ucengine/src/models/uce_file.erl

http://github.com/AF83/ucengine
Erlang | 61 lines | 38 code | 6 blank | 17 comment | 0 complexity | 4ca74ea6c28a252d7e453e6f7f67ba19 MD5 | raw file
 1%%
 2%%  U.C.Engine - Unified Collaboration Engine
 3%%  Copyright (C) 2011 af83
 4%%
 5%%  This program is free software: you can redistribute it and/or modify
 6%%  it under the terms of the GNU Affero General Public License as published by
 7%%  the Free Software Foundation, either version 3 of the License, or
 8%%  (at your option) any later version.
 9%%
10%%  This program is distributed in the hope that it will be useful,
11%%  but WITHOUT ANY WARRANTY; without even the implied warranty of
12%%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13%%  GNU Affero General Public License for more details.
14%%
15%%  You should have received a copy of the GNU Affero General Public License
16%%  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17%%
18-module(uce_file).
19
20-export([add/2, list/3, get/2, delete/2]).
21
22-include("uce.hrl").
23
24add(Domain, #uce_file{location=Location, name=Name} = File) ->
25    case uce_meeting:exists(Domain, Location) of
26        false ->
27            throw({error, not_found});
28        true ->
29            Extension = filename:extension(Name),
30            Base64Name = binary_to_list(base64:encode(unicode:characters_to_binary(Name))),
31            Rnd = utils:random(),
32            {Id, Mime} =
33                case Extension of
34                    "" ->
35                        {Base64Name ++ "_" ++ Rnd,
36                         "text/plain"};
37                    _ ->
38                        {Base64Name ++ "_" ++ Rnd ++ Extension,
39                         yaws_api:mime_type(Name)}
40                end,
41            (db:get(?MODULE, Domain)):add(Domain, File#uce_file{id=Id, mime=Mime})
42    end.
43
44list(Domain, Id, Order) ->
45    case uce_meeting:exists(Domain, Id) of
46        false ->
47            throw({error, not_found});
48        true ->
49            (db:get(?MODULE, Domain)):list(Domain, Id, Order)
50    end.
51
52get(Domain, Id) ->
53    (db:get(?MODULE, Domain)):get(Domain, Id).
54
55delete(Domain, Id) ->
56    case get(Domain, Id) of
57        {error, Reason} ->
58            {error, Reason};
59        {ok, _} ->
60            (db:get(?MODULE, Domain)):delete(Domain, Id)
61    end.