Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 25 additions & 43 deletions client/src/screens/browse/components/file-display/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand All @@ -47,55 +48,36 @@ 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 () => {
if (!isLoggedIn) {
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 () => {
Expand Down
1 change: 1 addition & 0 deletions client/src/utils/formatFile.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
4 changes: 2 additions & 2 deletions server/modules/course/course.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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},
});

Expand Down
11 changes: 10 additions & 1 deletion server/services/UploadFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down
Loading