Skip to content

Commit

Permalink
CLI: return non-zero result code when upload failed
Browse files Browse the repository at this point in the history
Set expiration for anonymous upload on imgbb.com #366
  • Loading branch information
zenden2k committed Nov 4, 2024
1 parent 71dd469 commit d9bb08d
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 15 deletions.
4 changes: 3 additions & 1 deletion Data/Scripts/Lang/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
"verification": "Введите код подтверждения:"
},
"imgbb": {
"expiration": "Автоудаление (в минутах)"
"expiration": "Автоудаление через (анонимно)",
"expiration_authorized": "Автоудаление в минутах (с аккаунтом)",
"never": "Никогда"
}
}
17 changes: 11 additions & 6 deletions Data/Scripts/imgbb.nut
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function _UploadToAccount(FileName, options) {
}
local expiration = 0;
try {
expiration = 60 * ServerParams.getParam("uploadExpiration").tointeger();
expiration = 60 * ServerParams.getParam("expirationAuthorized").tointeger();
} catch (ex) {

}
Expand Down Expand Up @@ -62,6 +62,7 @@ function UploadFile(FileName, options) {
local name = ExtractFileName(FileName);
local mime = GetFileMimeType(name);
local token = _ObtainToken();
local expiration = ServerParams.getParam("expiration");
if (token == "") {
WriteLog("error", "[imgbb.com] Unable to obtain auth token");

Expand All @@ -74,6 +75,10 @@ function UploadFile(FileName, options) {
nm.addQueryParam("timestamp", time() + "000");
nm.addQueryParam("auth_token", token);
nm.addQueryParam("category_id", "");
if (expiration != "") {
nm.addQueryParam("expiration", expiration);
}

nm.addQueryParam("nswd", "");
nm.addQueryParamFile("source", FileName, name, mime);
nm.doUploadMultipartData();
Expand Down Expand Up @@ -102,14 +107,14 @@ function UploadFile(FileName, options) {

function GetServerParamList() {
return {
uploadExpiration = tr("imgbb.expiration", "Expiration (in minutes)")
/*uploadExpiration = {
title = "Auto-delete after",
expirationAuthorized = tr("imgbb.expiration_authorized", "Expiration in minutes (authorized)"),
expiration = {
title = tr("imgbb.expiration", "Expiration (anonymous)"),
type = "choice",
items = [
{
id = "",
label = "Never"
label = tr("imgbb.never", "Never")
},
{
id = "PT5M",
Expand Down Expand Up @@ -196,6 +201,6 @@ function GetServerParamList() {
label = "6 months"
}
]
}*/
}
};
}
24 changes: 16 additions & 8 deletions Source/CLI/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ std::shared_ptr<UploadSession> session;
std::mutex finishSignalMutex;
std::condition_variable finishSignal;
bool finished = false;

int funcResult = 0;
struct TaskUserData {
int index;
};
Expand Down Expand Up @@ -175,11 +175,13 @@ void OnUploadSessionFinished(UploadSession* session) {
for (int i = 0; i < taskCount; i++) {
auto task = session->getTask(i);
UploadResult* res = task->uploadResult();
auto* fileTask = dynamic_cast<FileUploadTask*>(task.get());
//auto* fileTask = dynamic_cast<FileUploadTask*>(task.get());
if ( task->uploadSuccess() ) {
OutputGenerator::UploadObject uo;
uo.fillFromUploadResult(res, task.get());
uploadedList.push_back(uo);
} else {
funcResult++;
}
}
OutputGenerator::OutputGeneratorFactory factory;
Expand Down Expand Up @@ -287,7 +289,6 @@ int func() {
#ifdef _WIN32
GdiPlusInitializer gdiPlusInitializer;
#endif
int res = 0;
auto uploadErrorHandler = std::make_shared<ConsoleUploadErrorHandler>();
ServiceLocator* serviceLocator = ServiceLocator::instance();
serviceLocator->setUploadErrorHandler(uploadErrorHandler);
Expand Down Expand Up @@ -366,7 +367,7 @@ int func() {
{
std::string errorMessage = str(boost::format("File '%s' doesn't exist!")%filesToUpload[i]);
ConsoleUtils::instance()->printUnicode(stderr, errorMessage);
res++;
funcResult++;
continue;
}

Expand All @@ -393,7 +394,7 @@ int func() {
while (!finished) {
finishSignal.wait(lk/*, [] {return finished;}*/);
}
return res;
return funcResult;
}


Expand Down Expand Up @@ -426,6 +427,12 @@ void PrintServerParamList()
for (auto& parameter : parameterList) {
std::cout << ++i << ") " << parameter->getTitle() << std::endl
<< "Name: " << parameter->getName() << " Type: " << parameter->getType() << std::endl;

std::string description = parameter->getDescription();

if (!description.empty()) {
std::cout << description << std::endl;
}
}
} else {
throw std::invalid_argument("This server cannot have parameters");
Expand Down Expand Up @@ -529,7 +536,7 @@ int main(int argc, char *argv[]){
.store_into(serverName);

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

#ifdef _WIN32
program.add_argument("-ps", "--proxy_system")
.help("Use system proxy settings (this option supported only on Windows)")
.help("Use system proxy settings (this option is supported only on Windows)")
.flag()
.store_into(useSystemProxy);
#ifdef _WIN32

program.add_argument("-up", "--update")
.help("Update servers.xml. The 'Data' directory must be writable, otherwise update will fail.")
.action([=](const auto& s) {
Expand Down
4 changes: 4 additions & 0 deletions Source/Core/Upload/Parameters/AbstractParameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ void AbstractParameter::setTitle(const std::string title) {
std::string AbstractParameter::getTitle() const {
return title_;
}

std::string AbstractParameter::getDescription() const {
return {};
}
2 changes: 2 additions & 0 deletions Source/Core/Upload/Parameters/AbstractParameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class AbstractParameter {
virtual void setValue(const std::string& val) = 0;
virtual std::string getValueAsString() const = 0;

virtual std::string getDescription() const;

private:
std::string name_, title_;
};
Expand Down
10 changes: 10 additions & 0 deletions Source/Core/Upload/Parameters/ChoiceParameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,13 @@ void ChoiceParameter::setSelectedIndex(int val) {
int ChoiceParameter::selectedIndex() const {
return selectedIndex_;
}

std::string ChoiceParameter::getDescription() const {
std::string res = "Possible values:\n";

for (const auto& it : items_) {
res += " \"" + it.first + "\" (" + it.second + ")\n";
}

return res;
}
2 changes: 2 additions & 0 deletions Source/Core/Upload/Parameters/ChoiceParameter.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class ChoiceParameter: public AbstractParameter {
void setSelectedIndex(int val);
int selectedIndex() const;

std::string getDescription() const override;

private:
std::vector<std::pair<std::string, std::string>> items_;
int selectedIndex_ = -1;
Expand Down

0 comments on commit d9bb08d

Please sign in to comment.