File: api_Attachment.cc

package info (click to toggle)
drogon 1.9.11%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,820 kB
  • sloc: cpp: 57,270; sh: 297; xml: 20; makefile: 11
file content (112 lines) | stat: -rw-r--r-- 3,438 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "api_Attachment.h"
#include <fstream>

using namespace api;

// add definition of your processing function here
void Attachment::get(const HttpRequestPtr &req,
                     std::function<void(const HttpResponsePtr &)> &&callback)
{
    auto resp = HttpResponse::newHttpViewResponse("FileUpload", HttpViewData());
    callback(resp);
}

void Attachment::upload(const HttpRequestPtr &req,
                        std::function<void(const HttpResponsePtr &)> &&callback)
{
    MultiPartParser fileUpload;
    if (fileUpload.parse(req) == 0)
    {
        // LOG_DEBUG << "upload good!";
        auto &files = fileUpload.getFiles();
        // LOG_DEBUG << "file num=" << files.size();
        for (auto const &file : files)
        {
            LOG_DEBUG << "file:" << file.getFileName()
                      << "(extension=" << file.getFileExtension()
                      << ",type=" << file.getFileType()
                      << ",len=" << file.fileLength()
                      << ",md5=" << file.getMd5() << ")";
            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";
        for (auto &param : fileUpload.getParameters())
        {
            json[param.first] = param.second;
        }
        auto resp = HttpResponse::newHttpJsonResponse(json);
        callback(resp);
        return;
    }
    LOG_DEBUG << "upload error!";
    // LOG_DEBUG << req->con
    Json::Value json;
    json["result"] = "failed";
    auto resp = HttpResponse::newHttpJsonResponse(json);
    callback(resp);
}

void Attachment::uploadImage(
    const HttpRequestPtr &req,
    std::function<void(const HttpResponsePtr &)> &&callback)
{
    MultiPartParser fileUpload;

    // At this endpoint, we only accept one file
    if (fileUpload.parse(req) == 0 && fileUpload.getFiles().size() == 1)
    {
        // LOG_DEBUG << "upload image good!";
        Json::Value json;

        // Get the first file received
        auto &file = fileUpload.getFiles()[0];

        // There are 2 ways to check if the file extension is an image.
        // First way
        if (file.getFileType() == FT_IMAGE)
        {
            json["isImage"] = true;
        }
        // Second way
        auto fileExtension = file.getFileExtension();
        if (fileExtension == "png" || fileExtension == "jpeg" ||
            fileExtension == "jpg" || fileExtension == "ico" /* || etc... */)
        {
            json["isImage"] = true;
        }
        else
        {
            json["isImage"] = false;
        }

        json["result"] = "ok";
        for (auto &param : fileUpload.getParameters())
        {
            json[param.first] = param.second;
        }
        auto resp = HttpResponse::newHttpJsonResponse(json);
        callback(resp);
        return;
    }
    LOG_DEBUG << "upload image error!";
    // LOG_DEBUG << req->con
    Json::Value json;
    json["result"] = "failed";
    auto resp = HttpResponse::newHttpJsonResponse(json);
    callback(resp);
}

void Attachment::download(
    const HttpRequestPtr &req,
    std::function<void(const HttpResponsePtr &)> &&callback)
{
    auto resp = HttpResponse::newFileResponse("./drogon.jpg", "", CT_IMAGE_JPG);
    callback(resp);
}