Skip to content

Commit

Permalink
Generate JSON in CLI #366
Browse files Browse the repository at this point in the history
  • Loading branch information
zenden2k committed Feb 18, 2024
1 parent 5fa95b5 commit 40679ad
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
5 changes: 4 additions & 1 deletion Source/CLI/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ void PrintHelp() {
std::cerr<<" -s <server_name>"<<std::endl;
std::cerr<<" -u <username>"<<std::endl;
std::cerr<<" -p <password>"<<std::endl;
std::cerr<<" -cl <bbcode|html|plain> (Default: plain)"<<std::endl;
std::cerr<<" -cl <bbcode|html|json|plain> (Default: plain)"<<std::endl;
std::cerr<<" -ct <TableOfThumbnails|ClickableThumbnails|Images|Links>"<<std::endl;
std::cerr<<" -fl <folder_id> ID of remote folder (supported by some servers)\r\n"
<< " Note that this is not the folder\'s name! \r\n"
Expand Down Expand Up @@ -230,6 +230,8 @@ bool parseCommandLine(int argc, char *argv[])
codeLang = OutputCodeGenerator::clHTML;
else if(!IuStringUtils::stricmp(codelang, "bbcode"))
codeLang = OutputCodeGenerator::clBBCode;
else if (!IuStringUtils::stricmp(codelang, "json"))
codeLang = OutputCodeGenerator::clJSON;
i++;
continue;
}
Expand Down Expand Up @@ -390,6 +392,7 @@ void OnUploadSessionFinished(UploadSession* session) {
uo.directUrl = res->directUrl;
uo.thumbUrl = res->thumbUrl;
uo.viewUrl = res->downloadUrl;
uo.deleteUrl = res->deleteUrl;
uo.serverName = task->serverName();
if ( fileTask ) {
uo.localFilePath = fileTask->getFileName();
Expand Down
41 changes: 34 additions & 7 deletions Source/Core/OutputCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
*/

#include "OutputCodeGenerator.h"

#include <json/value.h>
#include <json/writer.h>
#include "Utils/CoreUtils.h"


OutputCodeGenerator::OutputCodeGenerator()
{
m_PreferDirectLinks = true;
Expand All @@ -40,13 +44,36 @@ void OutputCodeGenerator::setType(CodeType type)

std::string OutputCodeGenerator::generate(const std::vector<UploadObject>& items)
{
std::string result;
for(size_t i=0; i < items.size(); i++)
{
if(i) result += "\r\n";
result += generateCodeForItem(items[i], i);
}
return result;
if (m_lang == clJSON) {
return generateJson(items);
} else {
std::string result;
for (size_t i = 0; i < items.size(); i++)
{
if (i) result += "\r\n";
result += generateCodeForItem(items[i], i);
}
return result;
}
}

std::string OutputCodeGenerator::generateJson(const std::vector<UploadObject>& items) {
Json::Value arrValue(Json::arrayValue);

for (const auto& item: items) {
Json::Value objValue(Json::objectValue);
objValue["direct_url"] = item.directUrl;
objValue["thumb_url"] = item.thumbUrl;
objValue["view_url"] = item.viewUrl;
objValue["delete_url"] = item.deleteUrl;
objValue["filename"] = item.displayFileName;
arrValue.append(objValue);
}

Json::StreamWriterBuilder builder;
builder["commentStyle"] = "None";
builder["indentation"] = " ";
return Json::writeString(builder, arrValue);
}

std::string OutputCodeGenerator::generateCodeForItem(const UploadObject& item, int index)
Expand Down
20 changes: 11 additions & 9 deletions Source/Core/OutputCodeGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@

struct UploadObject
{
std::string localFilePath;
std::string directUrl;
std::string viewUrl;
std::string thumbUrl;
std::string serverName;
std::string displayFileName;
time_t timeStamp;
int64_t uploadFileSize;
std::string localFilePath;
std::string directUrl;
std::string viewUrl;
std::string thumbUrl;
std::string deleteUrl;
std::string serverName;
std::string displayFileName;
time_t timeStamp;
int64_t uploadFileSize;
};

class OutputCodeGenerator
{
public:
enum CodeLang {clHTML, clBBCode, clPlain};
enum CodeLang {clHTML, clBBCode, clPlain, clJSON};
enum CodeType {ctTableOfThumbnails, ctClickableThumbnails, ctImages, ctLinks};
OutputCodeGenerator();
void setLang(CodeLang lang);
Expand All @@ -30,6 +31,7 @@ class OutputCodeGenerator
std::string generateCodeForItem(const UploadObject& item, int index);
std::string image(const std::string& url);
std::string link(const std::string &url, const std::string &body);
std::string generateJson(const std::vector<UploadObject>& items);
CodeLang m_lang;
CodeType m_CodeType;
bool m_PreferDirectLinks;
Expand Down

0 comments on commit 40679ad

Please sign in to comment.