Rev 9287 | Rev 9602 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
8413 | leency | 1 | #define MEMSIZE 1024 * 40 |
8697 | leency | 2 | //Copyright 2020 - 2021 by Leency |
8278 | leency | 3 | #include "../lib/gui.h" |
4 | #include "../lib/random.h" |
||
5 | #include "../lib/obj/box_lib.h" |
||
6 | #include "../lib/obj/http.h" |
||
8281 | leency | 7 | |
8278 | leency | 8 | #include "const.h" |
9 | |||
8305 | leency | 10 | bool exit_param = false; |
8413 | leency | 11 | bool open_file = false; |
8305 | leency | 12 | |
9287 | leency | 13 | dword speed; |
14 | |||
8305 | leency | 15 | _http http; |
16 | |||
8291 | leency | 17 | checkbox autoclose = { T_AUTOCLOSE, false }; |
7284 | leency | 18 | |
8354 | leency | 19 | char uEdit[URL_SIZE]; |
8305 | leency | 20 | char filepath[URL_SIZE+96]; |
7521 | leency | 21 | |
9287 | leency | 22 | progress_bar pb = {0, GAPX, 58, 380, 17, 0, NULL, NULL, 0xFFFfff, 0x74DA00, NULL}; |
8305 | leency | 23 | edit_box ed = {WIN_W-GAPX-GAPX,GAPX,20,0xffffff,0x94AECE,0xffffff,0xffffff, |
8354 | leency | 24 | 0x10000000, sizeof(uEdit)-2,#uEdit,0,ed_focus,19,19}; |
8305 | leency | 25 | |
26 | |||
8278 | leency | 27 | void main() |
5493 | leency | 28 | { |
8278 | leency | 29 | dword shared_url; |
30 | load_dll(boxlib, #box_lib_init,0); |
||
31 | load_dll(libHTTP, #http_lib_init,1); |
||
32 | |||
8319 | leency | 33 | if (!dir_exists(#save_dir)) CreateDir(#save_dir); |
34 | SetCurDir(#save_dir); |
||
6969 | leency | 35 | |
8278 | leency | 36 | if (param) { |
8413 | leency | 37 | if (streqrp(#param, "-e ")) { |
8305 | leency | 38 | exit_param = true; |
8413 | leency | 39 | param += 3; |
8278 | leency | 40 | } |
8413 | leency | 41 | if (streqrp(#param, "-eo ")) { |
42 | exit_param = true; |
||
43 | open_file = true; |
||
44 | param += 4; |
||
45 | } |
||
8278 | leency | 46 | |
47 | if (!strncmp(#param, "-mem", 5)) { |
||
8281 | leency | 48 | //shared_url = memopen(#dl_shared, URL_SIZE+1, SHM_OPEN + SHM_WRITE); |
8354 | leency | 49 | strcpy(#uEdit, shared_url); |
8278 | leency | 50 | } else { |
8354 | leency | 51 | strcpy(#uEdit, #param); |
8278 | leency | 52 | } |
9246 | leency | 53 | |
54 | if (streq(#param, "-test")) { |
||
55 | strcpy(#uEdit, URL_SPEED_TEST); |
||
56 | } |
||
8305 | leency | 57 | } |
8354 | leency | 58 | if (uEdit[0]) StartDownloading(); else { |
8334 | leency | 59 | edit_box_set_text stdcall (#ed, "http://"); |
60 | } |
||
6374 | guillem | 61 | |
8278 | leency | 62 | @SetEventMask(EVM_REDRAW + EVM_KEY + EVM_BUTTON + EVM_MOUSE + EVM_MOUSE_FILTER + EVM_STACK); |
63 | loop() switch(@WaitEvent()) |
||
7281 | leency | 64 | { |
8305 | leency | 65 | case evMouse: edit_box_mouse stdcall (#ed); break; |
8413 | leency | 66 | case evButton: ProcessButtonClick(@GetButtonID()); break; |
8305 | leency | 67 | case evKey: ProcessKeyPress(); break; |
68 | case evReDraw: DrawWindow(); break; |
||
69 | default: MonitorProgress(); |
||
7281 | leency | 70 | } |
5493 | leency | 71 | } |
6374 | guillem | 72 | |
8413 | leency | 73 | void ProcessButtonClick(int id) |
6001 | leency | 74 | { |
8281 | leency | 75 | autoclose.click(id); |
8305 | leency | 76 | if (id==BTN_EXIT) { StopDownloading(); ExitProcess(); } |
77 | if (id==BTN_START) StartDownloading(); |
||
78 | if (id==BTN_STOP) StopDownloading(); |
||
8839 | leency | 79 | if (id==BTN_DIR) { RunProgram("/sys/File managers/Eolite", #filepath); ExitProcess(); } |
80 | if (id==BTN_RUN) { RunProgram("/sys/@open", #filepath); ExitProcess(); } |
||
6001 | leency | 81 | } |
8305 | leency | 82 | |
83 | void ProcessKeyPress() |
||
84 | { |
||
85 | @GetKey(); |
||
86 | edit_box_key stdcall(#ed); |
||
87 | EAX >>= 16; |
||
88 | if (AL == SCAN_CODE_ENTER) StartDownloading(); |
||
89 | if (AL == SCAN_CODE_ESC) StopDownloading(); |
||
90 | } |
||
6374 | guillem | 91 | |
8278 | leency | 92 | void DrawWindow() |
6374 | guillem | 93 | { |
8278 | leency | 94 | sc.get(); |
95 | pb.frame_color = sc.work_dark; |
||
8305 | leency | 96 | DefineAndDrawWindow(110 + random(300), 100 + random(300), WIN_W+9, |
9597 | leency | 97 | WIN_H + 5 + skin_h, 0x34, sc.work, DL_WINDOW_HEADER, 0); |
8278 | leency | 98 | |
8305 | leency | 99 | #define BUT_Y 58; |
100 | //autoclose.draw(WIN_W-135, BUT_Y+6); |
||
101 | if (!http.transfer) |
||
7281 | leency | 102 | { |
8305 | leency | 103 | DrawStandartCaptButton(GAPX, BUT_Y, BTN_START, T_DOWNLOAD); |
104 | if (filepath) |
||
7281 | leency | 105 | { |
8305 | leency | 106 | DrawStandartCaptButton(GAPX+102, BUT_Y, BTN_DIR, T_OPEN_DIR); |
107 | DrawStandartCaptButton(GAPX+276, BUT_Y, BTN_RUN, T_RUN); |
||
7281 | leency | 108 | } |
8305 | leency | 109 | } else { |
9287 | leency | 110 | DrawStandartCaptButton(WIN_W - 120, BUT_Y, BTN_STOP, T_CANCEL); |
8305 | leency | 111 | DrawDownloadingProgress(); |
7281 | leency | 112 | } |
8305 | leency | 113 | //ed.offset=0; //DEL? |
7281 | leency | 114 | DrawEditBox(#ed); |
5519 | leency | 115 | } |
6374 | guillem | 116 | |
6001 | leency | 117 | void StartDownloading() |
118 | { |
||
8354 | leency | 119 | char get_url[URL_SIZE+33]; |
8305 | leency | 120 | if (http.transfer > 0) return; |
9287 | leency | 121 | ResetDownloadSpeed(); |
8305 | leency | 122 | filepath = '\0'; |
8354 | leency | 123 | if (!strncmp(#uEdit,"https:",6)) { |
124 | miniprintf(#get_url, "http://gate.aspero.pro/?site=%s", #uEdit); |
||
8291 | leency | 125 | //notify("'HTTPS for download is not supported, trying to download the file via HTTP' -W"); |
8354 | leency | 126 | //miniprintf(#http_url, "http://%s", #uEdit+8); |
8291 | leency | 127 | //if (!downloader.Start(#http_url)) { |
128 | // notify("'Download failed.' -E"); |
||
129 | // StopDownloading(); |
||
130 | //} |
||
8354 | leency | 131 | } else { |
132 | strcpy(#get_url, #uEdit); |
||
8305 | leency | 133 | } |
8354 | leency | 134 | if (http.get(#get_url)) { |
8305 | leency | 135 | ed.blur_border_color = 0xCACACA; |
8319 | leency | 136 | EditBox_UpdateText(#ed, ed_disabled); |
8305 | leency | 137 | pb.value = 0; |
8278 | leency | 138 | DrawWindow(); |
8305 | leency | 139 | } else { |
8278 | leency | 140 | notify(T_ERROR_STARTING_DOWNLOAD); |
7281 | leency | 141 | StopDownloading(); |
8305 | leency | 142 | if (exit_param) ExitProcess(); |
7281 | leency | 143 | } |
6001 | leency | 144 | } |
7521 | leency | 145 | |
8305 | leency | 146 | |
147 | void DrawDownloadingProgress() |
||
7521 | leency | 148 | { |
8305 | leency | 149 | char bytes_received[70]; |
9287 | leency | 150 | dword gotkb = http.content_received/1024; |
7521 | leency | 151 | |
8305 | leency | 152 | EDI = http.content_received / 100; |
153 | if (pb.value == EDI) return; |
||
7521 | leency | 154 | |
8305 | leency | 155 | pb.value = EDI; |
156 | pb.max = http.content_length / 100; |
||
157 | progressbar_draw stdcall(#pb); |
||
9287 | leency | 158 | CalculateDownloadSpeed(); |
159 | sprintf(#bytes_received, KB_RECEIVED, gotkb/1024, gotkb%1024/103, speed); |
||
8278 | leency | 160 | WriteTextWithBg(GAPX, pb.top + 22, 0xD0, sc.work_text, #bytes_received, sc.work); |
5493 | leency | 161 | } |
6374 | guillem | 162 | |
5493 | leency | 163 | void StopDownloading() |
164 | { |
||
8319 | leency | 165 | http.stop(); |
166 | if (http.content_pointer) http.content_pointer = free(http.content_pointer); |
||
8305 | leency | 167 | http.content_received = http.content_length = 0; |
168 | |||
7281 | leency | 169 | ed.blur_border_color = 0xFFFfff; |
8319 | leency | 170 | EditBox_UpdateText(#ed, ed_focus); |
8278 | leency | 171 | DrawWindow(); |
7281 | leency | 172 | } |
173 | |||
8305 | leency | 174 | void MonitorProgress() |
7281 | leency | 175 | { |
8305 | leency | 176 | char redirect_url[URL_SIZE]; |
177 | if (http.transfer <= 0) return; |
||
178 | http.receive(); |
||
179 | if (!http.content_length) http.content_length = http.content_received * 20; //MOVE? |
||
180 | |||
8319 | leency | 181 | if (http.receive_result) { |
182 | DrawDownloadingProgress(); |
||
183 | } else { |
||
184 | if (http.status_code >= 300) && (http.status_code < 400) { |
||
185 | http.header_field("location", #redirect_url, URL_SIZE); |
||
8354 | leency | 186 | get_absolute_url(#redirect_url, #uEdit); |
187 | edit_box_set_text stdcall (#ed, #redirect_url); |
||
8305 | leency | 188 | StopDownloading(); |
189 | StartDownloading(); |
||
190 | return; |
||
191 | } |
||
8697 | leency | 192 | SaveFile(0); |
8305 | leency | 193 | if (exit_param) ExitProcess(); |
194 | StopDownloading(); |
||
195 | DrawWindow(); |
||
8319 | leency | 196 | } |
8305 | leency | 197 | } |
198 | |||
8697 | leency | 199 | void SaveFile(int attempt) |
8305 | leency | 200 | { |
8697 | leency | 201 | int i, fi=0; |
7281 | leency | 202 | char notify_message[4296]; |
8319 | leency | 203 | char file_name[URL_SIZE+96]; |
7281 | leency | 204 | |
8319 | leency | 205 | strcpy(#filepath, #save_dir); |
206 | chrcat(#filepath, '/'); |
||
207 | |||
8697 | leency | 208 | if (attempt > 9) { |
209 | notify("'Too many saving attempts' -E"); |
||
210 | return; |
||
211 | } |
||
212 | |||
213 | if (attempt > 0) { |
||
214 | chrcat(#filepath, attempt+'0'); |
||
215 | chrcat(#filepath, '_'); |
||
216 | } |
||
217 | |||
8305 | leency | 218 | //Content-Disposition: attachment; filename="RealFootball_2018_Nokia_5800_EN_IGP_EU_TS_101.zip" |
8319 | leency | 219 | if (http.header_field("content-disposition", #file_name, sizeof(file_name))) { |
220 | if (EDX = strstr(#file_name,"filename=\"")) { |
||
221 | strcat(#filepath, EDX+10); |
||
222 | ESBYTE[strchr(#filepath,'\"')] = '\0'; |
||
223 | } |
||
224 | } else { |
||
225 | // Clean all slashes at the end |
||
8354 | leency | 226 | strcpy(#file_name, #uEdit); |
8319 | leency | 227 | while (file_name[strlen(#file_name)-1] == '/') { |
228 | file_name[strlen(#file_name)-1] = 0; |
||
229 | } |
||
230 | strcat(#filepath, #file_name+strrchr(#file_name, '/')); |
||
7281 | leency | 231 | } |
232 | |||
233 | for (i=0; i |
||
234 | |||
8697 | leency | 235 | while (file_exists(#filepath)) { |
236 | SaveFile(attempt+1); |
||
237 | return; |
||
238 | } |
||
239 | |||
8319 | leency | 240 | if (CreateFile(http.content_received, http.content_pointer, #filepath)==0) { |
8278 | leency | 241 | miniprintf(#notify_message, FILE_SAVED_AS, #filepath); |
7765 | leency | 242 | } else { |
8278 | leency | 243 | miniprintf(#notify_message, FILE_NOT_SAVED, #filepath); |
7765 | leency | 244 | } |
8839 | leency | 245 | |
8305 | leency | 246 | if (!exit_param) notify(#notify_message); |
8839 | leency | 247 | |
248 | if (!strcmpi(#filepath+strlen(#filepath)-4, ".zip")) |
||
249 | || (!strcmpi(#filepath+strlen(#filepath)-3, ".7z")) { |
||
250 | Unarchive(#filepath); |
||
251 | } |
||
252 | |||
8413 | leency | 253 | if (open_file) ProcessButtonClick(BTN_RUN); |
8305 | leency | 254 | if (autoclose.checked) ExitProcess(); |
255 | } |
||
8278 | leency | 256 | |
8839 | leency | 257 | void Unarchive(dword _arc) |
258 | { |
||
259 | char folder_name[4096]; |
||
260 | strcpy(#folder_name, "/tmp0/1/Downloads/"); |
||
261 | strcpy(#folder_name, #filepath+strrchr(#filepath, '/')); |
||
262 | folder_name[strlen(#folder_name)-4] = '\0'; |
||
263 | CreateDir(#folder_name); |
||
8305 | leency | 264 | |
8839 | leency | 265 | strcpy(#param, "-o \""); |
266 | strcat(#param, #folder_name); |
||
267 | strcat(#param, "\" -h \""); |
||
268 | strcat(#param, #filepath); |
||
269 | chrcat(#param, '\"'); |
||
270 | RunProgram("/sys/unz", #param); |
||
271 | } |
||
272 | |||
273 | |||
8305 | leency | 274 | struct TIME |
275 | { |
||
276 | dword old; |
||
277 | dword cur; |
||
278 | dword gone; |
||
279 | } time = {0,0,0}; |
||
280 | |||
281 | dword netdata_received; |
||
282 | |||
9287 | leency | 283 | void ResetDownloadSpeed() |
8305 | leency | 284 | { |
9287 | leency | 285 | time.old = 0; |
286 | netdata_received = 0; |
||
287 | } |
||
288 | |||
289 | void CalculateDownloadSpeed() |
||
290 | { |
||
8305 | leency | 291 | time.cur = GetStartTime(); |
292 | |||
293 | if (time.old) { |
||
294 | time.gone = time.cur - time.old; |
||
9287 | leency | 295 | if (time.gone >= 200) { |
296 | speed = http.content_received - netdata_received / time.gone / 10; |
||
8305 | leency | 297 | time.old = time.cur; |
298 | netdata_received = http.content_received; |
||
299 | } |
||
9287 | leency | 300 | } else { |
301 | time.old = time.cur; |
||
8278 | leency | 302 | } |
9287 | leency | 303 | }>=> |