diff options
author | TheJackiMonster <thejackimonster@gmail.com> | 2022-01-06 23:41:30 +0100 |
---|---|---|
committer | TheJackiMonster <thejackimonster@gmail.com> | 2022-01-06 23:41:30 +0100 |
commit | aa8a1d1faeee94b92c3bb9094cbaa1c1e63f1e38 (patch) | |
tree | 88b03df371a67451b859911e7ee3c765e5f5b903 | |
parent | a7308b22bf24131c5a0bf715e4f79e375e4bcfa2 (diff) | |
download | libgnunetchat-aa8a1d1faeee94b92c3bb9094cbaa1c1e63f1e38.tar.gz libgnunetchat-aa8a1d1faeee94b92c3bb9094cbaa1c1e63f1e38.zip |
Updated file uploading to allow setting user pointer before upload
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
-rw-r--r-- | include/gnunet_chat_lib.h | 6 | ||||
-rw-r--r-- | src/gnunet_chat_handle_intern.c | 11 | ||||
-rw-r--r-- | src/gnunet_chat_lib.c | 41 | ||||
-rw-r--r-- | src/gnunet_chat_lib_intern.c | 22 |
4 files changed, 60 insertions, 20 deletions
diff --git a/include/gnunet_chat_lib.h b/include/gnunet_chat_lib.h index cc08662..785d3ff 100644 --- a/include/gnunet_chat_lib.h +++ b/include/gnunet_chat_lib.h | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2021 GNUnet e.V. | 3 | Copyright (C) 2021--2022 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software: you can redistribute it and/or modify it | 5 | GNUnet is free software: you can redistribute it and/or modify it |
6 | under the terms of the GNU Affero General Public License as published | 6 | under the terms of the GNU Affero General Public License as published |
@@ -624,9 +624,9 @@ GNUNET_CHAT_context_send_text (struct GNUNET_CHAT_Context *context, | |||
624 | * @param[in] path Local file path | 624 | * @param[in] path Local file path |
625 | * @param[in] callback Callback for file uploading (optional) | 625 | * @param[in] callback Callback for file uploading (optional) |
626 | * @param[in,out] cls Closure for file uploading (optional) | 626 | * @param[in,out] cls Closure for file uploading (optional) |
627 | * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure | 627 | * @return The file handle on success, NULL on failure |
628 | */ | 628 | */ |
629 | int | 629 | struct GNUNET_CHAT_File* |
630 | GNUNET_CHAT_context_send_file (struct GNUNET_CHAT_Context *context, | 630 | GNUNET_CHAT_context_send_file (struct GNUNET_CHAT_Context *context, |
631 | const char *path, | 631 | const char *path, |
632 | GNUNET_CHAT_FileUploadCallback callback, | 632 | GNUNET_CHAT_FileUploadCallback callback, |
diff --git a/src/gnunet_chat_handle_intern.c b/src/gnunet_chat_handle_intern.c index 2200e8e..57f1c5b 100644 --- a/src/gnunet_chat_handle_intern.c +++ b/src/gnunet_chat_handle_intern.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2021 GNUnet e.V. | 3 | Copyright (C) 2021--2022 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software: you can redistribute it and/or modify it | 5 | GNUnet is free software: you can redistribute it and/or modify it |
6 | under the terms of the GNU Affero General Public License as published | 6 | under the terms of the GNU Affero General Public License as published |
@@ -418,7 +418,14 @@ on_handle_message (void *cls, | |||
418 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST | 418 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST |
419 | ); | 419 | ); |
420 | 420 | ||
421 | struct GNUNET_CHAT_File *file = file_create_from_message( | 421 | struct GNUNET_CHAT_File *file = GNUNET_CONTAINER_multihashmap_get( |
422 | context->handle->files, &(msg->body.file.hash) | ||
423 | ); | ||
424 | |||
425 | if (file) | ||
426 | break; | ||
427 | |||
428 | file = file_create_from_message( | ||
422 | context->handle, &(msg->body.file) | 429 | context->handle, &(msg->body.file) |
423 | ); | 430 | ); |
424 | 431 | ||
diff --git a/src/gnunet_chat_lib.c b/src/gnunet_chat_lib.c index 0e86499..6447e2d 100644 --- a/src/gnunet_chat_lib.c +++ b/src/gnunet_chat_lib.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2021 GNUnet e.V. | 3 | Copyright (C) 2021--2022 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software: you can redistribute it and/or modify it | 5 | GNUnet is free software: you can redistribute it and/or modify it |
6 | under the terms of the GNU Affero General Public License as published | 6 | under the terms of the GNU Affero General Public License as published |
@@ -611,32 +611,40 @@ GNUNET_CHAT_context_send_text (struct GNUNET_CHAT_Context *context, | |||
611 | } | 611 | } |
612 | 612 | ||
613 | 613 | ||
614 | int | 614 | struct GNUNET_CHAT_File* |
615 | GNUNET_CHAT_context_send_file (struct GNUNET_CHAT_Context *context, | 615 | GNUNET_CHAT_context_send_file (struct GNUNET_CHAT_Context *context, |
616 | const char *path, | 616 | const char *path, |
617 | GNUNET_CHAT_FileUploadCallback callback, | 617 | GNUNET_CHAT_FileUploadCallback callback, |
618 | void *cls) | 618 | void *cls) |
619 | { | 619 | { |
620 | if ((!context) || (!path) || (!(context->room))) | 620 | if ((!context) || (!path) || (!(context->room))) |
621 | return GNUNET_SYSERR; | 621 | return NULL; |
622 | 622 | ||
623 | if (!(context->handle->directory)) | 623 | if (!(context->handle->directory)) |
624 | return GNUNET_SYSERR; | 624 | return NULL; |
625 | 625 | ||
626 | struct GNUNET_HashCode hash; | 626 | struct GNUNET_HashCode hash; |
627 | if (GNUNET_OK != util_hash_file(path, &hash)) | 627 | if (GNUNET_OK != util_hash_file(path, &hash)) |
628 | return GNUNET_SYSERR; | 628 | return NULL; |
629 | |||
630 | struct GNUNET_CHAT_File *file = GNUNET_CONTAINER_multihashmap_get( | ||
631 | context->handle->files, | ||
632 | &hash | ||
633 | ); | ||
629 | 634 | ||
630 | char *filename; | 635 | char *filename; |
631 | util_get_filename ( | 636 | util_get_filename ( |
632 | context->handle->directory, "files", &hash, &filename | 637 | context->handle->directory, "files", &hash, &filename |
633 | ); | 638 | ); |
634 | 639 | ||
640 | if (file) | ||
641 | goto file_upload; | ||
642 | |||
635 | if ((GNUNET_OK != GNUNET_DISK_directory_create_for_file(filename)) || | 643 | if ((GNUNET_OK != GNUNET_DISK_directory_create_for_file(filename)) || |
636 | (GNUNET_OK != GNUNET_DISK_file_copy(path, filename))) | 644 | (GNUNET_OK != GNUNET_DISK_file_copy(path, filename))) |
637 | { | 645 | { |
638 | GNUNET_free(filename); | 646 | GNUNET_free(filename); |
639 | return GNUNET_SYSERR; | 647 | return NULL; |
640 | } | 648 | } |
641 | 649 | ||
642 | struct GNUNET_CRYPTO_SymmetricSessionKey key; | 650 | struct GNUNET_CRYPTO_SymmetricSessionKey key; |
@@ -645,17 +653,30 @@ GNUNET_CHAT_context_send_file (struct GNUNET_CHAT_Context *context, | |||
645 | if (GNUNET_OK != util_encrypt_file(filename, &key)) | 653 | if (GNUNET_OK != util_encrypt_file(filename, &key)) |
646 | { | 654 | { |
647 | GNUNET_free(filename); | 655 | GNUNET_free(filename); |
648 | return GNUNET_SYSERR; | 656 | return NULL; |
649 | } | 657 | } |
650 | 658 | ||
651 | char* p = GNUNET_strdup(path); | 659 | char* p = GNUNET_strdup(path); |
652 | 660 | ||
653 | struct GNUNET_CHAT_File *file = file_create_from_disk( | 661 | file = file_create_from_disk( |
654 | context->handle, basename(p), &hash, &key | 662 | context->handle, |
663 | basename(p), | ||
664 | &hash, | ||
665 | &key | ||
655 | ); | 666 | ); |
656 | 667 | ||
657 | GNUNET_free(p); | 668 | GNUNET_free(p); |
658 | 669 | ||
670 | if (GNUNET_OK != GNUNET_CONTAINER_multihashmap_put( | ||
671 | context->handle->files, &hash, file, | ||
672 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST)) | ||
673 | { | ||
674 | file_destroy(file); | ||
675 | GNUNET_free(filename); | ||
676 | return NULL; | ||
677 | } | ||
678 | |||
679 | file_upload: | ||
659 | file_bind_upload(file, callback, cls); | 680 | file_bind_upload(file, callback, cls); |
660 | 681 | ||
661 | struct GNUNET_FS_BlockOptions bo; | 682 | struct GNUNET_FS_BlockOptions bo; |
@@ -685,7 +706,7 @@ GNUNET_CHAT_context_send_file (struct GNUNET_CHAT_Context *context, | |||
685 | ); | 706 | ); |
686 | 707 | ||
687 | GNUNET_free(filename); | 708 | GNUNET_free(filename); |
688 | return GNUNET_OK; | 709 | return file; |
689 | } | 710 | } |
690 | 711 | ||
691 | 712 | ||
diff --git a/src/gnunet_chat_lib_intern.c b/src/gnunet_chat_lib_intern.c index d19b0eb..3ad5d4f 100644 --- a/src/gnunet_chat_lib_intern.c +++ b/src/gnunet_chat_lib_intern.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) 2021 GNUnet e.V. | 3 | Copyright (C) 2021--2022 GNUnet e.V. |
4 | 4 | ||
5 | GNUnet is free software: you can redistribute it and/or modify it | 5 | GNUnet is free software: you can redistribute it and/or modify it |
6 | under the terms of the GNU Affero General Public License as published | 6 | under the terms of the GNU Affero General Public License as published |
@@ -156,17 +156,29 @@ struct GNUNET_CHAT_ContextIterateFiles | |||
156 | 156 | ||
157 | int | 157 | int |
158 | it_context_iterate_files (void *cls, | 158 | it_context_iterate_files (void *cls, |
159 | GNUNET_UNUSED const struct GNUNET_HashCode *key, | 159 | const struct GNUNET_HashCode *key, |
160 | void *value) | 160 | GNUNET_UNUSED void *value) |
161 | { | 161 | { |
162 | GNUNET_assert((cls) && (value)); | 162 | GNUNET_assert((cls) && (key)); |
163 | 163 | ||
164 | struct GNUNET_CHAT_ContextIterateFiles *it = cls; | 164 | struct GNUNET_CHAT_ContextIterateFiles *it = cls; |
165 | 165 | ||
166 | if (!(it->cb)) | 166 | if (!(it->cb)) |
167 | return GNUNET_YES; | 167 | return GNUNET_YES; |
168 | 168 | ||
169 | struct GNUNET_CHAT_File *file = value; | 169 | struct GNUNET_CHAT_Message *message = GNUNET_CONTAINER_multihashmap_get( |
170 | it->context->messages, key | ||
171 | ); | ||
172 | |||
173 | if (!message) | ||
174 | return GNUNET_YES; | ||
175 | |||
176 | struct GNUNET_CHAT_File *file = GNUNET_CONTAINER_multihashmap_get( | ||
177 | it->context->handle->files, &(message->hash) | ||
178 | ); | ||
179 | |||
180 | if (!file) | ||
181 | return GNUNET_YES; | ||
170 | 182 | ||
171 | return it->cb(it->cls, it->context, file); | 183 | return it->cb(it->cls, it->context, file); |
172 | } | 184 | } |