Skip to content

Commit

Permalink
Backport to release-0.6.x from r1938: Only use gzip encoding for mime…
Browse files Browse the repository at this point in the history
… types that actually benefit from it.
  • Loading branch information
Arjan Scherpenisse committed Apr 7, 2011
1 parent 8d78344 commit 932c8d2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
10 changes: 5 additions & 5 deletions modules/mod_base/resources/resource_lib.erl
Expand Up @@ -4,7 +4,7 @@
%%
%% Serves files like: /lib/<filepath>

%% Copyright 2009 Marc Worrell
%% Copyright 2009-2011 Marc Worrell, Konstantin Nikiforov
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -80,11 +80,11 @@ content_types_provided(ReqData, State) ->
end.

encodings_provided(ReqData, State) ->
State1 = lookup_path(ReqData, State),
Encodings = case State1#state.mime of
"image/jpeg" ->
State1 = lookup_path(ReqData, State),
Encodings = case z_media_identify:is_mime_compressed(State1#state.mime) of
true ->
[{"identity", fun(Data) -> Data end}];
_ ->
false ->
[{"identity", fun(Data) -> decode_data(identity, Data) end},
{"gzip", fun(Data) -> decode_data(gzip, Data) end}]
end,
Expand Down
28 changes: 24 additions & 4 deletions src/support/z_media_identify.erl
Expand Up @@ -5,7 +5,7 @@
%% @doc Identify files, fetch metadata about an image
%% @todo Recognize more files based on magic number, think of office files etc.

%% Copyright 2009 Marc Worrell
%% Copyright 2009-2011 Marc Worrell, Konstantin Nikiforov
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand All @@ -30,7 +30,8 @@
identify_file/3,
identify_file_direct/2,
extension/1,
guess_mime/1
guess_mime/1,
is_mime_compressed/1
]).

-include_lib("zotonic.hrl").
Expand Down Expand Up @@ -264,7 +265,7 @@ extension_mime() ->
{".doc", "application/msword"},
{".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
{".dot", "application/x-dot"},
{".dotx", "application/vnd.openxmlformats-officedocument.wordprocessingml.template"},
{".dotx", "application/vnd.openxmlformats-officedocument.wordprocessingml.template"},
{".dvi", "application/x-dvi"},
{".dwg", "application/acad"},
{".flipchart", "application/inspire"},
Expand Down Expand Up @@ -312,7 +313,7 @@ extension_mime() ->
{".ps", "application/postscript"},
{".psd", "image/vnd.adobe.photoshop"},
{".rar", "application/x-rar"},
{".rtf", "text/rtf"},
{".rtf", "text/rtf"},
{".sh", "text/x-shellscript"},
{".sit", "application/x-stuffit"},
{".svg", "image/svg+xml"},
Expand Down Expand Up @@ -369,3 +370,22 @@ exif_orientation(InFile) ->
_ -> 1
end.


%% @doc Given a mime type, return whether its file contents is already compressed or not.
%% @spec is_mime_compressed(Mime::string()) -> bool()
is_mime_compressed("text/"++_) -> false;
is_mime_compressed("image/svgz"++_) -> true;
is_mime_compressed("image/svg"++_) -> false;
is_mime_compressed("image/"++_) -> true;
is_mime_compressed("video/"++_) -> true;
is_mime_compressed("audio/x-wav") -> false;
is_mime_compressed("audio/"++_) -> true;
is_mime_compressed("application/x-compres"++_) -> true;
is_mime_compressed("application/zip") -> true;
is_mime_compressed("application/x-gz"++_) -> true;
is_mime_compressed("application/x-rar") -> true;
is_mime_compressed("application/x-bzip2") -> true;
is_mime_compressed("application/vnd.oasis.opendocument."++_) -> true;
is_mime_compressed("application/vnd.openxml"++_) -> true;
is_mime_compressed("application/x-shockwave-flash") -> true;
is_mime_compressed(_) -> false.

0 comments on commit 932c8d2

Please sign in to comment.