From 166efb1e90ddf78c5d79c3ed00545689d14baa01 Mon Sep 17 00:00:00 2001 From: hxy7yx <1595670487@qq.com> Date: Fri, 13 Oct 2023 13:40:34 +0800 Subject: [PATCH] remove api: file --- CMakeLists.txt | 1 - plugins/restful/file_handle.c | 191 ---------------------------------- plugins/restful/file_handle.h | 28 ----- plugins/restful/handle.c | 19 ---- src/parser/neu_json_file.c | 81 -------------- src/parser/neu_json_file.h | 52 --------- 6 files changed, 372 deletions(-) delete mode 100644 plugins/restful/file_handle.c delete mode 100644 plugins/restful/file_handle.h delete mode 100644 src/parser/neu_json_file.c delete mode 100644 src/parser/neu_json_file.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 3f367636..57dda448 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -130,7 +130,6 @@ set(NEURON_SOURCES src/adapter/driver/driver.c plugins/restful/handle.c plugins/restful/log_handle.c - plugins/restful/file_handle.c plugins/restful/metric_handle.c plugins/restful/normal_handle.c plugins/restful/rw_handle.c diff --git a/plugins/restful/file_handle.c b/plugins/restful/file_handle.c deleted file mode 100644 index 182a50c2..00000000 --- a/plugins/restful/file_handle.c +++ /dev/null @@ -1,191 +0,0 @@ -/** - * 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 - -#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" -#include "parser/neu_json_file.h" - -void handle_file_info(nng_aio *aio) -{ - char dir_path[128] = { 0 }; - - int n_file = 0; - struct dirent *files = { 0 }; - char * f_name[256] = { 0 }; - char c_time[32] = { 0 }; - char m_time[32] = { 0 }; - - neu_json_get_file_list_resp_t file_resp = { 0 }; - char * result = NULL; - - NEU_VALIDATE_JWT(aio); - - if (neu_http_get_param_str(aio, "dir_path", dir_path, sizeof(dir_path)) <= - 0) { - NEU_JSON_RESPONSE_ERROR(NEU_ERR_PARAM_IS_WRONG, { - neu_http_response(aio, error_code.error, result_error); - }); - - return; - } - - /*check whether the directory exists*/ - if (0 != access(dir_path, F_OK)) { - NEU_JSON_RESPONSE_ERROR(NEU_ERR_FILE_NOT_EXIST, { - neu_http_response(aio, error_code.error, result_error); - }); - - return; - } - - /*open directory*/ - DIR *dir = opendir(dir_path); - if (dir == NULL) { - nlog_error("open a directory failed"); - NEU_JSON_RESPONSE_ERROR(NEU_ERR_FILE_OPEN_FAILURE, { - neu_http_response(aio, error_code.error, result_error); - }); - - return; - } - - /*read directory*/ - while ((files = readdir(dir)) != NULL) { - if (strcmp(files->d_name, ".") == 0 || - strcmp(files->d_name, "..") == 0) { - continue; - } - - f_name[n_file] = files->d_name; - ++n_file; - } - - /*handle response*/ - file_resp.n_file = n_file; - file_resp.file = calloc(file_resp.n_file, sizeof(neu_json_get_file_resp_t)); - - for (int i = 0; i < file_resp.n_file; i++) { - char file_path[512] = { 0 }; - struct stat statbuf; - - sprintf(file_path, "%s/%s", dir_path, f_name[i]); - stat(file_path, &statbuf); - - strncpy(c_time, ctime(&statbuf.st_ctim.tv_sec), - strlen(ctime(&statbuf.st_ctim.tv_sec)) - 1); - strncpy(m_time, ctime(&statbuf.st_mtim.tv_sec), - strlen(ctime(&statbuf.st_mtim.tv_sec)) - 1); - - file_resp.file[i].name = f_name[i]; - file_resp.file[i].size = statbuf.st_size; - file_resp.file[i].ctime = c_time; - file_resp.file[i].mtime = m_time; - } - - neu_json_encode_by_fn(&file_resp, neu_json_encode_get_file_list_resp, - &result); - - closedir(dir); - neu_http_ok(aio, result); - free(result); - free(file_resp.file); - - return; -} - -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 = neu_http_response_file(aio, file_data, file_len, http_resp); - free(file_data); - nlog_notice("download file %s, rv: %d", file_name, rv); -} \ No newline at end of file diff --git a/plugins/restful/file_handle.h b/plugins/restful/file_handle.h deleted file mode 100644 index a4cb668b..00000000 --- a/plugins/restful/file_handle.h +++ /dev/null @@ -1,28 +0,0 @@ -/** - * 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); -void handle_file_info(nng_aio *aio); - -#endif \ No newline at end of file diff --git a/plugins/restful/handle.c b/plugins/restful/handle.c index 445163ca..fb2e1684 100644 --- a/plugins/restful/handle.c +++ b/plugins/restful/handle.c @@ -26,7 +26,6 @@ #include "adapter_handle.h" #include "datatag_handle.h" -#include "file_handle.h" #include "global_config_handle.h" #include "group_config_handle.h" #include "log_handle.h" @@ -111,12 +110,6 @@ static struct neu_http_handler cors_handler[] = { { .url = "/api/v2/log/level", }, - { - .url = "/api/v2/file", - }, - { - .url = "/api/v2/file/info", - }, { .url = "/api/v2/global/config", }, @@ -377,18 +370,6 @@ 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, - }, - { - .method = NEU_HTTP_METHOD_GET, - .type = NEU_HTTP_HANDLER_FUNCTION, - .url = "/api/v2/file/info", - .value.handler = handle_file_info, - }, { .method = NEU_HTTP_METHOD_GET, .type = NEU_HTTP_HANDLER_FUNCTION, diff --git a/src/parser/neu_json_file.c b/src/parser/neu_json_file.c deleted file mode 100644 index 2345ac2d..00000000 --- a/src/parser/neu_json_file.c +++ /dev/null @@ -1,81 +0,0 @@ -/** - * 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. - **/ - -/* - * DO NOT EDIT THIS FILE MANUALLY! - * It was automatically generated by `json-autotype`. - */ - -#include -#include - -#include "json/json.h" - -#include "neu_json_file.h" - -int neu_json_encode_get_file_list_resp(void *json_object, void *param) -{ - int ret = 0; - neu_json_get_file_list_resp_t *resp = - (neu_json_get_file_list_resp_t *) param; - - void * file_list_array = neu_json_array(); - neu_json_get_file_resp_t *p_file = resp->file; - - for (int i = 0; i < resp->n_file; i++) { - neu_json_elem_t file_elems[] = { - { - .name = "name", - .t = NEU_JSON_STR, - .v.val_str = p_file->name, - }, - { - .name = "size", - .t = NEU_JSON_INT, - .v.val_int = p_file->size, - }, - { - .name = "ctime", - .t = NEU_JSON_STR, - .v.val_str = p_file->ctime, - }, - { - .name = "mtime", - .t = NEU_JSON_STR, - .v.val_str = p_file->mtime, - }, - }; - - file_list_array = neu_json_encode_array(file_list_array, file_elems, - NEU_JSON_ELEM_SIZE(file_elems)); - - p_file++; - } - - neu_json_elem_t resp_elems[] = { { - .name = "files", - .t = NEU_JSON_OBJECT, - .v.val_object = file_list_array, - } }; - - ret = neu_json_encode_field(json_object, resp_elems, - NEU_JSON_ELEM_SIZE(resp_elems)); - - return ret; -} \ No newline at end of file diff --git a/src/parser/neu_json_file.h b/src/parser/neu_json_file.h deleted file mode 100644 index f071ff7e..00000000 --- a/src/parser/neu_json_file.h +++ /dev/null @@ -1,52 +0,0 @@ -/** - * 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. - **/ - -/* - * DO NOT EDIT THIS FILE MANUALLY! - * It was automatically generated by `json-autotype`. - */ - -#ifndef _NEU_JOSN_API_NEU_JSON_FILE_H_ -#define _NEU_JSON_API_NEU_JSON_FILE_H_ - -#include "json/json.h" - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct { - char *name; - int size; - char *ctime; - char *mtime; -} neu_json_get_file_resp_t; - -typedef struct { - int n_file; - neu_json_get_file_resp_t *file; -} neu_json_get_file_list_resp_t; - -int neu_json_encode_get_file_list_resp(void *json_object, void *param); - -#ifdef __cplusplus -} -#endif - -#endif \ No newline at end of file