2018-09-07 01:53:37 +00:00
|
|
|
#include "api_Attachment.h"
|
2019-01-28 10:25:04 +00:00
|
|
|
#include <fstream>
|
|
|
|
|
2018-09-07 01:53:37 +00:00
|
|
|
using namespace api;
|
2019-05-18 12:39:57 +00:00
|
|
|
// add definition of your processing function here
|
2018-10-14 07:56:54 +00:00
|
|
|
void Attachment::get(const HttpRequestPtr &req,
|
2019-05-09 03:25:25 +00:00
|
|
|
std::function<void(const HttpResponsePtr &)> &&callback)
|
2018-09-07 01:53:37 +00:00
|
|
|
{
|
2018-10-14 07:56:54 +00:00
|
|
|
auto resp = HttpResponse::newHttpViewResponse("FileUpload", HttpViewData());
|
2018-09-07 01:53:37 +00:00
|
|
|
callback(resp);
|
|
|
|
}
|
|
|
|
|
2018-10-14 07:56:54 +00:00
|
|
|
void Attachment::upload(const HttpRequestPtr &req,
|
2019-05-09 03:25:25 +00:00
|
|
|
std::function<void(const HttpResponsePtr &)> &&callback)
|
2018-09-07 01:53:37 +00:00
|
|
|
{
|
2019-01-27 07:59:56 +00:00
|
|
|
MultiPartParser fileUpload;
|
2019-01-28 10:25:04 +00:00
|
|
|
if (fileUpload.parse(req) == 0)
|
2018-09-07 01:53:37 +00:00
|
|
|
{
|
2019-05-18 12:39:57 +00:00
|
|
|
// LOG_DEBUG << "upload good!";
|
2020-06-10 03:11:24 +00:00
|
|
|
auto &files = fileUpload.getFiles();
|
2019-05-18 12:39:57 +00:00
|
|
|
// LOG_DEBUG << "file num=" << files.size();
|
2019-01-27 07:59:56 +00:00
|
|
|
for (auto const &file : files)
|
|
|
|
{
|
2019-05-18 12:39:57 +00:00
|
|
|
LOG_DEBUG << "file:" << file.getFileName()
|
|
|
|
<< "(len=" << file.fileLength()
|
|
|
|
<< ",md5=" << file.getMd5() << ")";
|
2019-01-27 07:59:56 +00:00
|
|
|
file.save();
|
|
|
|
file.save("123");
|
|
|
|
file.saveAs("456/hehe");
|
|
|
|
file.saveAs("456/7/8/9/" + file.getMd5());
|
|
|
|
file.save("..");
|
|
|
|
file.save(".xx");
|
|
|
|
file.saveAs("../xxx");
|
|
|
|
}
|
|
|
|
Json::Value json;
|
|
|
|
json["result"] = "ok";
|
2019-01-29 09:38:55 +00:00
|
|
|
for (auto ¶m : fileUpload.getParameters())
|
|
|
|
{
|
|
|
|
json[param.first] = param.second;
|
|
|
|
}
|
2019-01-27 07:59:56 +00:00
|
|
|
auto resp = HttpResponse::newHttpJsonResponse(json);
|
|
|
|
callback(resp);
|
|
|
|
return;
|
2018-09-07 01:53:37 +00:00
|
|
|
}
|
2019-01-29 09:38:55 +00:00
|
|
|
LOG_DEBUG << "upload error!";
|
2019-05-18 12:39:57 +00:00
|
|
|
// LOG_DEBUG<<req->con
|
2019-01-27 07:59:56 +00:00
|
|
|
Json::Value json;
|
|
|
|
json["result"] = "failed";
|
|
|
|
auto resp = HttpResponse::newHttpJsonResponse(json);
|
2018-09-07 01:53:37 +00:00
|
|
|
callback(resp);
|
2018-11-16 05:26:14 +00:00
|
|
|
}
|
2019-01-28 10:25:04 +00:00
|
|
|
|
2019-05-18 12:39:57 +00:00
|
|
|
void Attachment::download(
|
|
|
|
const HttpRequestPtr &req,
|
|
|
|
std::function<void(const HttpResponsePtr &)> &&callback)
|
2019-01-28 10:25:04 +00:00
|
|
|
{
|
2019-01-29 02:49:37 +00:00
|
|
|
auto resp = HttpResponse::newFileResponse("./drogon.jpg", "", CT_IMAGE_JPG);
|
2019-01-28 10:25:04 +00:00
|
|
|
callback(resp);
|
|
|
|
}
|