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