[WIP] Zipper to download artists/releases, ref #37

This commit is contained in:
emeric
2020-08-27 13:31:25 +02:00
parent 5e37ec865a
commit a785847b9c
18 changed files with 985 additions and 17 deletions
+1 -1
View File
@@ -26,7 +26,7 @@
#include <Wt/WDateTime.h>
void computeCrc(const std::filesystem::path& p, std::vector<unsigned char>& checksum);
std::uint32_t computeCrc32(const std::filesystem::path& p);
// Make sure the given path is a directory
// Create it if needed
+1
View File
@@ -71,6 +71,7 @@ template<>
std::optional<std::string>
readAs(const std::string& str);
[[nodiscard]]
std::string
replaceInString(const std::string& str, const std::string& from, const std::string& to);
+94
View File
@@ -0,0 +1,94 @@
/*
* Copyright (C) 2020 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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 3 of the License, or
* (at your option) any later version.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <map>
#include <set>
#include <filesystem>
#include "Exception.hpp"
namespace Zip
{
class ZipperException : public LmsException
{
using LmsException::LmsException;
};
// Very simple on-the-fly zip creator
class Zipper
{
public:
enum class CompressionMethod
{
NoCompression,
};
Zipper(const std::map<std::string, std::filesystem::path>& files, CompressionMethod comp = CompressionMethod::NoCompression);
static constexpr std::size_t minOutputBufferSize = 64;
std::size_t writeSome(std::byte* buffer, std::size_t bufferSize);
bool isComplete() const;
private:
void setComplete();
std::size_t writeLocalFileHeader(std::byte* buffer, std::size_t bufferSize);
std::size_t writeLocalFileHeaderFileName(std::byte* buffer, std::size_t bufferSize);
std::size_t writeFileData(std::byte* buffer, std::size_t bufferSize);
std::size_t writeCentralDirectoryHeader(std::byte* buffer, std::size_t bufferSize);
std::size_t writeCentralDirectoryHeaderFileName(std::byte* buffer, std::size_t bufferSize);
std::size_t writeEndOfCentralDirectoryRecord(std::byte* buffer, std::size_t bufferSize);
struct FileContext
{
std::filesystem::path filePath;
std::size_t fileSize;
std::uint32_t fileCrc32;
std::size_t localFileHeaderOffset {};
};
using FileContainer = std::map<std::string, FileContext>;
FileContainer _files;
enum class WriteState
{
LocalFileHeader,
LocalFileHeaderFileName,
FileData,
CentralDirectoryHeader,
CentralDirectoryHeaderFileName,
EndOfCentralDirectoryRecord,
Complete,
};
CompressionMethod _compMethod {CompressionMethod::NoCompression};
WriteState _writeState {WriteState::LocalFileHeader};
FileContainer::iterator _currentFile;
std::size_t _currentOffset {};
std::size_t _currentZipOffset {};
std::size_t _centralDirectoryOffset {};
std::size_t _centralDirectorySize {};
};
} // namespace Zip