From b290d61b3435f743e3fb2396b47366a3ebca796c Mon Sep 17 00:00:00 2001 From: DreamBot706 <187842058+DreamBot706@users.noreply.github.com.> Date: Sat, 5 Jul 2025 17:33:00 +0000 Subject: [PATCH] fix file display and add thumbnail to file schema --- .../browse/components/file-display/index.jsx | 68 +++++++------------ client/src/utils/formatFile.js | 1 + server/modules/course/course.model.js | 4 +- server/services/UploadFile.js | 11 ++- 4 files changed, 38 insertions(+), 46 deletions(-) diff --git a/client/src/screens/browse/components/file-display/index.jsx b/client/src/screens/browse/components/file-display/index.jsx index cd6c971d..1ad78db0 100644 --- a/client/src/screens/browse/components/file-display/index.jsx +++ b/client/src/screens/browse/components/file-display/index.jsx @@ -11,6 +11,7 @@ import clientRoot from "../../../../api/client"; import capitalise from "../../../../utils/capitalise.js"; import Share from "../../../share"; const FileDisplay = ({ file, path, code }) => { + console.log(file); const fileSize = formatFileSize(file.size); const fileType = formatFileType(file.name); let name = file.name; @@ -35,7 +36,7 @@ const FileDisplay = ({ file, path, code }) => { const dispatch = useDispatch(); - const urls = useSelector((state) => state.URLS); + const preview_url = file.webUrl; const handleShare = () => { const sectionShare = document.getElementById("share"); @@ -47,27 +48,28 @@ const FileDisplay = ({ file, path, code }) => { toast.error("Please login to download."); return; } - const openedWindow = window.open("", "_blank"); - openedWindow.document.write("Please close this window after download starts."); - const existingUrl = urls.downloadUrls.find((data) => data.id === file.id); - if (existingUrl) { - openedWindow.location.href = existingUrl.url; - return; - } - const response = donwloadFile(file.id); - toast.promise(response, { - pending: "Generating download link...", - success: "Downloading file....", - error: "Something went wrong!", - }); - response - .then((data) => { - dispatch(AddDownloadUrl(file.id, data.url)); - openedWindow.location.href = data.url; - }) - .catch(() => { - openedWindow.close(); - }); + window.open(file.downloadUrl); + // const openedWindow = window.open("", "_blank"); + // openedWindow.document.write("Please close this window after download starts."); + // const existingUrl = urls.downloadUrls.find((data) => data.id === file.id); + // if (existingUrl) { + // openedWindow.location.href = existingUrl.url; + // return; + // } + // const response = donwloadFile(file.id); + // toast.promise(response, { + // pending: "Generating download link...", + // success: "Downloading file....", + // error: "Something went wrong!", + // }); + // response + // .then((data) => { + // dispatch(AddDownloadUrl(file.id, data.url)); + // openedWindow.location.href = data.url; + // }) + // .catch(() => { + // openedWindow.close(); + // }); }; const handlePreview = async () => { @@ -75,27 +77,7 @@ const FileDisplay = ({ file, path, code }) => { toast.error("Please login to preview file."); return; } - const openedWindow = window.open("", "_blank"); - openedWindow.document.write("Loading preview..."); - const existingUrl = urls.previewUrls.find((data) => data.id === file.id); - if (existingUrl) { - openedWindow.location.href = existingUrl.url; - return; - } - const response = previewFile(file.id); - - toast.promise(response, { - pending: "Loading preview...", - error: "Something went wrong!", - }); - response - .then((data) => { - dispatch(AddPreviewUrl(file.id, data.url)); - openedWindow.location.href = data.url; - }) - .catch(() => { - openedWindow.close(); - }); + window.open(preview_url, "_blank"); }; const handleAddToFavourites = async () => { diff --git a/client/src/utils/formatFile.js b/client/src/utils/formatFile.js index fd102a42..d8d42fa6 100644 --- a/client/src/utils/formatFile.js +++ b/client/src/utils/formatFile.js @@ -1,5 +1,6 @@ export const formatFileSize = (file_size) => { try { + file_size = file_size/(1024 * 1024); return parseFloat(file_size) > 1 ? parseFloat(file_size).toFixed(0) + "MB" : (parseFloat(file_size) * 1000).toFixed(0) + "KB"; diff --git a/server/modules/course/course.model.js b/server/modules/course/course.model.js index 4f8b6e57..ecfeb704 100644 --- a/server/modules/course/course.model.js +++ b/server/modules/course/course.model.js @@ -14,9 +14,9 @@ const FileSchema = Schema({ //type: { type: String, required: true }, fileId: { type: String, required: true }, size: { type: String, required: true }, - //thumbnail: { type: String }, + thumbnail: { type: String }, webUrl: { type: String, required: true }, - downloadUrl: { type: String, required: true }, + downloadUrl: { type: String, required: true }, //remove isVerified: {type: Boolean, default: false, required: true}, }); diff --git a/server/services/UploadFile.js b/server/services/UploadFile.js index d7ac363a..fbad41ba 100644 --- a/server/services/UploadFile.js +++ b/server/services/UploadFile.js @@ -88,8 +88,8 @@ async function UploadFile(contributionId, filePath, fileName) { }; try { const { data } = await axios.put(url, file, config); - const createurllink = `https://graph.microsoft.com/v1.0/me/drive/items/${data.id}/createLink`; + const thumbnaillink = `https://graph.microsoft.com/v1.0/me/drive/items/${data.id}/thumbnails`; const urldata = await axios.post(createurllink, { type: "view", scope: "organization" @@ -101,11 +101,20 @@ async function UploadFile(contributionId, filePath, fileName) { } } ) + const thumbnaildata = await axios.get(thumbnaillink, + { + headers: { + Authorization: `Bearer ${access_token}`, + } + } + ) + const thumbnailurl = thumbnaildata.data.value?.[0]?.medium?.url; const webUrl = urldata?.data?.link?.webUrl; const fileData = new FileModel({ isVerified: (existingContribution.approved) ? true : false, // The file is directly verified if the contribution is default approved fileId: data.id, // which is what happens when BR makes a contribution size: data.size, + thumbnail: thumbnailurl, name: fileName, downloadUrl: `${webUrl}?download=1`, webUrl: webUrl,