application/upload.c

changeset 87
5360027fb282
equal deleted inserted replaced
86:8e7c57c23133 87:5360027fb282
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2024 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "upload.h"
30
31 #include "davcontroller.h"
32 #include "window.h"
33
34 #include <cx/printf.h>
35
36 #include "config.h"
37
38 #include "system.h"
39 #include "common/context.h"
40
41 #include <libidav/config.h>
42 #include <libidav/utils.h>
43
44 // ------------------------------------- File Upload -------------------------------------
45
46 static double upload_progress(DavFileUpload *upload) {
47 return ((double)upload->progress.transferred_bytes / (double)upload->progress.total_bytes) * 100;
48 }
49
50 static void update_upload_labels(DavFileUpload *upload) {
51 char *sz_total = util_size_str(FALSE, upload->progress.total_bytes);
52 char *sz_uploaded = util_size_str2(FALSE, upload->progress.transferred_bytes, upload->progress.total_bytes, 2);
53 char *sz_uploaded_end = strchr(sz_uploaded, ' ');
54 if (sz_uploaded_end) {
55 *sz_uploaded_end = 0;
56 }
57
58 double progress = upload_progress(upload);
59 ui_set(upload->progressbar, progress);
60
61 cxmutstr label1;
62 if (upload->progress.total_files + upload->progress.total_directories > 1) {
63 label1 = cx_asprintf(
64 "%s/%s %zu/%zu files",
65 sz_uploaded,
66 sz_total,
67 upload->progress.transferred_files+upload->progress.transferred_directories,
68 upload->progress.total_files+upload->progress.total_directories);
69 } else {
70 label1 = cx_asprintf(
71 "%s/%s",
72 sz_uploaded,
73 sz_total);
74 }
75 ui_set(upload->label_top_left, label1.ptr);
76
77 free(sz_total);
78 free(label1.ptr);
79
80 if (upload->progress.current_file_size > 0) {
81 cxmutstr file_label = cx_asprintf("%s (%.0f%%)", upload->current_file_name, ((float)upload->progress.current_file_transferred/(float)upload->progress.current_file_size)*100);
82 ui_set(upload->label_top_right, file_label.ptr);
83 free(file_label.ptr);
84 }
85 }
86
87
88
89 static int uithr_update_upload_labels(void *data) {
90 update_upload_labels(data);
91 return 0;
92 }
93
94 static void upload_dav_progress(DavResource *res, int64_t total, int64_t now, void *data) {
95 DavFileUpload *upload = data;
96 if (upload->upload_file) {
97 if (now > upload->progress.current_file_size) {
98 // current_file_size is not accurate (either the file was changed after the last stat
99 // or we have some extra bytes because of encryption
100 // adjust current_file_size and the total upload size
101 int64_t extra = now - upload->progress.current_file_size;
102 upload->progress.current_file_size += extra;
103 upload->progress.total_bytes += extra;
104 }
105
106 int64_t new_progress = now - upload->progress.current_file_transferred;
107 upload->progress.transferred_bytes += new_progress;
108 upload->progress.current_file_transferred = now;
109
110 ui_call_mainthread(uithr_update_upload_labels, upload);
111 }
112 }
113
114
115 typedef struct FileNameUpdate {
116 DavFileUpload *upload;
117 char *name;
118 char *path;
119 DavBool iscollection;
120 } FileNameUpdate;
121
122 static int uithr_update_file_label(FileNameUpdate *update) {
123 if(update->upload->cancel) {
124 return 0;
125 }
126
127 // replace upload->current_filename with update->name
128 if (update->upload->current_file_name) {
129 free(update->upload->current_file_name);
130 }
131 update->upload->current_file_name = update->name;
132
133 ui_set(update->upload->label_top_right, update->name);
134
135 DavFileUpload *upload = update->upload;
136 DavBrowser *browser = upload->browser;
137 // update the resource list in the browser, if the current collection has not changed
138 if (upload->collection == browser->current && upload->collection_ctn == browser->res_counter) {
139 char *parent = util_parent_path(update->path);
140 cxstring parent_s = cx_str(parent);
141 cxstring colpath_s = cx_str(upload->collection->path);
142 if (parent_s.length > 0 && parent_s.ptr[parent_s.length - 1] == '/') {
143 parent_s.length--;
144 }
145 if (colpath_s.length > 0 && colpath_s.ptr[colpath_s.length - 1] == '/') {
146 colpath_s.length--;
147 }
148
149 // only update, if the added resource has the current collection as parent
150 if (!cx_strcmp(parent_s, colpath_s)) {
151 DavResource *ui_res = dav_resource_new(upload->collection->session, update->path);
152 ui_res->iscollection = update->iscollection;
153 ui_res->lastmodified = time(NULL);
154 ui_res->creationdate = time(NULL);
155 upload->current_resource = ui_res;
156
157 ui_list_append(browser->resources, ui_res);
158 browser->resources->update(browser->resources, 0);
159 } else {
160 upload->current_resource = NULL; // maybe not necessary
161 }
162 free(parent);
163 }
164
165 free(update->path);
166 free(update);
167 return 0;
168 }
169
170 static size_t dufile_read(void *buf, size_t size, size_t count, void *stream) {
171 DUFile *f = (DUFile*)stream;
172 if(f->upload->cancel) {
173 return 0;
174 }
175 return fread(buf, size, count, f->fd);
176 }
177
178 static int dufile_seek(const void *stream, long off, int whence) {
179 DUFile *f = (DUFile*)stream;
180 return fseek(f->fd, off, whence);
181 }
182
183 static int qthr_file_upload(void *data) {
184 DUFile *f = data;
185 DavFileUpload *upload = f->upload;
186 DavSession *sn = upload->sn;
187
188 if(upload->cancel) {
189 return 0;
190 }
191
192 FILE *in = sys_fopen(f->path, "rb");
193 if (!in) {
194 // TODO: error msg
195 return 0;
196 }
197 f->fd = in;
198
199 upload->upload_file = TRUE;
200 upload->progress.current_file_size = f->bytes;
201 upload->progress.current_file_transferred = 0;
202
203 DavResource *res = dav_resource_new(sn, f->upload_path);
204
205 FileNameUpdate *ui_update = malloc(sizeof(FileNameUpdate));
206 ui_update->upload = upload;
207 ui_update->name = strdup(res->name);
208 ui_update->path = strdup(res->path);
209 ui_update->iscollection = FALSE;
210 ui_call_mainthread((ui_threadfunc)uithr_update_file_label, ui_update);
211
212 dav_set_content(res, f, (dav_read_func)dufile_read, (dav_seek_func)dufile_seek);
213 if (dav_store(res)) {
214 f->error = sn->error;
215 }
216 dav_resource_free(res);
217
218 fclose(in);
219
220 upload->upload_file = FALSE;
221
222 return 0;
223 }
224
225 static void uithr_file_uploaded(UiEvent *event, void *data) {
226 DUFile *file = data;
227 DavFileUpload *upload = file->upload;
228
229 upload->progress.transferred_files++;
230 //upload->uploaded_bytes += file->bytes;
231
232 double progress = upload_progress(upload);
233 ui_set(upload->progressbar, upload_progress(upload));
234
235 update_upload_labels(upload);
236
237 // update resource content length in the browser
238 DavBrowser *browser = upload->browser;
239 if (upload->collection == browser->current && upload->collection_ctn == browser->res_counter) {
240 if (upload->current_resource) {
241 upload->current_resource->contentlength = upload->progress.current_file_transferred;
242 browser->resources->update(browser->resources, 0);
243 }
244 }
245 upload->current_resource = NULL;
246
247 free(file->path);
248 free(file->upload_path);
249 }
250
251 static int qthr_dir_upload(void *data) {
252 DUFile *f = data;
253 DavFileUpload *upload = f->upload;
254 DavSession *sn = upload->sn;
255
256 DavResource *res = dav_resource_new(sn, f->upload_path);
257 res->iscollection = TRUE;
258
259 FileNameUpdate *ui_update = malloc(sizeof(FileNameUpdate));
260 ui_update->upload = upload;
261 ui_update->name = strdup(res->name);
262 ui_update->path = strdup(res->path);
263 ui_update->iscollection = TRUE;
264 ui_call_mainthread((ui_threadfunc)uithr_update_file_label, ui_update);
265
266 if (dav_create(res)) {
267 f->error = sn->error;
268 }
269
270 dav_resource_free(res);
271
272 return 0;
273 }
274
275 static void uithr_dir_uploaded(UiEvent *event, void *data) {
276 DUFile *file = data;
277 DavFileUpload *upload = file->upload;
278
279 upload->progress.transferred_directories++;
280
281 update_upload_labels(upload);
282
283 upload->current_resource = NULL;
284
285 free(file->path);
286 free(file->upload_path);
287 }
288
289 static int qthr_upload_finished(void *data) {
290 return 0;
291 }
292
293 static void uithr_upload_finished(UiEvent *event, void *data) {
294 DavFileUpload *upload = data;
295 if(upload->cancel) {
296 ui_set(upload->label_bottom_left, "Canceled");
297 }
298 if(upload->dialog->ref > 1) {
299 ui_close(upload->dialog);
300 }
301 ui_object_unref(upload->dialog);
302 }
303
304 static int jobthr_upload_scan(void *data) {
305 DavFileUpload *upload = data;
306
307 CxList *stack = cxLinkedListCreateSimple(CX_STORE_POINTERS);
308 for (int i = 0; i < upload->files.nfiles; i++) {
309 DUFile *f = malloc(sizeof(DUFile));
310 f->path = strdup(upload->files.files[i]);
311 f->upload_path = util_concat_path(upload->base_path, util_path_file_name(f->path));
312 f->isdirectory = FALSE;
313 f->bytes = 0;
314 f->upload = upload;
315 f->error = 0;
316 cxListInsert(stack, 0, f);
317 }
318
319 while (cxListSize(stack) > 0 && !upload->cancel) {
320 DUFile *f = cxListAt(stack, 0);
321
322 char *path = util_concat_path(upload->base_path, f->upload_path);
323 cxListRemove(stack, 0);
324
325 SYS_STAT s;
326 if (!sys_stat(f->path, &s)) {
327 if (S_ISDIR(s.st_mode)) {
328 f->isdirectory = TRUE;
329 upload->progress.total_directories++;
330 ui_threadpool_job(upload->queue, upload->dialog, qthr_dir_upload, f, uithr_dir_uploaded, f);
331
332 SYS_DIR dir = sys_opendir(f->path);
333 if (dir) {
334 SysDirEnt *entry;
335 int nument = 0;
336 while((entry = sys_readdir(dir)) != NULL) {
337 if(!strcmp(entry->name, ".") || !strcmp(entry->name, "..")) {
338 continue;
339 }
340
341 cxmutstr newpath = util_concat_sys_path(cx_str(f->path), cx_str(entry->name));
342 char *new_upload_path = util_concat_path(f->upload_path, entry->name);
343
344 DUFile *child = malloc(sizeof(DUFile));
345 child->path = newpath.ptr;
346 child->upload_path = new_upload_path;
347 child->isdirectory = FALSE;
348 child->bytes = 0;
349 child->upload = upload;
350 child->error = 0;
351 cxListAdd(stack, child);
352 }
353
354 sys_closedir(dir);
355 }
356 } else if (S_ISREG(s.st_mode)) {
357 f->isdirectory = FALSE;
358 f->bytes = s.st_size;
359 upload->progress.total_files++;
360 upload->progress.total_bytes += s.st_size;
361 ui_threadpool_job(upload->queue, upload->dialog, qthr_file_upload, f, uithr_file_uploaded, f);
362 }
363 }
364 } // TODO: else error msg
365
366 ui_threadpool_job(upload->queue, upload->dialog, qthr_upload_finished, upload, uithr_upload_finished, upload);
367
368 ui_filelist_free(upload->files);
369
370 return 0;
371 }
372
373 static void uithr_upload_scan_finished(UiEvent *event, void *data) {
374 DavFileUpload *upload = data;
375
376 update_upload_labels(upload);
377 }
378
379 static void upload_window_closed(UiEvent *event, void *data) {
380 DavFileUpload *upload = event->window;
381 ui_threadpool_destroy(upload->queue);
382
383 dav_session_destroy(upload->sn);
384 }
385
386 void action_upload_cancel(UiEvent *event, void *data) {
387 DavFileUpload *upload = event->window;
388 if(!upload->cancel) {
389 ui_set(upload->label_bottom_left, "Cancel...");
390 upload->cancel = TRUE;
391 }
392 }
393
394 DavFileUpload* dav_upload_create(DavBrowser *browser, UiObject *obj, UiFileList files) {
395 UiContext *ctx = obj->ctx;
396 DavFileUpload *upload = ui_malloc(ctx, sizeof(DavFileUpload));
397 memset(upload, 0, sizeof(DavFileUpload));
398 upload->dialog = obj;
399 obj->window = upload;
400 ui_object_ref(obj);
401
402 upload->progressbar = ui_double_new(ctx, "progressbar");
403 upload->label_top_left = ui_string_new(ctx, "label_top_left");
404 upload->label_top_right = ui_string_new(ctx, "label_top_right");
405 upload->label_bottom_left = ui_string_new(ctx, "label_bottom_left");
406 upload->label_bottom_right = ui_string_new(ctx, "label_bottom_right");
407
408 // we need a clone of the current session, because the upload
409 // is done in a separate thread
410 DavSession *upload_session = dav_session_clone(browser->sn);
411
412 dav_session_set_progresscallback(upload_session, NULL, upload_dav_progress, upload);
413
414 upload->browser = browser;
415 upload->sn = upload_session;
416 upload->files = files;
417 upload->base_path = ui_strdup(ctx, browser->current->path);
418 upload->queue = ui_threadpool_create(1);
419
420 upload->collection = browser->current;
421 upload->collection_ctn = browser->res_counter;
422
423 CxMempool *mp = ui_cx_mempool(ctx);
424 cxMempoolRegister(mp, upload_session, (cx_destructor_func)dav_session_destroy);
425 cxMempoolRegister(mp, upload->queue, (cx_destructor_func)ui_threadpool_destroy);
426
427 ui_set(upload->label_top_left, "");
428 ui_set(upload->label_top_right, "");
429 ui_set(upload->label_bottom_left, "");
430 ui_set(upload->label_bottom_right, "");
431 ui_set(upload->progressbar, 0);
432
433 return upload;
434 }
435
436 void dav_upload_start(DavFileUpload *upload) {
437 ui_show(upload->dialog);
438
439 // start upload and stat threads
440 ui_job(upload->dialog, jobthr_upload_scan, upload, uithr_upload_scan_finished, upload);
441 }

mercurial