Backend:
- add download of episode media function - serving of episodes/seasons files - pulling seasons/episodes media on entry create - maintenance for seasons/episodes media
This commit is contained in:
1 parent
534572775f
commit
4e20c5320b
6 files changed
+58
-31
No files matched your search
@@ -50,7 +50,7 @@ class MediaController {
|
|||||||
return res.status(404).send('Category Not Found');
|
return res.status(404).send('Category Not Found');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!['backdrops', 'posters', 'logos', 'others'].includes(type)) {
|
if (!['backdrops', 'posters', 'logos', 'stills', 'others'].includes(type)) {
|
||||||
return res.status(404).send('Type Not Found');
|
return res.status(404).send('Type Not Found');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -83,12 +83,16 @@ class ConfigService {
|
|||||||
const logoDir = path.join(dataPath, 'images', 'entries', 'logos');
|
const logoDir = path.join(dataPath, 'images', 'entries', 'logos');
|
||||||
const collectionsPostersDir = path.join(dataPath, 'images', 'collections', 'posters');
|
const collectionsPostersDir = path.join(dataPath, 'images', 'collections', 'posters');
|
||||||
const collectionsBackdropsDir = path.join(dataPath, 'images', 'collections', 'backdrops');
|
const collectionsBackdropsDir = path.join(dataPath, 'images', 'collections', 'backdrops');
|
||||||
|
const seasonsPostersDir = path.join(dataPath, 'images', 'seasons', 'posters');
|
||||||
|
const episodesStillsDir = path.join(dataPath, 'images', 'episodes', 'stills');
|
||||||
|
|
||||||
fs.mkdirSync(posterDir, { recursive: true });
|
fs.mkdirSync(posterDir, { recursive: true });
|
||||||
fs.mkdirSync(backdropDir, { recursive: true });
|
fs.mkdirSync(backdropDir, { recursive: true });
|
||||||
fs.mkdirSync(logoDir, { recursive: true });
|
fs.mkdirSync(logoDir, { recursive: true });
|
||||||
fs.mkdirSync(collectionsPostersDir, { recursive: true });
|
fs.mkdirSync(collectionsPostersDir, { recursive: true });
|
||||||
fs.mkdirSync(collectionsBackdropsDir, { recursive: true });
|
fs.mkdirSync(collectionsBackdropsDir, { recursive: true });
|
||||||
|
fs.mkdirSync(seasonsPostersDir, { recursive: true });
|
||||||
|
fs.mkdirSync(episodesStillsDir, { recursive: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
getTmdbApiUrl() {
|
getTmdbApiUrl() {
|
||||||
|
|||||||
@@ -210,7 +210,10 @@ class EntryManager {
|
|||||||
let existingEpisode = await Episode.findOne({
|
let existingEpisode = await Episode.findOne({
|
||||||
where: {
|
where: {
|
||||||
tmdb_id: episode.id,
|
tmdb_id: episode.id,
|
||||||
}
|
},
|
||||||
|
include: [
|
||||||
|
{ model: Media, as: 'medias' }
|
||||||
|
]
|
||||||
});
|
});
|
||||||
|
|
||||||
// If the episode exists, update it; otherwise, create a new episode
|
// If the episode exists, update it; otherwise, create a new episode
|
||||||
@@ -229,7 +232,7 @@ class EntryManager {
|
|||||||
await existingEpisode.save();
|
await existingEpisode.save();
|
||||||
} else {
|
} else {
|
||||||
// Create a new episode
|
// Create a new episode
|
||||||
await Episode.create({
|
existingEpisode = await Episode.create({
|
||||||
tmdb_id: episode.id,
|
tmdb_id: episode.id,
|
||||||
season_id: existingSeason.id,
|
season_id: existingSeason.id,
|
||||||
name: episode.name,
|
name: episode.name,
|
||||||
@@ -242,6 +245,9 @@ class EntryManager {
|
|||||||
still_path: episode.still_path,
|
still_path: episode.still_path,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (existingEpisode.medias.length == 0)
|
||||||
|
_mediaManager.getEpisodenMedia(existingEpisode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -198,6 +198,8 @@ class EntryService {
|
|||||||
banner_path: null,
|
banner_path: null,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
_mediaManager.getSeasonMedia(savedSeason);
|
||||||
|
|
||||||
// * episodes
|
// * episodes
|
||||||
let episodes = [];
|
let episodes = [];
|
||||||
for (const episode of seasonData.episodes) {
|
for (const episode of seasonData.episodes) {
|
||||||
@@ -219,6 +221,7 @@ class EntryService {
|
|||||||
});
|
});
|
||||||
|
|
||||||
episodes.push(savedEpisode);
|
episodes.push(savedEpisode);
|
||||||
|
_mediaManager.getEpisodenMedia(savedEpisode);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO fetch cast
|
// TODO fetch cast
|
||||||
@@ -364,7 +367,11 @@ class EntryService {
|
|||||||
entry_id: id
|
entry_id: id
|
||||||
},
|
},
|
||||||
include: [
|
include: [
|
||||||
{ model: Episode, as: 'episodes' },
|
{
|
||||||
|
model: Episode, as: 'episodes', include: [
|
||||||
|
{ model: Media, as: 'medias' }
|
||||||
|
]
|
||||||
|
},
|
||||||
{ model: Media, as: 'medias' }
|
{ model: Media, as: 'medias' }
|
||||||
],
|
],
|
||||||
order: [
|
order: [
|
||||||
|
|||||||
@@ -39,7 +39,8 @@ class MaintenanceService {
|
|||||||
'collection;poster': path.join(dataPath, 'images', 'collections', 'posters'),
|
'collection;poster': path.join(dataPath, 'images', 'collections', 'posters'),
|
||||||
'collection;backdrop': path.join(dataPath, 'images', 'collections', 'backdrops'),
|
'collection;backdrop': path.join(dataPath, 'images', 'collections', 'backdrops'),
|
||||||
|
|
||||||
'season;poster': path.join(dataPath, 'images', 'seasons', 'posters')
|
'season;poster': path.join(dataPath, 'images', 'seasons', 'posters'),
|
||||||
|
'episode;still': path.join(dataPath, 'images', 'episodes', 'stills')
|
||||||
};
|
};
|
||||||
|
|
||||||
let missingFiles = [];
|
let missingFiles = [];
|
||||||
@@ -56,7 +57,8 @@ class MaintenanceService {
|
|||||||
let removed = {
|
let removed = {
|
||||||
'posters': 0,
|
'posters': 0,
|
||||||
'logos': 0,
|
'logos': 0,
|
||||||
'backdrops': 0
|
'backdrops': 0,
|
||||||
|
'stills': 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
// if (missingFiles.length == 0)
|
// if (missingFiles.length == 0)
|
||||||
@@ -83,20 +85,11 @@ class MaintenanceService {
|
|||||||
// Getting the userDataPath to store the images
|
// Getting the userDataPath to store the images
|
||||||
const dataPath = _configService.getMediaFolder();
|
const dataPath = _configService.getMediaFolder();
|
||||||
|
|
||||||
// ? Getting the path of all media files and putting them in an array
|
|
||||||
// const allMedia = (await Media.findAll()).map(m => {
|
|
||||||
// if (m.mediaable == 'entry')
|
|
||||||
// return path.join(dataPath, 'images', 'entries', m.type + 's', m.path)
|
|
||||||
// else if (m.mediaable == 'collection')
|
|
||||||
// return path.join(dataPath, 'images', 'collections', m.type + 's', m.path)
|
|
||||||
// });
|
|
||||||
|
|
||||||
// ? Getting the name (without extention) of all media in database
|
// ? Getting the name (without extention) of all media in database
|
||||||
const allMedia = (await Media.findAll()).map(m => {
|
const allMedia = (await Media.findAll()).map(m => {
|
||||||
return m.path.split('.')[0]
|
return m.path.split('.')[0]
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// ? Loop trough the files of the images folder and check
|
// ? Loop trough the files of the images folder and check
|
||||||
// ? if a file doesnt exist in the allMedia array and mark it to delete
|
// ? if a file doesnt exist in the allMedia array and mark it to delete
|
||||||
const mediaTypeFolder = {
|
const mediaTypeFolder = {
|
||||||
@@ -107,14 +100,16 @@ class MaintenanceService {
|
|||||||
'collection;poster': path.join(dataPath, 'images', 'collections', 'posters'),
|
'collection;poster': path.join(dataPath, 'images', 'collections', 'posters'),
|
||||||
'collection;backdrop': path.join(dataPath, 'images', 'collections', 'backdrops'),
|
'collection;backdrop': path.join(dataPath, 'images', 'collections', 'backdrops'),
|
||||||
|
|
||||||
'season;poster': path.join(dataPath, 'images', 'seasons', 'posters')
|
'season;poster': path.join(dataPath, 'images', 'seasons', 'posters'),
|
||||||
|
'episode;still': path.join(dataPath, 'images', 'episodes', 'stills')
|
||||||
};
|
};
|
||||||
|
|
||||||
// * counter
|
// * counter
|
||||||
let removed = {
|
let removed = {
|
||||||
'posters': 0,
|
'posters': 0,
|
||||||
'logos': 0,
|
'logos': 0,
|
||||||
'backdrops': 0
|
'backdrops': 0,
|
||||||
|
'stills': 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Iterate over each media type directory and check for orphan files
|
// Iterate over each media type directory and check for orphan files
|
||||||
@@ -127,13 +122,6 @@ class MaintenanceService {
|
|||||||
|
|
||||||
// Check each file in the directory
|
// Check each file in the directory
|
||||||
for (let file of files) {
|
for (let file of files) {
|
||||||
const filePath = path.join(directory, file);
|
|
||||||
// // Check if the constructed file path exists in the allMedia array
|
|
||||||
// // ? this step also takes into account preview files, by removing '_preview'
|
|
||||||
// // ? and verifying if the file path is in database
|
|
||||||
// const hasJpgFormat = allMedia.includes(filePath.replace("_preview.jpg", ".jpg"));
|
|
||||||
// const hasPngFormat = allMedia.includes(filePath.replace("_preview.jpg", ".png"));
|
|
||||||
// const hasGifFormat = allMedia.includes(filePath.replace("_preview.jpg", ".gif"));
|
|
||||||
|
|
||||||
// preparing the file name
|
// preparing the file name
|
||||||
const fileName = file.split('.')[0].replace("_preview", "");
|
const fileName = file.split('.')[0].replace("_preview", "");
|
||||||
@@ -142,7 +130,10 @@ class MaintenanceService {
|
|||||||
// if the file isn't in database even on other extentions then delete it
|
// if the file isn't in database even on other extentions then delete it
|
||||||
if (!isInDatabase) {
|
if (!isInDatabase) {
|
||||||
removed[folderType.split(';')[1] + 's']++; // Increment the count of removed files
|
removed[folderType.split(';')[1] + 's']++; // Increment the count of removed files
|
||||||
if (!dry) await fsPromises.unlink(filePath);
|
if (!dry) {
|
||||||
|
const b = path.join(directory, file);
|
||||||
|
await fsPromises.unlink(filePath);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -120,10 +120,7 @@ class MediaManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async getSeasonMedia(season) {
|
async getSeasonMedia(season) {
|
||||||
// const season = await Season.findOne(seasonId);
|
if (!season.poster_path) return;
|
||||||
|
|
||||||
// if (!season) return NotFoundError(`Season with id ${seasonId} not found.`);
|
|
||||||
|
|
||||||
const url = `https://image.tmdb.org/t/p/original${season.poster_path}`;
|
const url = `https://image.tmdb.org/t/p/original${season.poster_path}`;
|
||||||
const file = await _mediaDownloaderService.downloadFile(url);
|
const file = await _mediaDownloaderService.downloadFile(url);
|
||||||
|
|
||||||
@@ -135,7 +132,6 @@ class MediaManager {
|
|||||||
const fullFilePath = path.join(dataPath, 'images', 'seasons', 'posters', filename)
|
const fullFilePath = path.join(dataPath, 'images', 'seasons', 'posters', filename)
|
||||||
|
|
||||||
_storageService.storeFile(file, fullFilePath).then(async () => {
|
_storageService.storeFile(file, fullFilePath).then(async () => {
|
||||||
// create media object
|
|
||||||
await Media.create({
|
await Media.create({
|
||||||
mediaable: 'season',
|
mediaable: 'season',
|
||||||
mediaableId: season.id,
|
mediaableId: season.id,
|
||||||
@@ -144,9 +140,32 @@ class MediaManager {
|
|||||||
isLocal: true,
|
isLocal: true,
|
||||||
isSelected: true
|
isSelected: true
|
||||||
});
|
});
|
||||||
|
console.log(`File created for season: ${filename}`);
|
||||||
|
}).catch();
|
||||||
|
}
|
||||||
|
|
||||||
// optimine file
|
async getEpisodenMedia(episode) {
|
||||||
|
if (!episode.still_path) return;
|
||||||
|
const url = `https://image.tmdb.org/t/p/original${episode.still_path}`;
|
||||||
|
const file = await _mediaDownloaderService.downloadFile(url);
|
||||||
|
|
||||||
|
const uniqueFilename = uuidv4();
|
||||||
|
const extension = episode.still_path.split('.').pop();
|
||||||
|
const filename = `${uniqueFilename}.${extension}`;
|
||||||
|
|
||||||
|
const dataPath = _configService.getSettings().userDataPath;
|
||||||
|
const fullFilePath = path.join(dataPath, 'images', 'episodes', 'stills', filename)
|
||||||
|
|
||||||
|
_storageService.storeFile(file, fullFilePath).then(async () => {
|
||||||
|
await Media.create({
|
||||||
|
mediaable: 'episode',
|
||||||
|
mediaableId: episode.id,
|
||||||
|
type: 'still',
|
||||||
|
path: filename,
|
||||||
|
isLocal: true,
|
||||||
|
isSelected: true
|
||||||
|
});
|
||||||
|
console.log(`File created for episode: ${filename}`);
|
||||||
}).catch();
|
}).catch();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user