diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-04-30 21:04:47 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-04-30 21:18:06 +0200 |
commit | 50afa93c577bed3eb9911d141d46f2e0d3ba2572 (patch) | |
tree | f910aa9617bca27435325fb72eec36d8e9aea808 /src/plugins | |
parent | 47bb22a4aee886deb54ca432bdb14747bf2e9160 (diff) |
Add 4chan file upload
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/FileManager.cpp | 72 | ||||
-rw-r--r-- | src/plugins/Fourchan.cpp | 45 | ||||
-rw-r--r-- | src/plugins/ImageBoard.cpp | 15 |
3 files changed, 94 insertions, 38 deletions
diff --git a/src/plugins/FileManager.cpp b/src/plugins/FileManager.cpp index f15deae..814ee70 100644 --- a/src/plugins/FileManager.cpp +++ b/src/plugins/FileManager.cpp @@ -14,12 +14,31 @@ namespace QuickMedia { return ""; } - static std::filesystem::file_time_type file_get_filetime_or(const std::filesystem::directory_entry &path, std::filesystem::file_time_type default_value) { - try { - return path.last_write_time(); - } catch(const std::filesystem::filesystem_error &err) { + static std::filesystem::file_time_type file_get_last_modified_time(const std::filesystem::directory_entry &path, std::filesystem::file_time_type default_value) { + std::error_code err; + auto last_write_time = path.last_write_time(err); + if(err) return default_value; - } + else + return last_write_time; + } + + static std::string file_size_to_human_readable_string(size_t bytes) { + double kb = (double)bytes / 1024.0; + double mb = (double)bytes / 1024.0 / 1024.0; + double gb = (double)bytes / 1024.0 / 1024.0 / 1024.0; + char result[32]; + + if(gb >= 1.0) + snprintf(result, sizeof(result), "%.2f GiB", gb); + else if(mb >= 1.0) + snprintf(result, sizeof(result), "%.2f MiB", mb); + else if(kb >= 1.0) + snprintf(result, sizeof(result), "%.2f KiB", kb); + else + snprintf(result, sizeof(result), "%zu bytes", bytes); + + return result; } PluginResult FileManagerPage::submit(const std::string &title, const std::string &url, std::vector<Tab> &result_tabs) { @@ -71,7 +90,7 @@ namespace QuickMedia { } std::sort(paths.begin(), paths.end(), [](const std::filesystem::directory_entry &path1, std::filesystem::directory_entry &path2) { - return file_get_filetime_or(path1, std::filesystem::file_time_type::min()) > file_get_filetime_or(path2, std::filesystem::file_time_type::min()); + return file_get_last_modified_time(path1, std::filesystem::file_time_type::min()) > file_get_last_modified_time(path2, std::filesystem::file_time_type::min()); }); if(current_dir != "/") { @@ -79,14 +98,51 @@ namespace QuickMedia { result_items.push_back(std::move(parent_item)); } + char time_str[128] = {0}; for(auto &p : paths) { - auto body_item = BodyItem::create(p.path().filename().string()); + std::error_code regular_file_err; + bool is_regular_file = p.is_regular_file(regular_file_err); + if(regular_file_err) + is_regular_file = true; + // TODO: Check file magic number instead of extension? const char *ext = get_ext(p.path()); - if(p.is_regular_file() && (is_image_ext(ext) || is_video_ext(ext))) { + FileManagerMimeType file_mime_type = FILE_MANAGER_MIME_TYPE_OTHER; + if(is_regular_file) { + if(is_image_ext(ext)) + file_mime_type = FILE_MANAGER_MIME_TYPE_IMAGE; + else if(is_video_ext(ext)) + file_mime_type = FILE_MANAGER_MIME_TYPE_VIDEO; + } + + if(is_regular_file && !(mime_type & file_mime_type)) + continue; + + auto body_item = BodyItem::create(p.path().filename().string()); + if(file_mime_type == FILE_MANAGER_MIME_TYPE_IMAGE || file_mime_type == FILE_MANAGER_MIME_TYPE_VIDEO) { body_item->thumbnail_is_local = true; body_item->thumbnail_url = p.path().string(); } + + time_t last_modified_time = std::chrono::duration_cast<std::chrono::seconds>(file_get_last_modified_time(p, std::filesystem::file_time_type::min()).time_since_epoch()).count(); + struct tm last_modified_tm; + localtime_r(&last_modified_time, &last_modified_tm); + strftime(time_str, sizeof(time_str) - 1, "%a %b %d %H:%M", &last_modified_tm); + + std::error_code file_size_err; + size_t file_size = p.file_size(file_size_err); + if(file_size_err) + file_size = 0; + + std::string description = "Modified: "; + description += time_str; + if(is_regular_file) { + description += "\n"; + description += "Size: " + file_size_to_human_readable_string(file_size); + } + body_item->set_description(std::move(description)); + body_item->set_description_color(sf::Color(179, 179, 179)); + result_items.push_back(std::move(body_item)); } diff --git a/src/plugins/Fourchan.cpp b/src/plugins/Fourchan.cpp index 7cad54f..5e6a970 100644 --- a/src/plugins/Fourchan.cpp +++ b/src/plugins/Fourchan.cpp @@ -208,11 +208,8 @@ namespace QuickMedia { } } - // TODO: Merge with get_threads_internal - PluginResult FourchanThreadListPage::submit(const std::string &title, const std::string &url, std::vector<Tab> &result_tabs) { - (void)title; - cached_media_urls.clear(); - + // TODO: Merge with lazy fetch + PluginResult FourchanThreadListPage::submit(const std::string&, const std::string &url, std::vector<Tab> &result_tabs) { Json::Value json_root; DownloadResult result = download_json(json_root, fourchan_url + board_id + "/thread/" + url + ".json", {}, true); if(result != DownloadResult::OK) return download_result_to_plugin_result(result); @@ -299,8 +296,8 @@ namespace QuickMedia { comment_text.append(cp.text.data, cp.text.size); break; case CommentPiece::Type::QUOTE: - comment_text += '>'; - comment_text.append(cp.text.data, cp.text.size); + //comment_text += '>'; + //comment_text.append(cp.text.data, cp.text.size); //comment_text += '\n'; break; case CommentPiece::Type::QUOTELINK: { @@ -342,8 +339,7 @@ namespace QuickMedia { // thumbnails always has .jpg extension even if they are gifs or webm. std::string tim_str = std::to_string(tim.asInt64()); body_item->thumbnail_url = fourchan_image_url + board_id + "/" + tim_str + "s.jpg"; - body_item->attached_content_url = fourchan_image_url + board_id + "/" + tim_str + ext_str; - cached_media_urls.push_back(body_item->attached_content_url); + body_item->url = fourchan_image_url + board_id + "/" + tim_str + ext_str; sf::Vector2i thumbnail_size(64, 64); const Json::Value &tn_w = post["tn_w"]; @@ -358,7 +354,7 @@ namespace QuickMedia { auto body = create_body(); body->items = std::move(result_items); - result_tabs.push_back(Tab{std::move(body), std::make_unique<FourchanThreadPage>(program, board_id, url, std::move(cached_media_urls)), nullptr}); + result_tabs.push_back(Tab{std::move(body), std::make_unique<FourchanThreadPage>(program, board_id, url), nullptr}); return PluginResult::OK; } @@ -404,8 +400,8 @@ namespace QuickMedia { title_text.append(cp.text.data, cp.text.size); break; case CommentPiece::Type::QUOTE: - title_text += '>'; - title_text.append(cp.text.data, cp.text.size); + //title_text += '>'; + //title_text.append(cp.text.data, cp.text.size); //comment_text += '\n'; break; case CommentPiece::Type::QUOTELINK: { @@ -433,8 +429,8 @@ namespace QuickMedia { comment_text.append(cp.text.data, cp.text.size); break; case CommentPiece::Type::QUOTE: - comment_text += '>'; - comment_text.append(cp.text.data, cp.text.size); + //comment_text += '>'; + //comment_text.append(cp.text.data, cp.text.size); //comment_text += '\n'; break; case CommentPiece::Type::QUOTELINK: { @@ -535,7 +531,14 @@ namespace QuickMedia { } } - PostResult FourchanThreadPage::post_comment(const std::string &captcha_id, const std::string &comment) { + static std::string file_get_filename(const std::string &filepath) { + size_t index = filepath.rfind('/'); + if(index == std::string::npos) + return filepath.c_str(); + return filepath.c_str() + index + 1; + } + + PostResult FourchanThreadPage::post_comment(const std::string &captcha_id, const std::string &comment, const std::string &filepath) { std::string url = "https://sys.4chan.org/" + board_id + "/post"; std::vector<CommandArg> additional_args = { @@ -546,6 +549,15 @@ namespace QuickMedia { CommandArg{"-F", "mode=regist"} }; + if(!filepath.empty()) { + std::string filename = file_get_filename(filepath); + if(filename[0] == '@') + filename = "\\" + filename; + + additional_args.push_back({ "-F", "upfile=@" + filepath }); + additional_args.push_back({ "-F", "filename=" + filename }); + } + if(pass_id.empty()) { additional_args.push_back(CommandArg{"-F", "g-recaptcha-response=" + captcha_id}); } else { @@ -569,6 +581,9 @@ namespace QuickMedia { return PostResult::BANNED; if(response.find("try again") != std::string::npos || response.find("No valid captcha") != std::string::npos) return PostResult::TRY_AGAIN; + if(response.find("Audio streams are not allowed") != std::string::npos) + return PostResult::FILE_TYPE_NOT_ALLOWED; + return PostResult::ERR; } diff --git a/src/plugins/ImageBoard.cpp b/src/plugins/ImageBoard.cpp index 6f97082..881d8ff 100644 --- a/src/plugins/ImageBoard.cpp +++ b/src/plugins/ImageBoard.cpp @@ -1,21 +1,6 @@ #include "../../plugins/ImageBoard.hpp" namespace QuickMedia { - BodyItems ImageBoardThreadPage::get_related_media(const std::string &url, std::string&) { - BodyItems body_items; - auto it = std::find(cached_media_urls.begin(), cached_media_urls.end(), url); - if(it == cached_media_urls.end()) - return body_items; - - ++it; - for(; it != cached_media_urls.end(); ++it) { - auto body_item = BodyItem::create(""); - body_item->url = *it; - body_items.push_back(std::move(body_item)); - } - return body_items; - } - std::unique_ptr<RelatedVideosPage> ImageBoardThreadPage::create_related_videos_page(Program*, const std::string&, const std::string&) { return nullptr; } |