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
11 changes: 8 additions & 3 deletions client/src/api/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ export const verifyFile = async (fileId) => {
};

// Unverify (delete) a file (BR only)
export const unverifyFile = async (fileId) => {
const { data } = await API.delete(`/files/unverify/${fileId}`);
return data;
export const unverifyFile = async (fileId, oneDriveId, folderId) => {
await API.delete(`/files/unverify/${fileId}`,
{
data: {
oneDriveId,
folderId,
}
});
};

export const getThumbnail = async (fileId) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const handleUnverify = async () => {

try {
console.log("Deleting file:", file._id);
await unverifyFile(file._id);
await unverifyFile(file._id, file.fileId, currFolderId);
toast.success("File deleted!");
// window.location.reload();
dispatch(RemoveFileFromFolder(file._id, true));
Expand Down
4 changes: 2 additions & 2 deletions client/src/screens/browse/components/folder-info/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CopyToClipboard } from "react-copy-to-clipboard";
import clientRoot from "../../../../api/client";
import Share from "../../../share";
import { useState } from "react";
const FolderInfo = ({ path, name, canDownload, contributionHandler, folderId, courseCode }) => {
const FolderInfo = ({ isBR, path, name, canDownload, contributionHandler, folderId, courseCode }) => {
const handleShare = () => {
const sectionShare = document.getElementById("share");
sectionShare.classList.add("show");
Expand Down Expand Up @@ -46,7 +46,7 @@ const FolderInfo = ({ path, name, canDownload, contributionHandler, folderId, co
</button> */}
<button className="btn plus" onClick={contributionHandler}>
<span className="icon plus-icon"></span>
<span className="text">Contribute</span>
<span className="text">{isBR? "Add File": "Contribute"}</span>
</button>
</div>
: <></>
Expand Down
1 change: 1 addition & 0 deletions client/src/screens/browse/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ const BrowseScreen = () => {
</div>
<div className="middle">
<FolderInfo
isBR={user.user.isBR}
path={folderData?.path ? folderData.path : "Select a folder..."}
name={folderData?.name ? folderData.name : "Select a folder"}
canDownload={folderData?.childType === "File"}
Expand Down
10 changes: 0 additions & 10 deletions client/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -487,11 +487,6 @@ electron-to-chromium@^1.4.251:
resolved "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.284.tgz"
integrity sha512-M8WEXFuKXMYMVr45fo8mq0wUrrJHheiKZf6BArTKk9ZBYCKJEOU5H8cdWgDT+qCVZf7Na4lVUaZsA+h6uA9+PA==

esbuild-darwin-arm64@0.15.16:
version "0.15.16"
resolved "https://registry.npmjs.org/esbuild-darwin-arm64/-/esbuild-darwin-arm64-0.15.16.tgz"
integrity sha512-fMXaUr5ou0M4WnewBKsspMtX++C1yIa3nJ5R2LSbLCfJT3uFdcRoU/NZjoM4kOMKyOD9Sa/2vlgN8G07K3SJnw==

esbuild@^0.15.9:
version "0.15.16"
resolved "https://registry.npmjs.org/esbuild/-/esbuild-0.15.16.tgz"
Expand Down Expand Up @@ -556,11 +551,6 @@ form-data@^4.0.0:
combined-stream "^1.0.8"
mime-types "^2.1.12"

fsevents@~2.3.2:
version "2.3.2"
resolved "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz"
integrity sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==

function-bind@^1.1.1:
version "1.1.1"
resolved "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz"
Expand Down
4 changes: 2 additions & 2 deletions server/modules/auth/auth.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ export const redirectHandler = async (req, res, next) => {
httpOnly: true,
});

// return res.redirect(appConfig.clientURL);
return res.redirect(`${appConfig.clientURL}/login/success?token=${token}`); //to redirect with token
return res.redirect(appConfig.clientURL);
// return res.redirect(`${appConfig.clientURL}/login/success?token=${token}`); //to redirect with token
};

export const mobileRedirectHandler = async (req, res, next) => {
Expand Down
12 changes: 9 additions & 3 deletions server/modules/file/file.controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FileModel } from "../course/course.model.js";
import { FileModel, FolderModel } from "../course/course.model.js";
import { DeleteFile } from "../../services/UploadFile.js";

// Verify file
export const verifyFile = async (req, res) => {
Expand All @@ -18,10 +19,15 @@ export const verifyFile = async (req, res) => {
// Unverify file (delete it)
export const unverifyFile = async (req, res) => {
try {
const file = await FileModel.findById(req.params.id);
//Delete file object from DB
const folderId = req.body.folderId;
await FolderModel.findByIdAndUpdate(folderId, {$pull: {children: req.params.id}});
const file = await FileModel.findByIdAndDelete(req.params.id);
if (!file) return res.status(404).json({ message: "File not found" });

await file.deleteOne();
//Delete file object from Onedrive
await DeleteFile(req.body.oneDriveId);

res.status(200).json({ message: "File deleted (unverified) successfully" });
} catch (err) {
res.status(500).json({ message: "Server error", error: err.message });
Expand Down
3 changes: 2 additions & 1 deletion server/modules/file/file.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const router = express.Router();

router.get("/all", isAuthenticated, getAllFiles);
router.put("/verify/:id", isAuthenticated, isBR, verifyFile);
router.delete("/unverify/:id", isAuthenticated, isBR, unverifyFile);
// router.delete("/unverify/:id", isAuthenticated, isBR, unverifyFile);
router.delete("/unverify/:id", unverifyFile);

export default router;
57 changes: 57 additions & 0 deletions server/services/UploadFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,61 @@ async function UploadFile(contributionId, filePath, fileName) {
}
}

async function DeleteFile(fileId) {
const access_token = await getAccessToken();

try {
//obtain parent folder onedrive id
const { data } = await axios.get(`https://graph.microsoft.com/v1.0/me/drive/items/${fileId}`,
{
headers: {
Authorization: `Bearer ${access_token}`,
}
}
)
const folderId = data?.parentReference?.id;

//delete entire folder if it is the only file or delete only the file
const empty = await isFolderEmpty(folderId, access_token)
if (empty) {
await axios.delete(`https://graph.microsoft.com/v1.0/me/drive/items/${folderId}`,
{
headers: {
Authorization: `Bearer ${access_token}`
}
}
)
}
else {
await axios.delete(`https://graph.microsoft.com/v1.0/me/drive/items/${fileId}`,
{
headers: {
Authorization: `Bearer ${access_token}`,
}
}
)
}

}
catch (err) {
console.log(err.response);
}
}

async function isFolderEmpty(folderId, access_token) {
const { data } = await axios.get(`https://graph.microsoft.com/v1.0/me/drive/items/${folderId}/children`,
{
headers: {
Authorization: `Bearer ${access_token}`
}
}
)

if (data.value.length === 1)
return true;
else
return false;
}

export { DeleteFile };
export default UploadFile;
5 changes: 0 additions & 5 deletions server/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1541,11 +1541,6 @@ fs.realpath@^1.0.0:
resolved "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz"
integrity sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==

fsevents@~2.3.2:
version "2.3.2"
resolved "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz"
integrity sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==

function-bind@^1.1.1:
version "1.1.1"
resolved "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz"
Expand Down
Loading