aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet_chat_lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gnunet_chat_lib.c')
-rw-r--r--src/gnunet_chat_lib.c41
1 files changed, 31 insertions, 10 deletions
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
614int 614struct GNUNET_CHAT_File*
615GNUNET_CHAT_context_send_file (struct GNUNET_CHAT_Context *context, 615GNUNET_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
679file_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