|
|
|
|
@ -12,14 +12,15 @@
|
|
|
|
|
#include <unistd.h> |
|
|
|
|
#include <libgen.h> |
|
|
|
|
|
|
|
|
|
int attachment_send(const char* channel_id, const char* src_file_path, const char* db_path) { |
|
|
|
|
int attachment_send(const char* channel_id, const char* src_file_path, const char* db_path, |
|
|
|
|
const uint8_t reply_to[32], uint64_t request_id) { |
|
|
|
|
if (!channel_id || !src_file_path || !db_path) { |
|
|
|
|
DEBUG_ERROR(DEBUG_CATEGORY_GENERAL, "attachment_send: invalid args"); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
struct UASYNC* ua = instance_lite_get_uasync(); |
|
|
|
|
if (!ua) { DEBUG_ERROR(DEBUG_CATEGORY_GENERAL, "attachment_send: no uasync"); return -1; } |
|
|
|
|
if (!uasync_is_running(ua)) { DEBUG_ERROR(DEBUG_CATEGORY_GENERAL, "attachment_send: core stopped"); return -1; } |
|
|
|
|
|
|
|
|
|
/* get original basename for display, then make unique filename for storage */ |
|
|
|
|
char path_copy[1024]; |
|
|
|
|
@ -52,6 +53,7 @@ int attachment_send(const char* channel_id, const char* src_file_path, const cha
|
|
|
|
|
tmp[off++] = media_dir[i]; |
|
|
|
|
if (media_dir[i] == '/' && off > 1) { tmp[off] = '\0'; mkdir(tmp, 0755); } |
|
|
|
|
} |
|
|
|
|
tmp[off] = '\0'; |
|
|
|
|
mkdir(tmp, 0755); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@ -64,9 +66,14 @@ int attachment_send(const char* channel_id, const char* src_file_path, const cha
|
|
|
|
|
data = original_basename — becomes first part of message "d" field: "name.ext|size|..." */ |
|
|
|
|
size_t datalen = strlen(original_basename); |
|
|
|
|
struct chat_msg_submit* req = u_calloc(1, sizeof(struct chat_msg_submit) + datalen + 1); |
|
|
|
|
if (!req) return -1; |
|
|
|
|
struct posted_task* post = u_calloc(1, sizeof(*post)); |
|
|
|
|
if (!req || !post) { |
|
|
|
|
DEBUG_ERROR(DEBUG_CATEGORY_MEDIA, "attachment_send: allocation failed"); u_free(req); u_free(post); return -1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
req->inst = instance_lite_get_instance(); |
|
|
|
|
memcpy(req->reply_to, reply_to, sizeof(req->reply_to)); |
|
|
|
|
req->request_id = request_id; |
|
|
|
|
snprintf(req->channel_id, sizeof(req->channel_id), "%s", channel_id); |
|
|
|
|
snprintf(req->content_type, sizeof(req->content_type), "application/octet-stream"); |
|
|
|
|
snprintf(req->media_src, sizeof(req->media_src), "%s", src_file_path); |
|
|
|
|
@ -80,6 +87,7 @@ int attachment_send(const char* channel_id, const char* src_file_path, const cha
|
|
|
|
|
DEBUG_INFO(DEBUG_CATEGORY_GENERAL, "attachment_send: ch=%s src=%s dst=%s name=%s size=%lld", |
|
|
|
|
channel_id, src_file_path, media_file, original_basename, (long long)st.st_size); |
|
|
|
|
|
|
|
|
|
uasync_post(ua, chat_core_submit_trampoline, req); |
|
|
|
|
post->callback = chat_core_submit_trampoline; post->arg = req; |
|
|
|
|
uasync_post_reserved(ua, post); |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|