Added switch

This commit is contained in:
2026-08-10 18:58:26 +02:00
parent f57792e192
commit c0463e1764
3 changed files with 79 additions and 12 deletions
+40 -12
View File
@@ -10,6 +10,16 @@ const sharp = require("sharp")
const albumMap = new Map()
const assetsList = {}
const IMAGE_EXTENSIONS = new Set([
".jpg", ".jpeg", ".png", ".webp", ".heic", ".heif",
".gif", ".tiff", ".bmp", ".svg", ".avif", ".raw", ".dng"
])
const VIDEO_EXTENSIONS = new Set([
".mp4", ".mov", ".avi", ".mkv", ".webm",
".flv", ".wmv", ".m4v", ".3gp", ".ts"
])
async function main() {
immich.init({
apiKey: process.env.API_KEY,
@@ -17,7 +27,7 @@ async function main() {
})
const user = await immich.getMyUser()
console.log(`Logged as: ${user.name.cyan.bold}`)
if (process.env.COMPRESSION == "true") {
if (process.env.IMG_COMPRESSION == "true") {
console.log("")
console.log("Processing files compression...")
await processFilesCompression()
@@ -43,17 +53,24 @@ async function main() {
async function processFilesCompression() {
const files = await getFilesRecursive(process.env.UPLOAD_DIR)
for (const file of files) {
try {
console.log(`Compressing file: ${file.yellow.bold}`)
const compressedFile = await sharp(file)
.toFormat(process.env.COMPRESSION_OUTPUT_FORMAT,
{ quality: parseInt(process.env.COMPRESSION_OUTPUT_QUALITY) || 80 }
)
.toFile(changeExtension(file, process.env.COMPRESSION_OUTPUT_FORMAT))
await fs.promises.rm(file)
} catch (error) {
console.log(`Error occured while compressing file: ${file.red.bold}`)
console.log(error)
switch (getMediaType(file)) {
case "image":
try {
console.log(`Compressing image: ${file.yellow.bold}`)
const compressedFile = await sharp(file)
.toFormat(process.env.IMG_COMPRESSION_OUTPUT_FORMAT,
{ quality: parseInt(process.env.IMG_COMPRESSION_OUTPUT_QUALITY) || 80 }
)
.toFile(changeExtension(file, process.env.IMG_COMPRESSION_OUTPUT_FORMAT))
await fs.promises.rm(file)
} catch (error) {
console.log(`Error occured while compressing image: ${file.red.bold}`)
console.log(error)
}
break
default:
break
}
}
}
@@ -192,4 +209,15 @@ function changeExtension(filePath, newExt) {
return path.format(parsed)
}
function getMediaType(filePath) {
if (!filePath) return "unknown"
const ext = path.extname(filePath).toLowerCase()
if (IMAGE_EXTENSIONS.has(ext)) return "image"
if (VIDEO_EXTENSIONS.has(ext)) return "video"
return "unknown"
}
main()