-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathUploads.cpp
More file actions
107 lines (89 loc) · 2.25 KB
/
Uploads.cpp
File metadata and controls
107 lines (89 loc) · 2.25 KB
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
/**
* Copyright (C) 2012-2013 Allan SIMON (Sysko) <allan.simon@supinfo.com>
* See accompanying file COPYING.TXT file for licensing details.
*
* @category Cppcms-skeleton
* @author Allan SIMON <allan.simon@supinfo.com>
* @package Models
*
*/
#include <cppdb/frontend.h>
#include <cppcms/util.h>
#include <cppcms/cppcms_error.h>
#include <cppcms/http_file.h>
#include <booster/posix_time.h>
#include <booster/log.h>
#include <cppcms_skel/generics/Config.h>
#include "models/Uploads.h"
namespace cppcmsskel {
namespace models {
/**
*
*/
Uploads::Uploads() :
SqliteModel()
{
}
/**
*
*/
std::string Uploads::save(
booster::shared_ptr<cppcms::http::file> file
) {
std::string filename = file->filename();
try {
//TODO if we keep the same name, we should then check that there's
// not already a file with the same name
file->write_data();
file->save_to(
(Config::get_upload_folder() + filename).c_str()
);
} catch (cppcms::cppcms_error const &e) {
BOOSTER_ERROR("cppcms_skel") << e.what();
return UPLOADS_SAVE_ON_DISK_ERROR;
}
cppdb::statement saveFile = sqliteDb.prepare(
"INSERT INTO uploads(filename, created) "
"VALUES(?,?)"
);
saveFile.bind(filename);
saveFile.bind(
booster::ptime::now().get_seconds()
);
if (!execute_simple(saveFile)) {
return UPLOADS_SAVE_ON_DB_ERROR;
}
return Config::get_upload_url() + filename;
}
/**
*
*/
results::Files Uploads::list(
const unsigned limit,
const unsigned page
) {
results::Files files;
cppdb::statement listFiles = sqliteDb.prepare(
"SELECT "
" filename, "
" created "
"FROM uploads "
"LIMIT ? OFFSET ? "
);
listFiles.bind(limit);
listFiles.bind(page*limit);
std::string baseURL = Config::get_upload_url();
cppdb::result res = listFiles.query();
while (res.next()) {
std::string name = res.get<std::string>("filename");
files.push_back(results::File(
name,
baseURL + name,
res.get<unsigned>("created")
));
}
listFiles.reset();
return files;
}
} // end namespace models
} // end namespace cppcmsskel