Improve:goahead show picture.

This commit is contained in:
Fancy code 2024-03-25 18:28:52 +08:00
parent 4780a46e8a
commit ddb61c4b91
3 changed files with 61 additions and 3 deletions

View File

@ -68,6 +68,7 @@ set(CURL_SHARED_LIBS_PATH "/mnt/mmc")
# ------------ build curl + openssl ------------ end
# ------------ build AppManager ------------ #
set(APP_MANAGER_HTTP_SERVER_IP "localhost")
# set(APP_MANAGER_HTTP_SERVER_IP "localhost")
set(APP_MANAGER_HTTP_SERVER_IP "192.168.1.29")
set(APP_MANAGER_HTTP_SERVER_PORT "8080")
# ------------ build AppManager end ------------ #

View File

@ -53,7 +53,9 @@ protected:
// ../output_files/test/bin/AppManagerTest --gtest_filter=AppManagerTest.INTEGRATION_AppManager_EXAMPLE_Demo0
TEST_F(AppManagerTest, INTEGRATION_AppManager_EXAMPLE_Demo0)
{
std::shared_ptr<VAppMonitor> monitor = AppManagerTestTool::MakeMonitorMock();
IAppManager::GetInstance()->Init(mAppParam);
IAppManager::GetInstance()->SetAppMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IAppManager::GetInstance()->UnInit();

View File

@ -19,6 +19,7 @@
#include <signal.h>
static int finished = 0;
static HttpHandleCallback gHttpHandle = NULL;
constexpr int UNKNOWN_LENGTH = -1;
static void sigHandler(int signo)
{
LogInfo("Stop goahead web server.\n");
@ -87,7 +88,7 @@ static void response_handle(const char *responseStr, void *context)
static bool AppRequestHandle(Webs *wp)
{
websSetStatus(wp, 200);
websWriteHeaders(wp, -1, 0);
websWriteHeaders(wp, UNKNOWN_LENGTH, 0);
websWriteHeader(wp, "Content-Type", "text/plain");
websWriteEndHeaders(wp);
gHttpHandle(wp->url, 0, response_handle, wp);
@ -101,7 +102,7 @@ static bool AppUploadHandle(Webs *wp)
char *upfile = nullptr;
websSetStatus(wp, 200);
websWriteHeaders(wp, -1, 0);
websWriteHeaders(wp, UNKNOWN_LENGTH, 0);
websWriteHeader(wp, "Content-Type", "text/plain");
websWriteEndHeaders(wp);
@ -126,6 +127,58 @@ static bool AppUploadHandle(Webs *wp)
websDone(wp);
return 1;
}
static bool AppGetThumbnail(Webs *wp)
{
LogInfo("sssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss\n");
websSetStatus(wp, 200);
websWriteHeaders(wp, UNKNOWN_LENGTH, 0);
websWriteHeader(wp, "Content-Type", "image/jpeg");
websWriteEndHeaders(wp);
// gHttpHandle(wp->url, 0, response_handle, wp);
// websWrite(wp, "ssssssssssssssssssssssssss");
char imagePath[] = "./34a396526922a33e97906920dbfef2a5.jpg";
FILE *file = nullptr;
unsigned char *buffer = nullptr;
size_t bytesRead = 0;
long int fsize = 0;
/* 打开图片文件 */
file = fopen(imagePath, "rb");
if (file == nullptr) {
/* 文件打开失败,返回 404 错误 */
LogError("Open picture failed.\n");
goto END;
}
// 获取文件大小
fseek(file, 0, SEEK_END);
fsize = ftell(file);
rewind(file);
// 分配足够大的缓冲区来存储文件内容
buffer = (unsigned char *)malloc(fsize + 1);
if (!buffer) {
LogError("malloc failed.\n");
fclose(file);
goto END;
}
memset(buffer, 0, fsize + 1);
// 读取文件内容到缓冲区
bytesRead = fread(buffer, 1, fsize, file);
if (bytesRead != fsize) {
LogError("fread picture failed.\n");
free(buffer);
fclose(file);
goto END;
}
// websWrite(wp, "%s", buffer);
websWriteBlock(wp, (cchar *)buffer, fsize);
END:
if (buffer) {
free(buffer);
}
websDone(wp);
return 1;
}
StatusCode WebServerInit(const WebServerParam webParam)
{
CheckUploadDir();
@ -161,6 +214,8 @@ StatusCode WebServerInit(const WebServerParam webParam)
websDefineHandler("AppUploadHandle", 0, AppUploadHandle, 0, 0);
websAddRoute("/upload", "AppUploadHandle", 0);
}
websDefineHandler("AppGetThumbnail", 0, AppGetThumbnail, 0, 0);
websAddRoute("/app/getthumbnail", "AppGetThumbnail", 0);
websServiceEvents(&finished);
logmsg(1, "Instructed to exit\n");
websClose();