/* * This file is part of nzbget. See . * * Copyright (C) 2016 Andrey Prygunkov * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "nzbget.h" #include "NzbGenerator.h" #include "Util.h" #include "FileSystem.h" #include "Log.h" void NzbGenerator::Execute() { info("Generating nzbs for %s", *m_dataDir); DirBrowser dir(m_dataDir); while (const char* filename = dir.Next()) { BString<1024> fullFilename("%s%c%s", *m_dataDir, PATH_SEPARATOR, filename); int len = strlen(filename); if (len > 4 && !strcasecmp(filename + len - 4, ".nzb")) { // skip nzb-files continue; } GenerateNzb(fullFilename); } info("Nzb generation finished"); } void NzbGenerator::GenerateNzb(const char* path) { BString<1024> nzbFilename("%s%c%s.nzb", *m_dataDir, PATH_SEPARATOR, FileSystem::BaseFileName(path)); if (FileSystem::FileExists(nzbFilename)) { return; } info("Generating nzb for %s", FileSystem::BaseFileName(path)); DiskFile outfile; if (!outfile.Open(nzbFilename, DiskFile::omWrite)) { error("Could not create file %s", *nzbFilename); return; } outfile.Print("\n"); outfile.Print("\n"); outfile.Print("\n"); bool isDir = FileSystem::DirectoryExists(path); if (isDir) { AppendDir(outfile, path); } else { AppendFile(outfile, path, nullptr); } outfile.Print("\n"); outfile.Close(); } void NzbGenerator::AppendDir(DiskFile& outfile, const char* path) { DirBrowser dir(path); while (const char* filename = dir.Next()) { BString<1024> fullFilename("%s%c%s", path, PATH_SEPARATOR, filename); bool isDir = FileSystem::DirectoryExists(fullFilename); if (!isDir) { AppendFile(outfile, fullFilename, FileSystem::BaseFileName(path)); } } } void NzbGenerator::AppendFile(DiskFile& outfile, const char* filename, const char* relativePath) { detail("Processing %s", FileSystem::BaseFileName(filename)); int64 fileSize = FileSystem::FileSize(filename); time_t timestamp = Util::CurrentTime(); int segmentCount = (int)((fileSize + m_segmentSize - 1) / m_segmentSize); outfile.Print("\n", (int)timestamp, FileSystem::BaseFileName(filename), segmentCount); outfile.Print("\n"); outfile.Print("alt.binaries.test\n"); outfile.Print("\n"); outfile.Print("\n"); int64 segOffset = 0; for (int segno = 1; segno <= segmentCount; segno++) { int segSize = (int)(segOffset + m_segmentSize < fileSize ? m_segmentSize : fileSize - segOffset); outfile.Print("%s%s%s?%i=%" PRIi64 ":%i\n", m_segmentSize, segno, relativePath ? relativePath : "", relativePath ? "/" : "", FileSystem::BaseFileName(filename), segno, segOffset, segSize); segOffset += segSize; } outfile.Print("\n"); outfile.Print("\n"); }