QString filesUrl() { QString folder = dataFolder().replace("\\", "/").replace(" ", "%20"); if (!folder.startsWith("//")) folder = "///" + folder; return QString("file:%1/files").arg(folder); }
void removeInAppFile(QString filename) { QString id = QFileInfo(filename).baseName(); QSqlQuery qry; if (! qry.exec("delete from files where id = " + id)) qDebug() << qry.lastError(); insertLog("files", "delete", id); filename = QString("%1/files/%2").arg(dataFolder()).arg(filename); if (! QFile::remove(filename)) qDebug() << "remove file error: " << filename; }
void initDownload() { // Find server port for (port_ = 6800; port_ < 7800; ++port_) { try { // Can bind to IP? Poco::Net::ServerSocket socket(Poco::Net::SocketAddress("127.0.0.1", port_)); socket.close(); // Stop, found port break; } catch (...) {} } // Build arguments Poco::Process::Args arguments; arguments.push_back("-n"); arguments.push_back("-o"); arguments.push_back("ParCheck=yes"); arguments.push_back("-o"); arguments.push_back("ParRepair=yes"); arguments.push_back("-o"); arguments.push_back("LockFile=" + dataFolder() + "nzbget.lock"); arguments.push_back("-o"); arguments.push_back("LogFile=" + dataFolder() + "nzbget.log"); arguments.push_back("-o"); arguments.push_back("NzbDir=" + dataFolder() + "nzb"); arguments.push_back("-o"); arguments.push_back("QueueDir=" + dataFolder() + "queue"); arguments.push_back("-o"); arguments.push_back("DestDir=" + dataFolder() + "done"); arguments.push_back("-o"); arguments.push_back("TempDir=" + dataFolder() + "temp"); arguments.push_back("-o"); arguments.push_back("ServerIp=0.0.0.0"); // 127.0.0.1"); arguments.push_back("-o"); arguments.push_back("ServerPort=" + Poco::NumberFormatter::format(port_)); arguments.push_back("-o"); arguments.push_back("OutputMode=loggable"); arguments.push_back("-o"); arguments.push_back("DownloadRate=" + config().getString("downloadRate", "0")); arguments.push_back("-o"); arguments.push_back("Server1.Level=0"); arguments.push_back("-o"); arguments.push_back("Server1.Host=" + config().getString("options.server", "usenet.server")); arguments.push_back("-o"); arguments.push_back("Server1.Port=" + config().getString("options.port", "119")); arguments.push_back("-o"); arguments.push_back("Server1.Username="******"options.username", "username")); arguments.push_back("-o"); arguments.push_back("Server1.Password="******"options.password", "")); arguments.push_back("-o"); arguments.push_back("Server1.Encryption=" + config().getString("options.encryption", "no")); arguments.push_back("-o"); arguments.push_back("Server1.Connections=" + config().getString("options.connections", "1")); arguments.push_back("-s"); // Start download server Poco::Pipe p1, p2; Poco::Process::launch(config().getString("options.nzbgetBin", "/usr/bin/nzbget"), arguments, 0, &p1, &p2); // Add download? if (config().getString("download", "-") != "-") { // Start download Poco::Process::Args arguments; arguments.push_back("-A"); arguments.push_back(config().getString("download")); sendCommand(arguments); } }
bool Syncer::getLogsAndFiles(QStringList& logs, QStringList& files) { QSqlQuery qry; bool finished = setSyncTime(); QString condition = QString("created_at > '%1' and created_at <= '%2'").arg(formatDateTime(lastSync), formatDateTime(syncTime)); qry.exec("select table_name, row_op, row_id, user_id, created_at, row_data from logs where "+ condition); while (qry.next()) logs.append(QString("%1,%2,%3,%4,%5|%6").arg(qry.value(0).toString(), qry.value(1).toString(), qry.value(2).toString(), qry.value(3).toString(), qry.value(4).toString(), qry.value(5).toString())); // extract new filenames qry.exec("select row_id, row_data from logs where table_name = 'files' and "+ condition); while (qry.next()) files.append(QString("%1/files/").arg(dataFolder()) + qry.value(0).toString() +"."+ qry.value(1).toString().mid(1,3)); return finished; }
QString getInAppFilename(QString filename) { if (! filename.startsWith(filesUrl())) { if (filename.startsWith("file:///")) filename = filename.mid(8); QString ext = filename.mid(filename.indexOf('.') + 1); StrMap file; QString fileId; file["extension"] = ext; QSqlQuery qry; if (qry.exec(getReplaceQuery("files", file, fileId))) { insertLog("files", "insert", fileId); QString newfile = QString("%1/files/%2.%3").arg(dataFolder()).arg(fileId).arg(ext); if (QFile::exists(newfile)) QFile::remove(newfile); if (QFile::copy(filename, newfile)) filename = newfile; else { qDebug() << "file copy error: " << filename << newfile; return ""; } } else { qDebug() << qry.lastError().text(); return ""; } } return QFileInfo(filename).fileName(); }
void FileSystemRoute::handleRequest(ServerEventArgs& evt) { Poco::Path dataFolder(ofToDataPath("", true)); Poco::Path documentRoot(ofToDataPath(getSettings().getDocumentRoot(), true)); std::string dataFolderString = dataFolder.toString(); std::string documentRootString = documentRoot.toString(); // Document root validity check. if (_settings.getRequireDocumentRootInDataFolder() && (documentRootString.length() < dataFolderString.length() || documentRootString.substr(0, dataFolderString.length()) != dataFolderString)) { ofLogError("FileSystemRoute::handleRequest") << "Document Root is not a sub directory of the data folder."; evt.getResponse() .setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR); handleErrorResponse(evt); return; } // check path Poco::URI uri(evt.getRequest().getURI()); std::string path = uri.getPath(); // just get the path // make paths absolute if (path.empty()) { path = "/"; } Poco::Path requestPath = documentRoot.append(path).makeAbsolute(); // add the default index if no filename is requested if (requestPath.getFileName().empty()) { requestPath.append(getSettings().getDefaultIndex()).makeAbsolute(); } std::string requestPathString = requestPath.toString(); // double check path safety (not needed?) if ((requestPathString.length() < documentRootString.length() || requestPathString.substr(0, documentRootString.length()) != documentRootString)) { ofLogError("FileSystemRoute::handleRequest") << "Requested document not inside DocumentFolder."; evt.getResponse().setStatusAndReason(Poco::Net::HTTPResponse::HTTP_NOT_FOUND); handleErrorResponse(evt); return; } ofFile file(requestPathString); // use it to parse file name parts std::string mediaTypeString = MediaTypeMap::getDefault()->getMediaTypeForPath(file.path()).toString(); try { // TODO: this is where we would begin to work honoring /// Accept-Encoding:gzip, deflate, sdch evt.getResponse().sendFile(file.getAbsolutePath(), mediaTypeString); return; } catch (const Poco::FileNotFoundException& exc) { ofLogError("FileSystemRoute::handleRequest") << exc.displayText(); evt.getResponse().setStatusAndReason(Poco::Net::HTTPResponse::HTTP_NOT_FOUND); handleErrorResponse(evt); return; } catch (const Poco::OpenFileException& exc) { ofLogError("FileSystemRoute::handleRequest") << exc.displayText(); evt.getResponse().setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR); handleErrorResponse(evt); return; } catch (const std::exception& exc) { ofLogError("FileSystemRoute::handleRequest") << "Unknown server error: " << exc.what(); evt.getResponse().setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR); handleErrorResponse(evt); return; } }
void FileSystemRouteHandler::handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response) { Poco::Path dataFolder(ofToDataPath("",true)); Poco::Path documentRoot(ofToDataPath(_parent.getSettings().getDocumentRoot(),true)); std::string dataFolderString = dataFolder.toString(); std::string documentRootString = documentRoot.toString(); // doc root validity check if(_parent.getSettings().getRequireDocumentRootInDataFolder() && (documentRootString.length() < dataFolderString.length() || documentRootString.substr(0,dataFolderString.length()) != dataFolderString)) { ofLogError("ServerDefaultRouteHandler::handleRequest") << "Document Root is not a sub directory of the data folder."; response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR); _parent.handleRequest(request,response); return; } // check path Poco::URI uri(request.getURI()); std::string path = uri.getPath(); // just get the path // make paths absolute if(path.empty()) { path = "/"; } Poco::Path requestPath = documentRoot.append(path).makeAbsolute(); // add the default index if no filename is requested if(requestPath.getFileName().empty()) { requestPath.append(_parent.getSettings().getDefaultIndex()).makeAbsolute(); } std::string requestPathString = requestPath.toString(); // double check path safety (not needed?) if((requestPathString.length() < documentRootString.length() || requestPathString.substr(0,documentRootString.length()) != documentRootString)) { ofLogError("ServerDefaultRouteHandler::handleRequest") << "Requested document not inside DocumentFolder."; response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_NOT_FOUND); _parent.handleRequest(request,response); return; } ofFile file(requestPathString); // use it to parse file name parts try { // ofx::Media::MediaTypeMap mediaMap; // Poco::Net::MediaType mediaType = mediaMap.getMediaTypeForSuffix(file.getExtension()); std::string mediaTypeString = "application/octet-stream"; std::string ext = file.getExtension(); if(ext == "json") { mediaTypeString = "application/json"; } else if(ext == "html") { mediaTypeString = "text/html"; } else if(ext == "jpg" || ext == "jpeg") { mediaTypeString = "image/jpeg"; } else if(ext == "png") { mediaTypeString = "image/png"; } else if(ext == "js") { mediaTypeString = "application/javascript"; } else if(ext == "css") { mediaTypeString = "text/css"; } else if(ext == "xml") { mediaTypeString = "application/xml"; } else if(ext == "ico") { mediaTypeString = "image/x-icon"; } response.sendFile(file.getAbsolutePath(), mediaTypeString); // will throw exceptions return; } catch (const Poco::FileNotFoundException& ex) { ofLogError("ServerDefaultRouteHandler::handleRequest") << ex.displayText(); response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_NOT_FOUND); _parent.handleRequest(request,response); } catch (const Poco::OpenFileException& ex) { ofLogError("ServerDefaultRouteHandler::handleRequest") << ex.displayText(); response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR); _parent.handleRequest(request,response); } catch (const exception& ex) { ofLogError("ServerDefaultRouteHandler::handleRequest") << "Unknown server error: " << ex.what(); response.setStatusAndReason(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR); _parent.handleRequest(request,response); } }
//--------------------------------------------------------------------------- STATIC void C23Cooker::CookObjFilesInFolder( const std::string& rawDataDirectory, const std::string& cookedDataDestinationDirectory, bool isCookAllEnabled ) { std::vector< Vertex3D > vertexes; std::vector< unsigned int > indexes; std::vector< std::string > filePaths; std::string objFileName; std::string c23FileName; std::string destination( cookedDataDestinationDirectory ); OBJFileHandler objFileHandler; C23FileHandler c23FileHandler; // find all the .obj files in this directory // if ( rawDataDirectory.back() == '/' ) // { // FileSystem::EnumerateFilesInDirectory( rawDataDirectory + "*.obj", filePaths ); // } // else // { // FileSystem::EnumerateFilesInDirectory( rawDataDirectory + "/*.obj", filePaths ); // } FileSystem::EnumerateFilesInDirectory( rawDataDirectory, filePaths ); // if we found no files, return if ( filePaths.empty() ) { std::cerr << "No files found!" << std::endl; return; } // add a trailing '/' to destination if don't already have if ( destination.back() != '/' ) { destination += '/'; } std::wstring dataFolder( destination.begin(), destination.end() ); CreateDirectoryW( dataFolder.c_str(), nullptr ); for ( unsigned int index = 0; index < ( unsigned int ) filePaths.size(); ++ index ) { std::string& currentFilePath = filePaths[ index ]; destination = cookedDataDestinationDirectory + currentFilePath.substr( rawDataDirectory.find_first_of( '*' ) ); if ( currentFilePath.substr( 0, currentFilePath.find_last_of( '/' ) + 1 ) == currentFilePath ) { std::cout << "Creating directory " + destination + "... "; std::wstring newDir( destination.begin(), destination.end() ); CreateDirectoryW( newDir.c_str(), nullptr ); std::cout << "Done!" << std::endl; } else if ( currentFilePath.substr( currentFilePath.find_last_of( '.' ) ) == ".obj" ) { if ( FileSystem::IsFileDateOlder( currentFilePath, destination.substr( 0, destination.find_last_of( '.' ) ) + ".c23" ) && !isCookAllEnabled ) continue; destination = destination.substr( 0, destination.find_last_of( '/' ) + 1 ); objFileName = currentFilePath.substr( currentFilePath.find_last_of( '/' ) + 1 ); c23FileName = objFileName.substr( 0, objFileName.find_last_of( '.' ) ) + ".c23"; std::cout << "Cooking " << objFileName << "... "; objFileHandler.ReadFromObjAndStoreVertexesAndIndexes( currentFilePath.c_str(), vertexes, indexes ); if ( vertexes.empty() ) { std::cerr << "Error - file failed to load!" << std:: endl; continue; } c23FileHandler.WriteToFile( ( destination + c23FileName ).c_str(), vertexes, indexes ); std::cout << "Success!" << std::endl; vertexes.clear(); indexes.clear(); } else { if ( FileSystem::IsFileDateOlder( currentFilePath, destination ) && !isCookAllEnabled ) continue; std::cout << "Copying " << currentFilePath << " to " << destination << "... "; std::wstring oldFile( currentFilePath.begin(), currentFilePath.end() ); std::wstring newFile( destination.begin(), destination.end() ); CopyFileW( oldFile.c_str(), newFile.c_str(), false ); std::cout << "Done!" << std::endl; } } // std::cout << "Cooking " << filePaths.size() << " items..." << std::endl; // cook each file to the destination directory }