Add download zip page

This commit is contained in:
2023-09-16 13:14:23 +08:00
parent 93a307b143
commit f2b6ad465d
13 changed files with 412 additions and 16 deletions

View File

@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.14)
project(runner LANGUAGES CXX)
project(runner)
set(ENABLE_ICONV OFF CACHE BOOL "Libiconv is not needed.")
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/../../utils" "${CMAKE_BINARY_DIR}/utils")

View File

@@ -10,6 +10,10 @@
#include "flutter/generated_plugin_registrant.h"
#include <fcntl.h>
#include <io.h>
#include "err.h"
#include "fileop.h"
#include "wchar_util.h"
#define MAX_PATH_SIZE 32768
@@ -19,6 +23,22 @@ FlutterWindow::FlutterWindow(const flutter::DartProject& project)
FlutterWindow::~FlutterWindow() {}
void updateDataFromMimeType(std::wstring& defExt, std::wstring& filter, std::string mimeType) {
if (mimeType == "image/jpeg") {
filter.append(std::wstring(L"JPEG File(*.jpg)\0*.jpg\0", 23));
defExt = L"jpg";
} else if (mimeType == "image/png") {
filter.append(std::wstring(L"PNG File(*.png)\0*.png\0", 22));
defExt = L"png";
} else if (mimeType == "image/gif") {
filter.append(std::wstring(L"GIF File(*.gif)\0*.gif\0", 22));
defExt = L"gif";
} else if (mimeType == "application/zip") {
filter.append(std::wstring(L"ZIP File(*.zip)\0*.zip\0", 22));
defExt = L"zip";
}
}
bool FlutterWindow::OnCreate() {
if (!Win32Window::OnCreate()) {
return false;
@@ -87,16 +107,7 @@ bool FlutterWindow::OnCreate() {
ofn.hwndOwner = Win32Window::GetHandle();
std::wstring filter;
std::wstring defExt;
if (*mimeType == "image/jpeg") {
filter.append(std::wstring(L"JPEG File(*.jpg)\0*.jpg\0", 23));
defExt = L"jpg";
} else if (*mimeType == "image/png") {
filter.append(std::wstring(L"PNG File(*.png)\0*.png\0", 22));
defExt = L"png";
} else if (*mimeType == "image/gif") {
filter.append(std::wstring(L"GIF File(*.gif)\0*.gif\0", 22));
defExt = L"gif";
};
updateDataFromMimeType(defExt, filter, *mimeType);
filter.append(std::wstring(L"All Files\0*.*\0\0", 15));
ofn.lpstrFilter = filter.c_str();
ofn.lpstrDefExt = defExt.empty() ? nullptr : defExt.c_str();
@@ -119,6 +130,88 @@ bool FlutterWindow::OnCreate() {
fwrite(data->data(), sizeof(uint8_t), data->size(), f);
fclose(f);
result->Success();
} else if (call.method_name() == "closeFile") {
auto args = std::get_if<flutter::EncodableList>(call.arguments());
auto fd = std::get_if<int>(&args->at(0));
if (!fd) {
result->Error("INVALID_ARGUMENT", "Invalid arguments.");
return;
}
fileop::close(*fd);
result->Success();
} else if (call.method_name() == "openFile") {
auto args = std::get_if<flutter::EncodableList>(call.arguments());
auto fileName = std::get_if<std::string>(&args->at(0));
auto dir = std::get_if<std::string>(&args->at(1));
auto mimeType = std::get_if<std::string>(&args->at(2));
if (!fileName || !dir || !mimeType) {
result->Error("INVALID_ARGUMENT", "Invalid arguments.");
return;
}
std::wstring wFileName;
if (!wchar_util::str_to_wstr(wFileName, *fileName, CP_UTF8)) {
result->Error("ERROR", "Failed to convert file name to wstring.");
return;
}
std::wstring wDir;
if (!dir->empty() && !wchar_util::str_to_wstr(wDir, *dir, CP_UTF8)) {
result->Error("ERROR", "Failed to convert dir to wstring.");
return;
}
OPENFILENAMEW ofn;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = Win32Window::GetHandle();
std::wstring filter;
std::wstring defExt;
updateDataFromMimeType(defExt, filter, *mimeType);
filter.append(std::wstring(L"All Files\0*.*\0\0", 15));
ofn.lpstrFilter = filter.c_str();
ofn.lpstrDefExt = defExt.empty() ? nullptr : defExt.c_str();
wchar_t wFileNameBuf[MAX_PATH_SIZE];
memcpy(wFileNameBuf, wFileName.c_str(), (wFileName.size() + 1) * sizeof(wchar_t));
ofn.lpstrFile = wFileNameBuf;
ofn.nMaxFile = MAX_PATH_SIZE;
ofn.lpstrInitialDir = wDir.empty() ? nullptr : wDir.c_str();
ofn.Flags = OFN_DONTADDTORECENT | OFN_NONETWORKBUTTON | OFN_NOREADONLYRETURN | OFN_OVERWRITEPROMPT;
if (!GetSaveFileNameW(&ofn)) {
result->Error("ERROR", "Failed to get file name.");
return;
}
std::string fn;
if (!wchar_util::wstr_to_str(fn, wFileNameBuf, CP_UTF8)) {
result->Error("ERROR", "Failed to convert file name to UTF-8.");
return;
}
int fd = 0;
int e = fileop::open(fn, fd, _O_WRONLY | _O_BINARY | O_CREAT);
if (e) {
std::string errmsg;
if (!err::get_errno_message(errmsg, e)) {
errmsg = "Unknown error.";
}
result->Error("ERROR", "Failed to open file: " + errmsg);
return;
}
result->Success(fd);
} else if (call.method_name() == "writeFile") {
auto args = std::get_if<flutter::EncodableList>(call.arguments());
auto fd = std::get_if<int>(&args->at(0));
auto data = std::get_if<std::vector<uint8_t>>(&args->at(1));
if (!fd || !data) {
result->Error("INVALID_ARGUMENT", "Invalid arguments.");
return;
}
int num = _write(*fd, data->data(), (unsigned int)data->size());
if (num == -1) {
std::string errmsg;
if (!err::get_errno_message(errmsg, errno)) {
errmsg = "Unknown error.";
}
result->Error("ERROR", "Failed to write file:" + errmsg);
return;
}
result->Success(num);
} else {
result->NotImplemented();
}