bool InvokeJavascript_TwoArgs(NPObject *npobj, const char *methodName, const NPVariant &arg1, const NPVariant &arg2, NPVariant *&result) { bool success = false; if (!strcmp(methodName, "saveTextFile") && NPVARIANT_IS_STRING(arg1) && NPVARIANT_IS_STRING(arg2)) { const char *filename = stringFromNpVariant(arg1); const char *contents = stringFromNpVariant(arg2); success = saveText(filename, contents, arg2.value.stringValue.UTF8Length); delete[] contents; delete[] filename; } else if (!strcmp(methodName, "saveBinaryFile") && NPVARIANT_IS_STRING(arg1) && NPVARIANT_IS_OBJECT(arg2)) { const char *filename = stringFromNpVariant(arg1); size_t length; const char *bytes = byteArrayFromNpVariant(arg2, GetInstance(npobj), length); success = saveBinaryFile(filename, bytes, length); delete[] bytes; delete[] filename; } return success; }
FB::variant BracExtenssionProviderAPI::saveToBracFile(const FB::variant& msg) { std::string command = ""; if (msg.empty()) log("Received message is empty."); std::string msg_str = msg.cast<std::string>(); std::istringstream input(msg_str); jsonxx::Object o; if(!jsonxx::Object::parse(input, o)) { log("Couldn't parse the JSON message."); return "error"; } if(!o.has<jsonxx::Object>("brac")) { log("Couldn't find the brac JSON object."); return "error"; } jsonxx::Object & brac_info = o.get<jsonxx::Object>("brac"); jsonxx::Object & bric_info = o.get<jsonxx::Object>("bric"); std::string path = brac_info.get<std::string>("filepath"); std::string path_orig = path; path += ".zip"; #if defined _WIN32 char drive[_MAX_DRIVE]; char dir[_MAX_DIR]; char fname[_MAX_FNAME]; char ext[_MAX_EXT]; _splitpath(path.c_str(), drive, dir, fname, ext); command = "rename \"" + path_orig + "\" \"" + fname + ext + "\""; #elif defined __APPLE__ std::string escaped_path = escape_path(path); std::string escaped_path_orig = escape_path(path_orig); command = "mv " + escaped_path_orig + " " + escaped_path; #endif systemCall(command); #if defined _WIN32 command = "cd /d \"" + m_extension_path + "\" & bin\\7za.exe e -y \"" + path + "\" brac.xml"; #elif defined __APPLE__ std::string escaped_extension_path = escape_path(m_extension_path); command = "cd " + escaped_extension_path + "; bin/7za e -y " + escaped_path + " brac.xml"; #endif systemCall(command); std::string in_file = m_extension_path + "/brac.xml"; pugi::xml_document brac_xml; pugi::xml_parse_result result = brac_xml.load_file(in_file.c_str()); if (!result) log(result.description()); pugi::xml_node brac_node = brac_xml.child("brac"); if (brac_node.empty()) { log("brac node could not be found"); return "error"; } pugi::xml_node layers_node = brac_node.child("layers"); if (layers_node.empty()) { log("layers node could not be found"); return "error"; } pugi::xml_object_range<pugi::xml_node_iterator> layers = layers_node.children(); int max_id = -1; if (layers.begin() != layers.end()) for (pugi::xml_node_iterator itr = layers.begin(); itr != layers.end(); ++itr) if (max_id < itr->attribute("id").as_int()) max_id = itr->attribute("id").as_int(); int new_brac_num = max_id + 1; pugi::xml_node bric_node = layers_node.append_child("bric"); time_t t = time(0); // get current time struct tm * now = localtime( &t ); char time_now[50]; sprintf(time_now, "%d-%02d-%02d %02d:%02d:%02d", now->tm_year + 1900, now->tm_mon + 1, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec ); bric_node.append_attribute("revision" ).set_value(1); bric_node.append_attribute("lastupdate" ).set_value(time_now); bric_node.append_attribute("id" ).set_value(new_brac_num); bric_node.append_attribute("alpha" ).set_value(bric_info.get<std::string>("alpha" ).c_str()); bric_node.append_attribute("order" ).set_value(bric_info.get<std::string>("order" ).c_str()); bric_node.append_attribute("resolution" ).set_value(bric_info.get<std::string>("resolution" ).c_str()); bric_node.append_attribute("position" ).set_value(bric_info.get<std::string>("position" ).c_str()); bric_node.append_attribute("maskposition").set_value(bric_info.get<std::string>("maskposition").c_str()); bric_node.append_attribute("rotate" ).set_value(bric_info.get<std::string>("rotation" ).c_str()); bric_node.append_attribute("scale" ).set_value(bric_info.get<std::string>("scale" ).c_str()); bric_node.append_attribute("timeinterval").set_value(bric_info.get<std::string>("timeInterval").c_str()); bric_node.append_child(pugi::node_pcdata).set_value(bric_info.get<std::string>("comment").c_str()); brac_node.attribute("name") .set_value(brac_info.get<std::string>("name").c_str()); brac_node.attribute("artist").set_value(brac_info.get<std::string>("artist").c_str()); brac_node.attribute("tags") .set_value(brac_info.get<std::string>("tags").c_str()); pugi::xml_object_range<pugi::xml_node_iterator> children = brac_node.children(); for (pugi::xml_node_iterator itr = children.begin(); itr != children.end(); ++itr) if (itr->type() == pugi::node_pcdata) { brac_node.remove_child(*itr); itr = children.begin(); } brac_node.append_child(pugi::node_pcdata).set_value(brac_info.get<std::string>("comment").c_str()); std::string out_file = m_extension_path + "/brac.xml"; if (!brac_xml.save_file(out_file.c_str())) log("Error occurred while creating the new brac file: " + out_file); #if defined _WIN32 command = "cd /d \"" + m_extension_path + "\" & bin\\7za.exe u \"" + path + "\" brac.xml & del /F /Q brac.xml"; #elif defined __APPLE__ command = "cd " + escaped_extension_path + "; bin/7za u " + escaped_path + " brac.xml; rm -f brac.xml"; #endif systemCall(command); std::stringstream stm; stm << new_brac_num; std::string new_bric_path = m_extension_path + "/temp/bric." + stm.str(); #if defined _WIN32 command = "mkdir \"" + new_bric_path + "\""; #elif defined __APPLE__ std::string escaped_new_bric_path = escape_path(new_bric_path); command = "mkdir -p " + escaped_new_bric_path; #endif systemCall(command); pugi::xml_document bric_xml; pugi::xml_node root_node = bric_xml.append_child("bric"); root_node.append_attribute("version" ).set_value("1.0"); root_node.append_attribute("title" ).set_value(bric_info.get<std::string>("title" ).c_str()); root_node.append_attribute("timeinterval").set_value(bric_info.get<std::string>("timeInterval").c_str()); root_node.append_attribute("startdate" ).set_value(bric_info.get<std::string>("startDate" ).c_str()); root_node.append_attribute("url" ).set_value(bric_info.get<std::string>("url" ).c_str()); root_node.append_attribute("region" ).set_value(bric_info.get<std::string>("region" ).c_str()); root_node.append_attribute("tags" ).set_value(bric_info.get<std::string>("tags" ).c_str()); pugi::xml_node snapshot_node = root_node.append_child("snapshot"); snapshot_node.append_attribute("revision").set_value("1"); snapshot_node.append_attribute("date").set_value(time_now); std::string new_bric_filepath = new_bric_path + "/bric.xml"; std::string bric_screenshot_filename = "1.png"; std::string bric_screenshot = new_bric_path + "/" + bric_screenshot_filename; std::string bric_thumbnail = "thumb.png"; std::string bric_thumbnail_path = new_bric_path + "/" + bric_thumbnail; if (!bric_xml.save_file(new_bric_filepath.c_str())) log("Error occurred while creating the new bric file: " + new_bric_filepath); std::vector<std::string> brac_resolution = split(brac_info.get<std::string>("resolution"), ' '); if (brac_resolution.size() != 2) log("Brac resolution should be a pair of integer numbers, separated by space character."); std::vector<std::string> bric_region = split(bric_info.get<std::string>("local_region"), ' '); if (bric_region.size() != 4) log("Bric region should be four integer numbers, separated by space character."); std::string mask_layer_b64 = bric_info.get<std::string>("mask_b64").c_str(); bool mask_created = false; std::string mask_filename = new_bric_path + "/mask.png"; if (!mask_layer_b64.empty()) { std::string decoded = base64_decode(mask_layer_b64); mask_created = saveBinaryFile(mask_filename.c_str(), decoded.c_str(), decoded.size()); } #if defined _WIN32 command = "cd /d \"" + m_extension_path + "\" & move \"" + m_screenshot_dir + "\\" + m_screenshot_filename + "\" \"" + bric_screenshot + "\""; systemCall(command); //*/ command = "cd /d \"" + m_extension_path + "\" & bin\\clg-crop.exe" + " \"" + bric_screenshot + "\" " + bric_region[0] + " " + bric_region[1] + " " + bric_region[2] + " " + bric_region[3]; /*/ command = "cd /d \"" + m_extension_path + "\" & python bin\\crop.py" + " \"" + bric_screenshot + "\" " + bric_region[0] + " " + bric_region[1] + " " + bric_region[2] + " " + bric_region[3]; //*/ systemCall(command); command = "cd /d \"" + new_bric_path + "\" & copy " + bric_screenshot_filename + " " + bric_thumbnail; systemCall(command); command = "cd /d \"" + m_extension_path + "\" & bin\\7za.exe a \"" + path + "\" \"" + new_bric_path + "\""; systemCall(command); command = "rmdir /S /Q \"" + m_extension_path + "\\temp\""; systemCall(command); _splitpath(path_orig.c_str(), drive, dir, fname, ext); command = "rename \"" + path + "\" \"" + fname + ext + "\""; systemCall(command); #elif defined __APPLE__ std::string escaped_bric_screenshot = escape_path(bric_screenshot); std::string escaped_screenshot_dir = escape_path(m_screenshot_dir); command = "mv " + escaped_screenshot_dir + "/" + m_screenshot_filename + " " + escaped_bric_screenshot; systemCall(command); //*/ command = "cd " + escaped_extension_path + "; open -gW bin/clg-crop.app --args " + " " + escaped_bric_screenshot + " " + bric_region[0] + " " + bric_region[1] + " " + bric_region[2] + " " + bric_region[3]; /*/ command = "cd " + escaped_extension_path + "; bin/crop.py" + " " + escaped_bric_screenshot + " " + bric_region[0] + " " + bric_region[1] + " " + bric_region[2] + " " + bric_region[3]; //*/ systemCall(command); std::string escaped_bric_thumbnail = escape_path(bric_thumbnail_path); command = "cp \"" + escaped_bric_screenshot + "\" \"" + escaped_bric_thumbnail + "\""; systemCall(command); command = "cd " + escaped_extension_path + "; bin/7za a " + escaped_path + " " + escaped_new_bric_path; systemCall(command); command = "rm -Rf " + escaped_extension_path + "/temp"; systemCall(command); command = "mv " + escaped_path + " " + escaped_path_orig; systemCall(command); #endif fire_cleanup(); return "done!"; }