refactor: media fixing

This commit is contained in:
elangptra 2024-11-21 08:14:50 +07:00
parent 82a496acdf
commit cbe50bc14d
13 changed files with 81 additions and 26 deletions

View File

@ -713,6 +713,7 @@ export const updateExerciseById = async (req, res) => {
if (AUDIO) {
if (exercise.AUDIO) {
const oldAudioPath = path.join(
process.cwd(),
"media/uploads/exercise/audio",
exercise.AUDIO
);
@ -731,6 +732,7 @@ export const updateExerciseById = async (req, res) => {
if (IMAGE) {
if (exercise.IMAGE) {
const oldImagePath = path.join(
process.cwd(),
"media/uploads/exercise/image",
exercise.IMAGE
);
@ -988,11 +990,19 @@ export const deleteExerciseFileById = async (req, res) => {
if (fileType === "audio" && exercise.AUDIO) {
fileName = exercise.AUDIO;
filePath = path.join("media/uploads/exercise/audio", fileName);
filePath = path.join(
process.cwd(),
"media/uploads/exercise/audio",
fileName
);
exercise.AUDIO = null;
} else if (fileType === "image" && exercise.IMAGE) {
fileName = exercise.IMAGE;
filePath = path.join("media/uploads/exercise/image", fileName);
filePath = path.join(
process.cwd(),
"media/uploads/exercise/image",
fileName
);
exercise.IMAGE = null;
} else if (fileType === "video" && exercise.VIDEO) {
exercise.VIDEO = null;

View File

@ -577,6 +577,7 @@ export const updateLevelById = async (req, res, next) => {
if (AUDIO) {
if (level.AUDIO) {
const oldAudioPath = path.join(
process.cwd(),
"media/uploads/level/audio",
level.AUDIO
);
@ -594,6 +595,7 @@ export const updateLevelById = async (req, res, next) => {
if (IMAGE) {
if (level.IMAGE) {
const oldImagePath = path.join(
process.cwd(),
"media/uploads/level/image",
level.IMAGE
);
@ -677,11 +679,19 @@ export const deleteLevelFileById = async (req, res) => {
if (fileType === "audio" && level.AUDIO) {
fileName = level.AUDIO;
filePath = path.join("media/uploads/level/audio", fileName);
filePath = path.join(
process.cwd(),
"media/uploads/level/audio",
fileName
);
level.AUDIO = null;
} else if (fileType === "image" && level.IMAGE) {
fileName = level.IMAGE;
filePath = path.join("media/uploads/level/image", fileName);
filePath = path.join(
process.cwd(),
"media/uploads/level/image",
fileName
);
level.IMAGE = null;
} else if (fileType === "video" && level.VIDEO) {
level.VIDEO = null;
@ -745,9 +755,17 @@ export const deleteLevelFilesById = async (req, res) => {
let filePath;
if (fileType === "AUDIO") {
filePath = path.join("media/uploads/level/audio", fileName);
filePath = path.join(
process.cwd(),
"media/uploads/level/audio",
fileName
);
} else if (fileType === "IMAGE") {
filePath = path.join("media/uploads/level/image", fileName);
filePath = path.join(
process.cwd(),
"media/uploads/level/image",
fileName
);
} else {
return response(400, null, "Invalid file type", res);
}
@ -984,8 +1002,14 @@ export const getLevelFiles = async (req, res) => {
const { levelId } = req.params;
try {
const audioFolderPath = path.join("media/uploads/level/audio");
const imageFolderPath = path.join("media/uploads/level/image");
const audioFolderPath = path.join(
process.cwd(),
"media/uploads/level/audio"
);
const imageFolderPath = path.join(
process.cwd(),
"media/uploads/level/image"
);
const getFilesByLevelId = (folderPath, fileType) => {
if (fs.existsSync(folderPath)) {

View File

@ -59,7 +59,13 @@ export const getSectionForAdmin = async (req, res) => {
],
}),
},
attributes: ["ID_SECTION", "NAME_SECTION", "DESCRIPTION_SECTION", "THUMBNAIL", "TIME_SECTION"],
attributes: [
"ID_SECTION",
"NAME_SECTION",
"DESCRIPTION_SECTION",
"THUMBNAIL",
"TIME_SECTION",
],
distinct: true,
});
@ -72,9 +78,13 @@ export const getSectionForAdmin = async (req, res) => {
}));
if (sort === "section") {
formattedSections.sort((a, b) => a.NAME_SECTION.localeCompare(b.NAME_SECTION));
formattedSections.sort((a, b) =>
a.NAME_SECTION.localeCompare(b.NAME_SECTION)
);
} else if (sort === "description") {
formattedSections.sort((a, b) => a.DESCRIPTION_SECTION.localeCompare(b.DESCRIPTION_SECTION));
formattedSections.sort((a, b) =>
a.DESCRIPTION_SECTION.localeCompare(b.DESCRIPTION_SECTION)
);
} else {
formattedSections.sort(
(a, b) => new Date(b.TIME_SECTION) - new Date(a.TIME_SECTION)
@ -200,6 +210,7 @@ export const updateSectionById = async (req, res) => {
if (THUMBNAIL) {
if (section.THUMBNAIL) {
const oldThumbnailPath = path.join(
process.cwd(),
"media/uploads/section",
section.THUMBNAIL
);

View File

@ -163,6 +163,7 @@ export const updateMatchingPairsExerciseById = async (req, res) => {
if (AUDIO) {
if (exercise.AUDIO) {
const oldAudioPath = path.join(
process.cwd(),
"media/uploads/exercise/audio",
exercise.AUDIO
);
@ -181,6 +182,7 @@ export const updateMatchingPairsExerciseById = async (req, res) => {
if (IMAGE) {
if (exercise.IMAGE) {
const oldImagePath = path.join(
process.cwd(),
"media/uploads/exercise/image",
exercise.IMAGE
);

View File

@ -173,6 +173,7 @@ export const updateMultipleChoicesExerciseById = async (req, res) => {
if (AUDIO) {
if (exercise.AUDIO) {
const oldAudioPath = path.join(
process.cwd(),
"media/uploads/exercise/audio",
exercise.AUDIO
);
@ -191,6 +192,7 @@ export const updateMultipleChoicesExerciseById = async (req, res) => {
if (IMAGE) {
if (exercise.IMAGE) {
const oldImagePath = path.join(
process.cwd(),
"media/uploads/exercise/image",
exercise.IMAGE
);

View File

@ -139,6 +139,7 @@ export const updateTrueFalseExerciseById = async (req, res) => {
if (AUDIO) {
if (exercise.AUDIO) {
const oldAudioPath = path.join(
process.cwd(),
"media/uploads/exercise/audio",
exercise.AUDIO
);
@ -157,6 +158,7 @@ export const updateTrueFalseExerciseById = async (req, res) => {
if (IMAGE) {
if (exercise.IMAGE) {
const oldImagePath = path.join(
process.cwd(),
"media/uploads/exercise/image",
exercise.IMAGE
);

View File

@ -473,7 +473,11 @@ export const updateUserById = async (req, res) => {
if (PICTURE) {
if (user.PICTURE) {
const oldPicturePath = path.join("media/uploads/avatar", user.PICTURE);
const oldPicturePath = path.join(
process.cwd(),
"media/uploads/avatar",
user.PICTURE
);
if (fs.existsSync(oldPicturePath)) {
fs.unlinkSync(oldPicturePath);
}

View File

@ -116,13 +116,13 @@ export const saveFileToDisk = (file, type, topicId, sectionId, levelId) => {
let folderPath;
switch (type) {
case "AUDIO":
folderPath = path.join("media/uploads/level/audio");
folderPath = path.join(process.cwd(), "media/uploads/level/audio");
break;
case "IMAGE":
folderPath = path.join("media/uploads/level/image");
folderPath = path.join(process.cwd(), "media/uploads/level/image");
break;
default:
folderPath = path.join("media/uploads/level");
folderPath = path.join(process.cwd(), "media/uploads/level");
}
if (!fs.existsSync(folderPath)) {

View File

@ -116,13 +116,13 @@ export const saveFileToDisk = (file, type, topicId, sectionId, levelId) => {
let folderPath;
switch (type) {
case "AUDIO":
folderPath = path.join("media/uploads/level/audio");
folderPath = path.join(process.cwd(), "media/uploads/level/audio");
break;
case "IMAGE":
folderPath = path.join("media/uploads/level/image");
folderPath = path.join(process.cwd(), "media/uploads/level/image");
break;
default:
folderPath = path.join("media/uploads/level");
folderPath = path.join(process.cwd(), "media/uploads/level");
}
if (!fs.existsSync(folderPath)) {

View File

@ -77,7 +77,7 @@ export const saveFileToDisk = (file, userId, userName) => {
)
.digest("hex");
const filename = `user-${hash}${ext}`;
const folderPath = path.join("media/uploads/avatar");
const folderPath = path.join(process.cwd(), "media/uploads/avatar");
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath, { recursive: true });

View File

@ -104,13 +104,13 @@ export const saveFileToDisk = (file, type, levelId, exerciseId) => {
let folderPath;
switch (type) {
case "AUDIO":
folderPath = path.join("media/uploads/exercise/audio");
folderPath = path.join(process.cwd(), "media/uploads/exercise/audio");
break;
case "IMAGE":
folderPath = path.join("media/uploads/exercise/image");
folderPath = path.join(process.cwd(), "media/uploads/exercise/image");
break;
default:
folderPath = path.join("media/uploads/exercise");
folderPath = path.join(process.cwd(), "media/uploads/exercise");
}
if (!fs.existsSync(folderPath)) {

View File

@ -116,13 +116,13 @@ export const saveFileToDisk = (file, type, levelId, exerciseId) => {
let folderPath;
switch (type) {
case "AUDIO":
folderPath = path.join("media/uploads/exercise/audio");
folderPath = path.join(process.cwd(), "media/uploads/exercise/audio");
break;
case "IMAGE":
folderPath = path.join("media/uploads/exercise/image");
folderPath = path.join(process.cwd(), "media/uploads/exercise/image");
break;
default:
folderPath = path.join("media/uploads/exercise");
folderPath = path.join(process.cwd(), "media/uploads/exercise");
}
if (!fs.existsSync(folderPath)) {

View File

@ -75,7 +75,7 @@ export const saveFileToDisk = (file, fieldName, idSection) => {
.update(file.originalname + file.buffer.length.toString())
.digest("hex");
const filename = `${fieldName}-${idSection}-${hash}${ext}`;
const folderPath = path.join("media/uploads/section");
const folderPath = path.join(process.cwd(), "media/uploads/section");
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath, { recursive: true });