diff --git a/src/util/XojPreviewExtractor.cpp b/src/util/XojPreviewExtractor.cpp index 7f0cb286..fea69617 100644 --- a/src/util/XojPreviewExtractor.cpp +++ b/src/util/XojPreviewExtractor.cpp @@ -5,6 +5,8 @@ #include #include +#include +#include const char* TAG_PREVIEW_NAME = "preview"; const int TAG_PREVIEW_NAME_LEN = strlen(TAG_PREVIEW_NAME); @@ -108,23 +110,69 @@ PreviewExtractResult XojPreviewExtractor::readFile(Path file) { return PREVIEW_RESULT_BAD_FILE_EXTENSION; } + //read the new file format + int zipError = 0; + zip_t* zipFp = zip_open(file.c_str(), ZIP_RDONLY, &zipError); - gzFile fp = GzUtil::openPath(file, "r"); - if (!fp) + if (!zipFp && zipError == ZIP_ER_NOZIP) { - return PREVIEW_RESULT_COULD_NOT_OPEN_FILE; + gzFile fp = GzUtil::openPath(file, "r"); + if (!fp) + { + return PREVIEW_RESULT_COULD_NOT_OPEN_FILE; + } + + // The Tag is within the first 179 Bytes + // The Preview should end within the first 8k + + char buffer[BUF_SIZE]; + int readLen = gzread(fp, buffer, BUF_SIZE); + + PreviewExtractResult result = readPreview(buffer, readLen); + + gzclose(fp); + return result; } - // The Tag is within the first 179 Bytes - // The Preview should end within the first 8k + zip_stat_t thumbStat; + int statStatus = zip_stat(zipFp, "thumbnails/thumbnail.png", 0, &thumbStat); + if (statStatus != 0) + { + return PREVIEW_RESULT_NO_PREVIEW; + } - char buffer[BUF_SIZE]; - int readLen = gzread(fp, buffer, BUF_SIZE); + if (thumbStat.valid & ZIP_STAT_SIZE) + { + dataLen = thumbStat.size; + } else + { + return PREVIEW_RESULT_ERROR_READING_PREVIEW; + } + + zip_file_t* thumb = zip_fopen(zipFp, "thumbnails/thumbnail.png", 0); + + if (!thumb) + { + return PREVIEW_RESULT_ERROR_READING_PREVIEW; + } - PreviewExtractResult result = readPreview(buffer, readLen); + data = (unsigned char *)g_malloc(thumbStat.size); + zip_uint64_t readBytes = 0; + while (readBytes < dataLen) + { + zip_int64_t read = zip_fread(thumb, data, thumbStat.size); + if (read == -1) + { + g_free(data); + return PREVIEW_RESULT_ERROR_READING_PREVIEW; + } + + readBytes += read; + } - gzclose(fp); - return result; + zip_fclose(thumb); + zip_close(zipFp); + return PREVIEW_RESULT_IMAGE_READ; } diff --git a/src/xoj-preview-extractor/CMakeLists.txt b/src/xoj-preview-extractor/CMakeLists.txt index b232b930..6c9722f4 100644 --- a/src/xoj-preview-extractor/CMakeLists.txt +++ b/src/xoj-preview-extractor/CMakeLists.txt @@ -19,6 +19,7 @@ add_executable (xournal-thumbnailer target_link_libraries (xournal-thumbnailer ${ZLIB_LIBRARIES} ${Glib_LDFLAGS} + ${ZIP_LDFLAGS} ) set (THUMBNAILER_BIN "xournal-thumbnailer")