Major Backend Single Responsibility Update part 1
This commit is contained in:
1 parent
65177c7553
commit
cbdffb21e3
41 files changed
+1723
-888
No files matched your search
@@ -0,0 +1,118 @@
|
||||
const path = require('path');
|
||||
|
||||
const Entry = require("../models/Entry");
|
||||
|
||||
const BadRequestError = require("../errors/BadRequestError");
|
||||
const NotFoundError = require("../errors/NotFoundError");
|
||||
|
||||
const configService = require("./configService");
|
||||
const tmdbService = require("./tmdbService");
|
||||
const mediaService = require("./mediaService");
|
||||
const mediaDownloaderService = require("./mediaDownloaderService");
|
||||
const mediaOptimizerService = require("./mediaOptimizerService");
|
||||
|
||||
const _configService = configService;
|
||||
const _mediaService = mediaService;
|
||||
const _tmdbService = tmdbService;
|
||||
const _mediaOptimizerService = mediaOptimizerService;
|
||||
const _mediaDownloaderService = mediaDownloaderService;
|
||||
|
||||
class MediaManager {
|
||||
|
||||
constructor() {
|
||||
if (!MediaManager.instance) {
|
||||
MediaManager.instance = this;
|
||||
}
|
||||
return MediaManager.instance;
|
||||
}
|
||||
|
||||
async saveMedia(entryId, files) {
|
||||
|
||||
if (!files) {
|
||||
throw new BadRequestError("No media uploaded");
|
||||
}
|
||||
|
||||
const entry = await Entry.findByPk(entryId);
|
||||
if (!entry) {
|
||||
throw new NotFoundError("Entry not found");
|
||||
}
|
||||
|
||||
const fileType = ['poster', 'backdrop', 'logo'].find(type => req.files[type]);
|
||||
|
||||
if (!fileType) {
|
||||
throw new BadRequestError("Invalid media type");
|
||||
}
|
||||
|
||||
// * 1 - save the media into database
|
||||
const file = req.files[fileType][0];
|
||||
const createdMedia = await _mediaService.createMedia({
|
||||
mediaable: 'entry',
|
||||
mediaableId: id,
|
||||
type: fileType,
|
||||
path: file.filename,
|
||||
isLocal: true,
|
||||
isSelected: false,
|
||||
isCustom: true
|
||||
});
|
||||
|
||||
// * 2 - generating low resolution preview
|
||||
_mediaOptimizerService.generatePreviewFiles('entries', fileType + 's', file.filename).then((result) => {
|
||||
const stream = fs.createReadStream(filePath);
|
||||
stream.pipe(res);
|
||||
|
||||
const msg = `Media Route | Generated Entry Preview`
|
||||
logToFile(msg);
|
||||
}).catch((err) => {
|
||||
const msg = `Media Route | Error Generating Entry Preview`
|
||||
logToFile(msg);
|
||||
});
|
||||
|
||||
return createdMedia;
|
||||
}
|
||||
|
||||
async getEntryMedia(tmdb_id, is_tvserie, includes = ['posters', 'logos', 'backdrops']) {
|
||||
// Fetching entry media
|
||||
const mediaData = await _tmdbService.fetchEntryMedia(tmdb_id, is_tvserie);
|
||||
if (!mediaData) {
|
||||
throw new InternalServerError(`Counldn't fetch data from TMDB`);
|
||||
}
|
||||
|
||||
// Local variables for this function invocation
|
||||
let downloadTasks = [];
|
||||
let downloadedImages = [];
|
||||
|
||||
_configService.createMediaFolders(); // Ensuring directories exist
|
||||
|
||||
// Getting the userDataPath to store the images
|
||||
const dataPath = _configService.getSettings().userDataPath;
|
||||
|
||||
// Handling different media types
|
||||
const mediaTypes = [
|
||||
{ type: 'posters', dir: path.join(dataPath, 'images', 'entries', 'posters') },
|
||||
{ type: 'logos', dir: path.join(dataPath, 'images', 'entries', 'logos') },
|
||||
{ type: 'backdrops', dir: path.join(dataPath, 'images', 'entries', 'backdrops') }
|
||||
];
|
||||
|
||||
mediaTypes.forEach(({ type, dir }) => {
|
||||
if (!includes.includes(type)) return; // ? the media type is not requested
|
||||
|
||||
if (mediaData[type] && mediaData[type].length) {
|
||||
const selectedMedia = _mediaDownloaderService.findPreferredMedia(mediaData[type]);
|
||||
if (selectedMedia) {
|
||||
_mediaDownloaderService.scheduleDownloads(selectedMedia, type.slice(0, -1), dir, downloadTasks, downloadedImages);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Wait for all downloads to complete
|
||||
await Promise.all(downloadTasks).then(() => {
|
||||
console.log('Downloaded images:', downloadedImages);
|
||||
}).catch(err => {
|
||||
console.error('Error downloading entry images:', err);
|
||||
});
|
||||
|
||||
return downloadedImages;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new MediaManager();
|
||||
Reference in new issue
Block a user