From 0d5ca44316c059e0be5d5da247299869a87cb40a Mon Sep 17 00:00:00 2001 From: Matthias Date: Tue, 6 Sep 2022 15:59:16 +0200 Subject: [PATCH] Added PDF Preview Closes #30 --- backend/src/controllers/fs/fs_routes.cpp | 14 +++++++---- .../components/FileViewer/BlobDownload.tsx | 18 +++++++++++--- .../src/components/FileViewer/FileViewer.vue | 24 +++++++++++++++---- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/backend/src/controllers/fs/fs_routes.cpp b/backend/src/controllers/fs/fs_routes.cpp index 6aea185..d7e09e7 100644 --- a/backend/src/controllers/fs/fs_routes.cpp +++ b/backend/src/controllers/fs/fs_routes.cpp @@ -42,6 +42,8 @@ const std::unordered_map mime_type_map = { { ".mkv" , "video/x-matroska" }, { ".mk3d" , "video/x-matroska" }, { ".mks" , "video/x-matroska" }, + + { ".pdf" , "application/pdf" } }; template @@ -170,7 +172,9 @@ namespace api { try { if (file.fileLength() > 100 * 1024 * 1024) throw std::exception(); std::filesystem::path filename(inode->getValueOfName()); - const std::string& mime = mime_type_map.at(filename.extension().string()); + std::string ext = filename.extension().string(); + std::transform(ext.begin(), ext.end(), ext.begin(), tolower); + const std::string& mime = mime_type_map.at(ext); if (!mime.starts_with("image")) throw std::exception(); cv::_InputArray image_arr(file.fileData(), (int) file.fileLength()); @@ -332,12 +336,12 @@ namespace api { if (!inode.has_value()) return cbk(dto::Responses::get_badreq_res("Unknown node")); - - std::filesystem::path p("./files"), name(inode->getValueOfName()); - p /= std::to_string(inode->getValueOfId()); + std::filesystem::path name(inode->getValueOfName()); + std::string ext = name.extension().string(); + std::transform(ext.begin(), ext.end(), ext.begin(), tolower); try { - cbk(dto::Responses::get_type_res(mime_type_map.at(name.extension().string()))); + cbk(dto::Responses::get_type_res(mime_type_map.at(ext))); } catch (const std::exception&) { cbk(dto::Responses::get_badreq_res("Invalid file type")); } diff --git a/frontend/src/components/FileViewer/BlobDownload.tsx b/frontend/src/components/FileViewer/BlobDownload.tsx index 9088599..a246b85 100644 --- a/frontend/src/components/FileViewer/BlobDownload.tsx +++ b/frontend/src/components/FileViewer/BlobDownload.tsx @@ -1,11 +1,12 @@ import { ref } from 'vue'; import { NProgress } from 'naive-ui'; import filesize from 'filesize'; -import { Music, Video, Image } from '@vicons/carbon'; +import { Music, Video, Image, Document } from '@vicons/carbon'; import type { DialogApiInjection } from 'naive-ui/es/dialog/src/DialogProvider'; export default function createBlobDialog( dialog: DialogApiInjection, + image: boolean, audio: boolean, video: boolean ) { @@ -14,11 +15,22 @@ export default function createBlobDialog( const percentage = ref(0); const dia = dialog.create({ title: - 'Loading ' + (video ? 'video' : audio ? 'audio' : 'image') + '...', + 'Loading ' + + (video ? 'video' : audio ? 'audio' : image ? 'image' : 'content') + + '...', closable: false, closeOnEsc: false, maskClosable: false, - icon: () => (video ?