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

View File

@ -577,6 +577,7 @@ export const updateLevelById = async (req, res, next) => {
if (AUDIO) { if (AUDIO) {
if (level.AUDIO) { if (level.AUDIO) {
const oldAudioPath = path.join( const oldAudioPath = path.join(
process.cwd(),
"media/uploads/level/audio", "media/uploads/level/audio",
level.AUDIO level.AUDIO
); );
@ -594,6 +595,7 @@ export const updateLevelById = async (req, res, next) => {
if (IMAGE) { if (IMAGE) {
if (level.IMAGE) { if (level.IMAGE) {
const oldImagePath = path.join( const oldImagePath = path.join(
process.cwd(),
"media/uploads/level/image", "media/uploads/level/image",
level.IMAGE level.IMAGE
); );
@ -677,11 +679,19 @@ export const deleteLevelFileById = async (req, res) => {
if (fileType === "audio" && level.AUDIO) { if (fileType === "audio" && level.AUDIO) {
fileName = 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; level.AUDIO = null;
} else if (fileType === "image" && level.IMAGE) { } else if (fileType === "image" && level.IMAGE) {
fileName = 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; level.IMAGE = null;
} else if (fileType === "video" && level.VIDEO) { } else if (fileType === "video" && level.VIDEO) {
level.VIDEO = null; level.VIDEO = null;
@ -745,9 +755,17 @@ export const deleteLevelFilesById = async (req, res) => {
let filePath; let filePath;
if (fileType === "AUDIO") { 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") { } else if (fileType === "IMAGE") {
filePath = path.join("media/uploads/level/image", fileName); filePath = path.join(
process.cwd(),
"media/uploads/level/image",
fileName
);
} else { } else {
return response(400, null, "Invalid file type", res); return response(400, null, "Invalid file type", res);
} }
@ -984,8 +1002,14 @@ export const getLevelFiles = async (req, res) => {
const { levelId } = req.params; const { levelId } = req.params;
try { try {
const audioFolderPath = path.join("media/uploads/level/audio"); const audioFolderPath = path.join(
const imageFolderPath = path.join("media/uploads/level/image"); process.cwd(),
"media/uploads/level/audio"
);
const imageFolderPath = path.join(
process.cwd(),
"media/uploads/level/image"
);
const getFilesByLevelId = (folderPath, fileType) => { const getFilesByLevelId = (folderPath, fileType) => {
if (fs.existsSync(folderPath)) { 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, distinct: true,
}); });
@ -72,9 +78,13 @@ export const getSectionForAdmin = async (req, res) => {
})); }));
if (sort === "section") { 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") { } 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 { } else {
formattedSections.sort( formattedSections.sort(
(a, b) => new Date(b.TIME_SECTION) - new Date(a.TIME_SECTION) (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 (THUMBNAIL) {
if (section.THUMBNAIL) { if (section.THUMBNAIL) {
const oldThumbnailPath = path.join( const oldThumbnailPath = path.join(
process.cwd(),
"media/uploads/section", "media/uploads/section",
section.THUMBNAIL section.THUMBNAIL
); );

View File

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

View File

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

View File

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

View File

@ -473,7 +473,11 @@ export const updateUserById = async (req, res) => {
if (PICTURE) { if (PICTURE) {
if (user.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)) { if (fs.existsSync(oldPicturePath)) {
fs.unlinkSync(oldPicturePath); fs.unlinkSync(oldPicturePath);
} }

View File

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

View File

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

View File

@ -77,7 +77,7 @@ export const saveFileToDisk = (file, userId, userName) => {
) )
.digest("hex"); .digest("hex");
const filename = `user-${hash}${ext}`; 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)) { if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath, { recursive: true }); fs.mkdirSync(folderPath, { recursive: true });

View File

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

View File

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

View File

@ -75,7 +75,7 @@ export const saveFileToDisk = (file, fieldName, idSection) => {
.update(file.originalname + file.buffer.length.toString()) .update(file.originalname + file.buffer.length.toString())
.digest("hex"); .digest("hex");
const filename = `${fieldName}-${idSection}-${hash}${ext}`; 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)) { if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath, { recursive: true }); fs.mkdirSync(folderPath, { recursive: true });