mirror of
https://github.com/emqx/neuron.git
synced 2026-10-29 15:09:11 +08:00
Merge pull request #1071 from lixiumei123/main
feat(api):add a api for downloading files
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <nng/nng.h>
|
||||
#include <nng/supplemental/http/http.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
}
|
||||
@@ -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 <nng/nng.h>
|
||||
|
||||
void handle_download_file(nng_aio *aio);
|
||||
|
||||
#endif
|
||||
@@ -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)
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user