From dcd1f2966355eb42ce1c22b18709a47e832519d5 Mon Sep 17 00:00:00 2001 From: liyuxi Date: Thu, 16 Feb 2023 09:53:43 +0000 Subject: [PATCH] feat(api):add a api for downloading files --- CMakeLists.txt | 1 + include/neuron/define.h | 1 + plugins/restful/file_handle.c | 102 ++++++++++++++++++++++++++++++++++ plugins/restful/file_handle.h | 27 +++++++++ plugins/restful/handle.c | 10 ++++ plugins/restful/log_handle.c | 8 +-- plugins/restful/log_handle.h | 4 ++ 7 files changed, 147 insertions(+), 6 deletions(-) create mode 100644 plugins/restful/file_handle.c create mode 100644 plugins/restful/file_handle.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e7d01b30..5b38e3e9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -109,6 +109,7 @@ set(NEURON_SOURCES src/adapter/driver/driver.c plugins/restful/handle.c plugins/restful/log_handle.c + plugins/restful/file_handle.c plugins/restful/normal_handle.c plugins/restful/rw_handle.c plugins/restful/adapter_handle.c diff --git a/include/neuron/define.h b/include/neuron/define.h index 953c65b4..11acc2ca 100644 --- a/include/neuron/define.h +++ b/include/neuron/define.h @@ -37,6 +37,7 @@ #define NEU_USER_PASSWORD_MIN_LEN 4 #define NEU_USER_PASSWORD_MAX_LEN 16 #define NEU_LOG_LEVEL_LEN 9 +#define NEU_FILE_PATH_LEN 128 extern int default_log_level; extern bool disable_jwt; diff --git a/plugins/restful/file_handle.c b/plugins/restful/file_handle.c new file mode 100644 index 00000000..a1a0a9e8 --- /dev/null +++ b/plugins/restful/file_handle.c @@ -0,0 +1,102 @@ +/** + * NEURON IIoT System for Industry 4.0 + * Copyright (C) 2020-2022 EMQ Technologies Co., Ltd All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + **/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "define.h" +#include "handle.h" +#include "utils/http.h" + +#include "plugin.h" +#include "utils/log.h" +#include "json/neu_json_error.h" +#include "json/neu_json_fn.h" + +#include "file_handle.h" +#include "log_handle.h" + +void handle_download_file(nng_aio *aio) +{ + char file_name[NEU_FILE_PATH_LEN] = { 0 }; + char file_path[256] = { 0 }; + char http_resp[256] = { 0 }; + + int rv = 0; + void * file_data = NULL; + size_t file_len = 0; + char * p = NULL; + + NEU_VALIDATE_JWT(aio); + + if (neu_http_get_param_str(aio, "file_path", file_path, + sizeof(file_path)) <= 0) { + NEU_JSON_RESPONSE_ERROR(NEU_ERR_PARAM_IS_WRONG, { + neu_http_response(aio, error_code.error, result_error); + }); + + return; + } + + /*check whether the file exists*/ + rv = access(file_path, F_OK); + if (0 != rv) { + NEU_JSON_RESPONSE_ERROR(NEU_ERR_FILE_NOT_EXIST, { + neu_http_response(aio, error_code.error, result_error); + }); + + return; + } + + /*read file content*/ + rv = read_file(file_path, &file_data, &file_len); + if (0 != rv) { + NEU_JSON_RESPONSE_ERROR( + rv, { neu_http_response(aio, error_code.error, result_error); }); + + return; + } + + /*extract file name*/ + p = strchr(file_path, '/'); + while (strchr(p, '/') != NULL) { + p = strchr(p, '/') + 1; + } + + if (p != NULL) { + strcpy(file_name, p); + } + + /*handle http response*/ + sprintf(http_resp, "%s%s", "attachment; filename=", file_name); + rv = http_resp_files(aio, file_data, file_len, http_resp); + if (0 != rv) { + return; + } +} \ No newline at end of file diff --git a/plugins/restful/file_handle.h b/plugins/restful/file_handle.h new file mode 100644 index 00000000..957a970e --- /dev/null +++ b/plugins/restful/file_handle.h @@ -0,0 +1,27 @@ +/** + * NEURON IIoT System for Industry 4.0 + * Copyright (C) 2020-2022 EMQ Technologies Co., Ltd All rights reserved. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + **/ + +#ifndef _NEU_FILE_HANDLE_H_ +#define _NEU_FILE_HANDLE_H_ + +#include + +void handle_download_file(nng_aio *aio); + +#endif \ No newline at end of file diff --git a/plugins/restful/handle.c b/plugins/restful/handle.c index 80fe39f9..287c7d8e 100644 --- a/plugins/restful/handle.c +++ b/plugins/restful/handle.c @@ -26,6 +26,7 @@ #include "adapter_handle.h" #include "datatag_handle.h" +#include "file_handle.h" #include "group_config_handle.h" #include "log_handle.h" #include "normal_handle.h" @@ -97,6 +98,9 @@ static struct neu_http_handler cors_handler[] = { { .url = "/api/v2/log/level", }, + { + .url = "/api/v2/file", + } }; static struct neu_http_handler rest_handlers[] = { @@ -300,6 +304,12 @@ static struct neu_http_handler rest_handlers[] = { .url = "/api/v2/version", .value.handler = handle_get_version, }, + { + .method = NEU_HTTP_METHOD_GET, + .type = NEU_HTTP_HANDLER_FUNCTION, + .url = "/api/v2/file", + .value.handler = handle_download_file, + } }; void neu_rest_handler(const struct neu_http_handler **handlers, uint32_t *size) diff --git a/plugins/restful/log_handle.c b/plugins/restful/log_handle.c index 983662c3..2ffda84e 100644 --- a/plugins/restful/log_handle.c +++ b/plugins/restful/log_handle.c @@ -44,10 +44,6 @@ UT_array *collect_log_files(); -int read_file(const char *file_name, void **datap, size_t *lenp); -static int http_resp_files(nng_aio *aio, void *data, size_t len, - const char *disposition); - void handle_logs_files(nng_aio *aio) { char path[128] = { 0 }; @@ -184,8 +180,8 @@ void handle_log_level(nng_aio *aio) }) } -static int http_resp_files(nng_aio *aio, void *data, size_t len, - const char *disposition) +int http_resp_files(nng_aio *aio, void *data, size_t len, + const char *disposition) { nng_http_res *res = NULL; int rv = 0; diff --git a/plugins/restful/log_handle.h b/plugins/restful/log_handle.h index 7173ea7e..b12aa042 100644 --- a/plugins/restful/log_handle.h +++ b/plugins/restful/log_handle.h @@ -26,4 +26,8 @@ void handle_logs_files(nng_aio *aio); void handle_log_level(nng_aio *aio); +int read_file(const char *file_name, void **datap, size_t *lenp); +int http_resp_files(nng_aio *aio, void *data, size_t len, + const char *disposition); + #endif