Skip to content

Commit d9bb08d

Browse files
committed
CLI: return non-zero result code when upload failed
Set expiration for anonymous upload on imgbb.com #366
1 parent 71dd469 commit d9bb08d

File tree

7 files changed

+48
-15
lines changed

7 files changed

+48
-15
lines changed

Data/Scripts/Lang/ru.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
"verification": "Введите код подтверждения:"
4141
},
4242
"imgbb": {
43-
"expiration": "Автоудаление (в минутах)"
43+
"expiration": "Автоудаление через (анонимно)",
44+
"expiration_authorized": "Автоудаление в минутах (с аккаунтом)",
45+
"never": "Никогда"
4446
}
4547
}

Data/Scripts/imgbb.nut

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function _UploadToAccount(FileName, options) {
2323
}
2424
local expiration = 0;
2525
try {
26-
expiration = 60 * ServerParams.getParam("uploadExpiration").tointeger();
26+
expiration = 60 * ServerParams.getParam("expirationAuthorized").tointeger();
2727
} catch (ex) {
2828

2929
}
@@ -62,6 +62,7 @@ function UploadFile(FileName, options) {
6262
local name = ExtractFileName(FileName);
6363
local mime = GetFileMimeType(name);
6464
local token = _ObtainToken();
65+
local expiration = ServerParams.getParam("expiration");
6566
if (token == "") {
6667
WriteLog("error", "[imgbb.com] Unable to obtain auth token");
6768

@@ -74,6 +75,10 @@ function UploadFile(FileName, options) {
7475
nm.addQueryParam("timestamp", time() + "000");
7576
nm.addQueryParam("auth_token", token);
7677
nm.addQueryParam("category_id", "");
78+
if (expiration != "") {
79+
nm.addQueryParam("expiration", expiration);
80+
}
81+
7782
nm.addQueryParam("nswd", "");
7883
nm.addQueryParamFile("source", FileName, name, mime);
7984
nm.doUploadMultipartData();
@@ -102,14 +107,14 @@ function UploadFile(FileName, options) {
102107

103108
function GetServerParamList() {
104109
return {
105-
uploadExpiration = tr("imgbb.expiration", "Expiration (in minutes)")
106-
/*uploadExpiration = {
107-
title = "Auto-delete after",
110+
expirationAuthorized = tr("imgbb.expiration_authorized", "Expiration in minutes (authorized)"),
111+
expiration = {
112+
title = tr("imgbb.expiration", "Expiration (anonymous)"),
108113
type = "choice",
109114
items = [
110115
{
111116
id = "",
112-
label = "Never"
117+
label = tr("imgbb.never", "Never")
113118
},
114119
{
115120
id = "PT5M",
@@ -196,6 +201,6 @@ function GetServerParamList() {
196201
label = "6 months"
197202
}
198203
]
199-
}*/
204+
}
200205
};
201206
}

Source/CLI/main.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ std::shared_ptr<UploadSession> session;
104104
std::mutex finishSignalMutex;
105105
std::condition_variable finishSignal;
106106
bool finished = false;
107-
107+
int funcResult = 0;
108108
struct TaskUserData {
109109
int index;
110110
};
@@ -175,11 +175,13 @@ void OnUploadSessionFinished(UploadSession* session) {
175175
for (int i = 0; i < taskCount; i++) {
176176
auto task = session->getTask(i);
177177
UploadResult* res = task->uploadResult();
178-
auto* fileTask = dynamic_cast<FileUploadTask*>(task.get());
178+
//auto* fileTask = dynamic_cast<FileUploadTask*>(task.get());
179179
if ( task->uploadSuccess() ) {
180180
OutputGenerator::UploadObject uo;
181181
uo.fillFromUploadResult(res, task.get());
182182
uploadedList.push_back(uo);
183+
} else {
184+
funcResult++;
183185
}
184186
}
185187
OutputGenerator::OutputGeneratorFactory factory;
@@ -287,7 +289,6 @@ int func() {
287289
#ifdef _WIN32
288290
GdiPlusInitializer gdiPlusInitializer;
289291
#endif
290-
int res = 0;
291292
auto uploadErrorHandler = std::make_shared<ConsoleUploadErrorHandler>();
292293
ServiceLocator* serviceLocator = ServiceLocator::instance();
293294
serviceLocator->setUploadErrorHandler(uploadErrorHandler);
@@ -366,7 +367,7 @@ int func() {
366367
{
367368
std::string errorMessage = str(boost::format("File '%s' doesn't exist!")%filesToUpload[i]);
368369
ConsoleUtils::instance()->printUnicode(stderr, errorMessage);
369-
res++;
370+
funcResult++;
370371
continue;
371372
}
372373

@@ -393,7 +394,7 @@ int func() {
393394
while (!finished) {
394395
finishSignal.wait(lk/*, [] {return finished;}*/);
395396
}
396-
return res;
397+
return funcResult;
397398
}
398399

399400

@@ -426,6 +427,12 @@ void PrintServerParamList()
426427
for (auto& parameter : parameterList) {
427428
std::cout << ++i << ") " << parameter->getTitle() << std::endl
428429
<< "Name: " << parameter->getName() << " Type: " << parameter->getType() << std::endl;
430+
431+
std::string description = parameter->getDescription();
432+
433+
if (!description.empty()) {
434+
std::cout << description << std::endl;
435+
}
429436
}
430437
} else {
431438
throw std::invalid_argument("This server cannot have parameters");
@@ -529,7 +536,7 @@ int main(int argc, char *argv[]){
529536
.store_into(serverName);
530537

531538
program.add_argument("-l", "--list")
532-
.help("Print server list (hostings) and exit")
539+
.help("Prints server list (hosting services) and exits")
533540
.action([=](const auto& s) {
534541
PrintServerList();
535542
std::exit(0);
@@ -659,11 +666,12 @@ int main(int argc, char *argv[]){
659666
.default_value("http")
660667
.nargs(1);
661668

669+
#ifdef _WIN32
662670
program.add_argument("-ps", "--proxy_system")
663-
.help("Use system proxy settings (this option supported only on Windows)")
671+
.help("Use system proxy settings (this option is supported only on Windows)")
664672
.flag()
665673
.store_into(useSystemProxy);
666-
#ifdef _WIN32
674+
667675
program.add_argument("-up", "--update")
668676
.help("Update servers.xml. The 'Data' directory must be writable, otherwise update will fail.")
669677
.action([=](const auto& s) {

Source/Core/Upload/Parameters/AbstractParameter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ void AbstractParameter::setTitle(const std::string title) {
1717
std::string AbstractParameter::getTitle() const {
1818
return title_;
1919
}
20+
21+
std::string AbstractParameter::getDescription() const {
22+
return {};
23+
}

Source/Core/Upload/Parameters/AbstractParameter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class AbstractParameter {
1717
virtual void setValue(const std::string& val) = 0;
1818
virtual std::string getValueAsString() const = 0;
1919

20+
virtual std::string getDescription() const;
21+
2022
private:
2123
std::string name_, title_;
2224
};

Source/Core/Upload/Parameters/ChoiceParameter.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,13 @@ void ChoiceParameter::setSelectedIndex(int val) {
5151
int ChoiceParameter::selectedIndex() const {
5252
return selectedIndex_;
5353
}
54+
55+
std::string ChoiceParameter::getDescription() const {
56+
std::string res = "Possible values:\n";
57+
58+
for (const auto& it : items_) {
59+
res += " \"" + it.first + "\" (" + it.second + ")\n";
60+
}
61+
62+
return res;
63+
}

Source/Core/Upload/Parameters/ChoiceParameter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class ChoiceParameter: public AbstractParameter {
1717
void setSelectedIndex(int val);
1818
int selectedIndex() const;
1919

20+
std::string getDescription() const override;
21+
2022
private:
2123
std::vector<std::pair<std::string, std::string>> items_;
2224
int selectedIndex_ = -1;

0 commit comments

Comments
 (0)