From 1a9f38df49269d5322d43665bae774d693ec64b1 Mon Sep 17 00:00:00 2001 From: Kevin Lynx Date: Sat, 6 Jul 2013 09:42:32 +0800 Subject: [PATCH] see master branch --- src/hash_reader/db_hash_reader_sup.erl | 1 + src/local_torrent/loc_torrent_cache.erl | 14 +++++++++----- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/hash_reader/db_hash_reader_sup.erl b/src/hash_reader/db_hash_reader_sup.erl index 955bed0..041a30a 100644 --- a/src/hash_reader/db_hash_reader_sup.erl +++ b/src/hash_reader/db_hash_reader_sup.erl @@ -69,4 +69,5 @@ config_default() -> [{save_torrent, true}, {save_to_db, false}, {save_to_file, true}, + {load_from_db, false}, {torrent_path, "torrents/"}]. diff --git a/src/local_torrent/loc_torrent_cache.erl b/src/local_torrent/loc_torrent_cache.erl index b66bbf3..9d304c3 100644 --- a/src/local_torrent/loc_torrent_cache.erl +++ b/src/local_torrent/loc_torrent_cache.erl @@ -17,16 +17,20 @@ save(Conn, MagHash, Content) when is_list(MagHash), length(MagHash) == 40 -> ok. load(Conn, MagHash) when is_list(MagHash), length(MagHash) == 40 -> + LoadDB = config:get(load_from_db, false), case load_from_file(MagHash) of - not_found -> + not_found when LoadDB -> db_loc_torrent:load(Conn, MagHash); + not_found -> + not_found; Content -> Content end. %% TODO: put these file-realted codes to another module 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 ok -> ok; {error, Reason} -> @@ -34,7 +38,7 @@ save_to_file(MagHash, Content) -> end. load_from_file(MagHash) -> - FileName = torrent_file_name(MagHash), + {_, FileName} = torrent_file_name(MagHash), case file:read_file(FileName) of {ok, Content} -> Content; @@ -47,5 +51,5 @@ torrent_file_name(MagHash) -> Path = config:get(torrent_path, "torrents/"), FullPath = Path ++ lists:sublist(MagHash, 1, 2) ++ "/" ++ lists:sublist(MagHash, 3, 2) ++ "/", - filelib:ensure_dir(FullPath), - FullPath ++ MagHash ++ ".torrent". + {FullPath, FullPath ++ MagHash ++ ".torrent"}. +