Skip to content

Commit

Permalink
Fixes a bug with thumbnail generation.
Browse files Browse the repository at this point in the history
  • Loading branch information
m2w committed Sep 25, 2013
1 parent 52cbd00 commit bceb8bd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
7 changes: 4 additions & 3 deletions src/erli_thumbnails.erl
Expand Up @@ -30,6 +30,7 @@ start() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).

generate_thumbnails() ->
error_logger:info_msg("[INFO] Initiating thumbnail collection"),
gen_server:cast(?SERVER, gen_thumbs).

%%-----------------------------------------------------------
Expand Down Expand Up @@ -72,10 +73,10 @@ grab([T|Targets], {Exe, ThumbnailDir} = Ctx) ->
ThumbnailPath = filename:join([ThumbnailDir,
<<(T#target.id)/bitstring, ".jpeg">>]),
Cmd = <<Exe/bitstring, " ", TUrl/bitstring, " ", ThumbnailPath/bitstring>>,
case os:cmd(binary_to_list(Cmd)) of
0 ->
case erli_utils:run(binary_to_list(Cmd)) of
{0, _} ->
erli_storage:thumbnail_generated(T);
_ ->
{_, _ErrorDesc} ->
error_logger:info_msg(
"[ERROR] Generation of a thumbnail for ~s failed~n", [TUrl])
end,
Expand Down
24 changes: 23 additions & 1 deletion src/erli_utils.erl
Expand Up @@ -20,7 +20,8 @@
generate_etag/1,
unix_timestamp/0,
parse_range_header/2,
build_content_range_header/2]).
build_content_range_header/2,
run/1]).

-include("models.hrl").
-include_lib("webmachine/include/webmachine.hrl").
Expand All @@ -32,6 +33,7 @@
-type file_path() :: string().
-type maybe_int() :: [] | integer().
-type etag() :: string().
-type exit_status() :: non_neg_integer().

%%-----------------------------------------------------------
%% API Methods
Expand Down Expand Up @@ -189,10 +191,30 @@ get_location(Ip) ->
{ok, Rec} = egeoip:lookup(Ip),
list_to_binary(egeoip:get(Rec, country_code)).

-spec run(string()) -> {exit_status(), iolist()}.
run(Cmd) ->
Opt = [stream, exit_status, use_stdio, stderr_to_stdout, in, eof],
P = open_port({spawn, Cmd}, Opt),
get_data(P, []).


%%-----------------------------------------------------------
%% Internal Methods
%%-----------------------------------------------------------

-spec get_data(port(), iolist()) -> {exit_status(), iolist()}.
get_data(P, D) ->
receive
{P, {data, D1}} ->
get_data(P, [D|D1]);
{P, eof} ->
port_close(P),
receive
{P, {exit_status, N}} ->
{N, lists:reverse(D)}
end
end.

-spec parse_range(string(), collection_type()) ->
range() |
{error, invalid_range}.
Expand Down

0 comments on commit bceb8bd

Please sign in to comment.