6 #include "core/fileupload.hpp"
7 #include "library/string.hpp"
8 #include <boost/iostreams/categories.hpp>
9 #include <boost/iostreams/copy.hpp>
10 #include <boost/iostreams/stream.hpp>
11 #include <boost/iostreams/stream_buffer.hpp>
12 #include <boost/iostreams/filter/symmetric.hpp>
13 #include <boost/iostreams/filter/zlib.hpp>
14 #include <boost/iostreams/filtering_stream.hpp>
15 #include <boost/iostreams/device/back_inserter.hpp>
22 std::vector
<char> load_vec(const std::string
& fn
)
24 std::vector
<char> out
;
27 throw std::runtime_error("Can't open file '" + fn
+ "'");
28 boost::iostreams::back_insert_device
<std::vector
<char>> rd(out
);
29 boost::iostreams::copy(f
, rd
);
33 std::string
load_str(const std::string
& fn
)
35 std::vector
<char> out
= load_vec(fn
);
36 return std::string(&out
[0], out
.size());
40 int main(int argc
, char** argv
)
45 upload
.hidden
= false;
46 for(int i
= 1; i
< argc
; i
++) {
47 std::string arg
= argv
[i
];
49 if(arg
== "--show-pubkey") {
51 get_dh25519_pubkey(key
);
54 for(unsigned i
= 0; i
< 32; i
++)
55 sprintf(out
+ 2 * i
, "%02x", key
[i
]);
56 std::cout
<< out
<< std::endl
;
58 } else if(r
= regex("--url=(.+)", arg
)) {
59 upload
.base_url
= r
[1];
60 } else if(r
= regex("--filename=(.+)", arg
)) {
61 upload
.filename
= r
[1];
62 } else if(r
= regex("--title=(.*)", arg
)) {
64 } else if(r
= regex("--description-file=(.+)", arg
)) {
65 upload
.description
= load_str(r
[1]);
66 } else if(r
= regex("--gamename=(.+)", arg
)) {
67 upload
.gamename
= r
[1];
68 } else if(r
= regex("--hidden", arg
)) {
70 } else if(r
= regex("([^-].*)", arg
)) {
73 throw std::runtime_error("Unrecognized argument '" + arg
+ "'");
75 if(upload
.base_url
== "")
76 throw std::runtime_error("--url=foo is needed");
78 throw std::runtime_error("File to upload is needed");
79 if(upload
.filename
== "") {
80 upload
.filename
= mainfile
;
81 auto r
= regex(".*/([^/]+)", upload
.filename
);
82 if(r
) upload
.filename
= r
[1];
84 upload
.content
= load_vec(mainfile
);
87 int last_progress
= -1;
88 while(!upload
.finished
) {
90 for(auto i
: upload
.get_messages())
91 std::cout
<< i
<< std::endl
;
92 auto progress
= upload
.get_progress_ppm();
93 if(progress
>= 0 && progress
!= last_progress
) {
94 std::cout
<< "Upload: " << (double)progress
/ 10000 << "%" << std::endl
;
95 last_progress
= progress
;
98 for(auto i
: upload
.get_messages())
99 std::cout
<< i
<< std::endl
;
100 return upload
.success
? 0 : 1;
101 } catch(std::exception
& e
) {
102 std::cerr
<< "Error: " << e
.what() << std::endl
;