see master branch

This commit is contained in:
Kevin Lynx 2013-07-06 09:42:32 +08:00
parent 8f04efed5d
commit 1a9f38df49
2 changed files with 10 additions and 5 deletions

View File

@ -69,4 +69,5 @@ config_default() ->
[{save_torrent, true}, [{save_torrent, true},
{save_to_db, false}, {save_to_db, false},
{save_to_file, true}, {save_to_file, true},
{load_from_db, false},
{torrent_path, "torrents/"}]. {torrent_path, "torrents/"}].

View File

@ -17,16 +17,20 @@ save(Conn, MagHash, Content) when is_list(MagHash), length(MagHash) == 40 ->
ok. ok.
load(Conn, MagHash) when is_list(MagHash), length(MagHash) == 40 -> load(Conn, MagHash) when is_list(MagHash), length(MagHash) == 40 ->
LoadDB = config:get(load_from_db, false),
case load_from_file(MagHash) of case load_from_file(MagHash) of
not_found -> not_found when LoadDB ->
db_loc_torrent:load(Conn, MagHash); db_loc_torrent:load(Conn, MagHash);
not_found ->
not_found;
Content -> Content ->
Content Content
end. end.
%% TODO: put these file-realted codes to another module %% TODO: put these file-realted codes to another module
save_to_file(MagHash, Content) -> save_to_file(MagHash, Content) ->
FileName = torrent_file_name(MagHash), {Path, FileName} = torrent_file_name(MagHash),
filelib:ensure_dir(Path),
case file:write_file(FileName, Content) of case file:write_file(FileName, Content) of
ok -> ok; ok -> ok;
{error, Reason} -> {error, Reason} ->
@ -34,7 +38,7 @@ save_to_file(MagHash, Content) ->
end. end.
load_from_file(MagHash) -> load_from_file(MagHash) ->
FileName = torrent_file_name(MagHash), {_, FileName} = torrent_file_name(MagHash),
case file:read_file(FileName) of case file:read_file(FileName) of
{ok, Content} -> {ok, Content} ->
Content; Content;
@ -47,5 +51,5 @@ torrent_file_name(MagHash) ->
Path = config:get(torrent_path, "torrents/"), Path = config:get(torrent_path, "torrents/"),
FullPath = Path ++ lists:sublist(MagHash, 1, 2) ++ "/" ++ FullPath = Path ++ lists:sublist(MagHash, 1, 2) ++ "/" ++
lists:sublist(MagHash, 3, 2) ++ "/", lists:sublist(MagHash, 3, 2) ++ "/",
filelib:ensure_dir(FullPath), {FullPath, FullPath ++ MagHash ++ ".torrent"}.
FullPath ++ MagHash ++ ".torrent".