PageRenderTime 23ms CodeModel.GetById 19ms RepoModel.GetById 0ms 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. -export([add/2, list/3, get/2, delete/2]).
  20. -include("uce.hrl").
  21. add(Domain, #uce_file{location=Location, name=Name} = File) ->
  22. case uce_meeting:exists(Domain, Location) of
  23. false ->
  24. throw({error, not_found});
  25. true ->
  26. Extension = filename:extension(Name),
  27. Base64Name = binary_to_list(base64:encode(unicode:characters_to_binary(Name))),
  28. Rnd = utils:random(),
  29. {Id, Mime} =
  30. case Extension of
  31. "" ->
  32. {Base64Name ++ "_" ++ Rnd,
  33. "text/plain"};
  34. _ ->
  35. {Base64Name ++ "_" ++ Rnd ++ Extension,
  36. yaws_api:mime_type(Name)}
  37. end,
  38. (db:get(?MODULE, Domain)):add(Domain, File#uce_file{id=Id, mime=Mime})
  39. end.
  40. list(Domain, Id, Order) ->
  41. case uce_meeting:exists(Domain, Id) of
  42. false ->
  43. throw({error, not_found});
  44. true ->
  45. (db:get(?MODULE, Domain)):list(Domain, Id, Order)
  46. end.
  47. get(Domain, Id) ->
  48. (db:get(?MODULE, Domain)):get(Domain, Id).
  49. delete(Domain, Id) ->
  50. case get(Domain, Id) of
  51. {error, Reason} ->
  52. {error, Reason};
  53. {ok, _} ->
  54. (db:get(?MODULE, Domain)):delete(Domain, Id)
  55. end.