@@ -12,126 +12,23 @@ init(_, Req, _Opts) ->
12
12
{ok , Req , []}.
13
13
14
14
handle (Req , State ) ->
15
- {ok , Req1 } = cowboy_req :chunked_reply (
16
- 200 , [{<<" content-type" >>, <<" application/javascript" >>}], Req
15
+ {Components , Req1 } = cowboy_req :qs_vals (Req ),
16
+ {ok , Req2 } = cowboy_req :chunked_reply (
17
+ 200 , [{<<" content-type" >>, <<" application/javascript" >>}], Req1
17
18
),
18
- % % Proplist with one or more components to minfify
19
- {Components , Req2 } = cowboy_req :qs_vals (Req1 ),
20
-
21
- % % Build a map with all components like
22
- ComponentScriptFilePaths = pmap (
23
- fun ({Component , Tag }) ->
24
- {DiversityProps } = jiffy :decode (git_utils :get_diversity_json (Component , Tag )),
25
- ScriptFiles = proplists :get_value (<<" script" >>, DiversityProps ),
26
- TaggedScriptFiles = lists :map (fun get_file_type /1 , ScriptFiles ),
27
- {{Component , Tag }, TaggedScriptFiles }
28
- end ,
29
- Components
30
- ),
31
-
32
- Z = zlib :open (),
33
- ok = zlib :deflateInit (Z , default ),
34
19
pmap (
35
- fun ({{Component , Tag }, FilePaths }) ->
36
- % zlib:deflateReset(Z),
37
- Result = get_minified_file (Component , Tag , FilePaths , fun get_file_contents /4 ),
38
- Result1 = zlib :deflate (Z , Result , finish ),
39
- io :format (" Result ~p " , [Result1 ]),
40
- cowboy_req :chunk (Result1 , Req2 ),
20
+ fun ({Component , Tag }) ->
21
+ Result = divapi_js_minifier :minify (Component , Tag ),
22
+ cowboy_req :chunk (Result , Req2 ),
41
23
Result
42
24
end ,
43
- ComponentScriptFilePaths
25
+ Components
44
26
),
45
- cowboy_req :chunk (zlib :deflate (Z , <<>>, finish ), Req2 ),
46
- ok = zlib :deflateEnd (Z ),
47
- zlib :close (Z ),
48
27
{ok , Req2 , State }.
49
28
50
29
terminate (_Reason , _Req , _State ) ->
51
30
ok .
52
31
53
- get_file_type (<<" //" , _Rest /binary >> = ScriptFile ) ->
54
- % % This is an exteral js file that needs to be downloaded.
55
- {external , <<" http:" , ScriptFile /binary >>};
56
- get_file_type (<<" http://" , _Rest /binary >> = ScriptFile ) ->
57
- % % This is an exteral js file that needs to be downloaded.
58
- {external , ScriptFile };
59
- get_file_type (<<" https://" , _Rest /binary >> = ScriptFile ) ->
60
- % % This is an exteral js file that needs to be downloaded.
61
- {external , ScriptFile };
62
- get_file_type (ScriptFile ) ->
63
- case re :run (ScriptFile , <<" .min.js" >>) of
64
- {match , _ } ->
65
- % % It's already minified. And exists in our component already
66
- {already_minified , ScriptFile };
67
- _ ->
68
- % % It's not a minified file. Assume internal
69
- {jsfile , ScriptFile }
70
- end .
71
-
72
- get_minified_file (Component , Tag , FilePaths , MinifierFun ) ->
73
- MinifiedPath = case code :priv_dir (divapi ) of
74
- {error , bad_name } -> <<" priv/js_minfied/" >>;
75
- PrivDir -> <<(list_to_binary (PrivDir ))/binary , " /js_minified" >>
76
- end ,
77
- ComponentMinifiedPath = filename :join (MinifiedPath , <<Component /binary , " /" , Tag /binary , " /" >>),
78
- ok = filelib :ensure_dir (<<ComponentMinifiedPath /binary , " /" >>),
79
- FilePath = filename :join (ComponentMinifiedPath , <<" scripts.min.js" >>),
80
- case filelib :is_file (FilePath ) of
81
- true ->
82
- {ok , FileBody } = file :read_file (FilePath ),
83
- FileBody ;
84
- false ->
85
- FileBody = MinifierFun (Component , Tag , FilePaths , <<>>),
86
- ok = file :write_file (FilePath , FileBody ),
87
- FileBody
88
- end .
89
-
90
- % % Fetch filec content from outside. And add it to the filecontent
91
- get_file_contents (Component , Tag , [{external , ScriptFilePath } | Rest ], FileContent ) ->
92
- Opts = [{body_format , binary }],
93
- Request = {binary_to_list (ScriptFilePath ), []},
94
- ScriptFile = case httpc :request (get , Request , [], Opts ) of
95
- {ok , {{_Version , Status , _ReasonPhrase }, _Headers , Body }} ->
96
-
97
- case Status of
98
- 404 -> throw ({resource_not_found , ScriptFilePath });
99
- 500 -> throw ({server_error , ScriptFilePath });
100
- 200 -> Body
101
- end
102
- end ,
103
- get_file_contents (Component , Tag , Rest , <<FileContent /binary , ScriptFile /binary >>);
104
- % % No action needed, add it to the file acc as is
105
- get_file_contents (ComponentName , Tag , [{already_minified , ScriptFilePath } | Rest ], FileContent ) ->
106
- FileBody = git_utils :get_file (ComponentName , Tag , ScriptFilePath ),
107
- get_file_contents (ComponentName , Tag , Rest , <<FileContent /binary , FileBody /binary >>);
108
- % % Fetches filecontent for component and minify it through uglify
109
- get_file_contents (ComponentName , Tag , [{jsfile , ScriptFilePath } | Rest ], FileContent ) ->
110
- TmpName0 = {ComponentName , Tag , ScriptFilePath },
111
- TmpName1 = integer_to_binary (erlang :phash2 (TmpName0 )),
112
- TmpPath = filename :join (<<" /tmp/divapi/" >>, <<TmpName1 /binary , " .js" >>),
113
- File = git_utils :get_file (ComponentName , Tag , ScriptFilePath ),
114
- ok = filelib :ensure_dir (TmpPath ),
115
- ok = file :write_file (TmpPath , File ),
116
- Command = <<" uglifyjs " , TmpPath /binary >>,
117
- Port = erlang :open_port ({spawn , Command }, [exit_status , binary , stderr_to_stdout ]),
118
- Result = wait_for_file (Port , <<>>),
119
- % % Cleanup
120
- file :delete (TmpPath ),
121
- get_file_contents (ComponentName , Tag , Rest , <<FileContent /binary , Result /binary >>);
122
- get_file_contents (_Component , _Tag , [], FileContent ) ->
123
- FileContent .
124
-
125
- wait_for_file (Port , File ) ->
126
- receive
127
- {Port , {data , Chunk }} ->
128
- wait_for_file (Port , <<File /binary , Chunk /binary >>);
129
- {_ , {exit_status , 0 }} ->
130
- File ;
131
- {_ , {exit_status , _ }} ->
132
- throw ({wait_for_response , failure })
133
- end .
134
-
135
32
pmap (Fun , List ) ->
136
33
pmap (Fun , List , 5000 ).
137
34
0 commit comments