"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.updatePlaylistMiniatureFromExisting = exports.updatePlaylistMiniatureFromUrl = exports.updatePlaceholderThumbnail = exports.updateVideoMiniatureFromExisting = exports.updateVideoMiniatureFromUrl = exports.generateVideoMiniature = void 0;
const tslib_1 = require("tslib");
const path_1 = require("path");
const image_utils_1 = require("../helpers/image-utils");
const config_1 = require("../initializers/config");
const constants_1 = require("../initializers/constants");
const thumbnail_1 = require("../models/video/thumbnail");
const local_actor_1 = require("./local-actor");
const video_path_manager_1 = require("./video-path-manager");
const parent_process_1 = require("./worker/parent-process");
function updatePlaylistMiniatureFromExisting(options) {
    const { inputPath, playlist, automaticallyGenerated, keepOriginal = false, size } = options;
    const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size);
    const type = 1;
    const thumbnailCreator = () => {
        return (0, parent_process_1.processImageFromWorker)({ path: inputPath, destination: outputPath, newSize: { width, height }, keepOriginal });
    };
    return updateThumbnailFromFunction({
        thumbnailCreator,
        filename,
        height,
        width,
        type,
        automaticallyGenerated,
        existingThumbnail
    });
}
exports.updatePlaylistMiniatureFromExisting = updatePlaylistMiniatureFromExisting;
function updatePlaylistMiniatureFromUrl(options) {
    const { downloadUrl, playlist, size } = options;
    const { filename, basePath, height, width, existingThumbnail } = buildMetadataFromPlaylist(playlist, size);
    const type = 1;
    const fileUrl = playlist.isOwned()
        ? null
        : downloadUrl;
    const thumbnailCreator = () => {
        return (0, local_actor_1.downloadImageFromWorker)({ url: downloadUrl, destDir: basePath, destName: filename, size: { width, height } });
    };
    return updateThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl });
}
exports.updatePlaylistMiniatureFromUrl = updatePlaylistMiniatureFromUrl;
function updateVideoMiniatureFromUrl(options) {
    const { downloadUrl, video, type, size } = options;
    const { filename: updatedFilename, basePath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size);
    const fileUrl = video.isOwned()
        ? null
        : downloadUrl;
    const thumbnailUrlChanged = hasThumbnailUrlChanged(existingThumbnail, downloadUrl, video);
    const filename = thumbnailUrlChanged
        ? updatedFilename
        : existingThumbnail.filename;
    const thumbnailCreator = () => {
        if (thumbnailUrlChanged) {
            return (0, local_actor_1.downloadImageFromWorker)({ url: downloadUrl, destDir: basePath, destName: filename, size: { width, height } });
        }
        return Promise.resolve();
    };
    return updateThumbnailFromFunction({ thumbnailCreator, filename, height, width, type, existingThumbnail, fileUrl });
}
exports.updateVideoMiniatureFromUrl = updateVideoMiniatureFromUrl;
function updateVideoMiniatureFromExisting(options) {
    const { inputPath, video, type, automaticallyGenerated, size, keepOriginal = false } = options;
    const { filename, outputPath, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size);
    const thumbnailCreator = () => {
        return (0, parent_process_1.processImageFromWorker)({ path: inputPath, destination: outputPath, newSize: { width, height }, keepOriginal });
    };
    return updateThumbnailFromFunction({
        thumbnailCreator,
        filename,
        height,
        width,
        type,
        automaticallyGenerated,
        existingThumbnail
    });
}
exports.updateVideoMiniatureFromExisting = updateVideoMiniatureFromExisting;
function generateVideoMiniature(options) {
    const { video, videoFile, type } = options;
    return video_path_manager_1.VideoPathManager.Instance.makeAvailableVideoFile(videoFile.withVideoOrPlaylist(video), input => {
        const { filename, basePath, height, width, existingThumbnail, outputPath } = buildMetadataFromVideo(video, type);
        const thumbnailCreator = videoFile.isAudio()
            ? () => (0, parent_process_1.processImageFromWorker)({
                path: constants_1.ASSETS_PATH.DEFAULT_AUDIO_BACKGROUND,
                destination: outputPath,
                newSize: { width, height },
                keepOriginal: true
            })
            : () => (0, image_utils_1.generateImageFromVideoFile)({
                fromPath: input,
                folder: basePath,
                imageName: filename,
                size: { height, width }
            });
        return updateThumbnailFromFunction({
            thumbnailCreator,
            filename,
            height,
            width,
            type,
            automaticallyGenerated: true,
            existingThumbnail
        });
    });
}
exports.generateVideoMiniature = generateVideoMiniature;
function updatePlaceholderThumbnail(options) {
    const { fileUrl, video, type, size } = options;
    const { filename: updatedFilename, height, width, existingThumbnail } = buildMetadataFromVideo(video, type, size);
    const thumbnailUrlChanged = hasThumbnailUrlChanged(existingThumbnail, fileUrl, video);
    const thumbnail = existingThumbnail || new thumbnail_1.ThumbnailModel();
    const filename = thumbnailUrlChanged
        ? updatedFilename
        : existingThumbnail.filename;
    thumbnail.filename = filename;
    thumbnail.height = height;
    thumbnail.width = width;
    thumbnail.type = type;
    thumbnail.fileUrl = fileUrl;
    return thumbnail;
}
exports.updatePlaceholderThumbnail = updatePlaceholderThumbnail;
function hasThumbnailUrlChanged(existingThumbnail, downloadUrl, video) {
    const existingUrl = existingThumbnail
        ? existingThumbnail.fileUrl
        : null;
    return !existingUrl || existingUrl !== downloadUrl || downloadUrl.endsWith(`${video.uuid}.jpg`);
}
function buildMetadataFromPlaylist(playlist, size) {
    const filename = playlist.generateThumbnailName();
    const basePath = config_1.CONFIG.STORAGE.THUMBNAILS_DIR;
    return {
        filename,
        basePath,
        existingThumbnail: playlist.Thumbnail,
        outputPath: (0, path_1.join)(basePath, filename),
        height: size ? size.height : constants_1.THUMBNAILS_SIZE.height,
        width: size ? size.width : constants_1.THUMBNAILS_SIZE.width
    };
}
function buildMetadataFromVideo(video, type, size) {
    const existingThumbnail = Array.isArray(video.Thumbnails)
        ? video.Thumbnails.find(t => t.type === type)
        : undefined;
    if (type === 1) {
        const filename = (0, image_utils_1.generateImageFilename)();
        const basePath = config_1.CONFIG.STORAGE.THUMBNAILS_DIR;
        return {
            filename,
            basePath,
            existingThumbnail,
            outputPath: (0, path_1.join)(basePath, filename),
            height: size ? size.height : constants_1.THUMBNAILS_SIZE.height,
            width: size ? size.width : constants_1.THUMBNAILS_SIZE.width
        };
    }
    if (type === 2) {
        const filename = (0, image_utils_1.generateImageFilename)();
        const basePath = config_1.CONFIG.STORAGE.PREVIEWS_DIR;
        return {
            filename,
            basePath,
            existingThumbnail,
            outputPath: (0, path_1.join)(basePath, filename),
            height: size ? size.height : constants_1.PREVIEWS_SIZE.height,
            width: size ? size.width : constants_1.PREVIEWS_SIZE.width
        };
    }
    return undefined;
}
function updateThumbnailFromFunction(parameters) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const { thumbnailCreator, filename, width, height, type, existingThumbnail, automaticallyGenerated = null, fileUrl = null } = parameters;
        const oldFilename = existingThumbnail && existingThumbnail.filename !== filename
            ? existingThumbnail.filename
            : undefined;
        const thumbnail = existingThumbnail || new thumbnail_1.ThumbnailModel();
        thumbnail.filename = filename;
        thumbnail.height = height;
        thumbnail.width = width;
        thumbnail.type = type;
        thumbnail.fileUrl = fileUrl;
        thumbnail.automaticallyGenerated = automaticallyGenerated;
        if (oldFilename)
            thumbnail.previousThumbnailFilename = oldFilename;
        yield thumbnailCreator();
        return thumbnail;
    });
}
//# sourceMappingURL=thumbnail.js.map