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
+38
View File
@@ -12,6 +12,7 @@
"@immich/sdk": "^3.1.0", "@immich/sdk": "^3.1.0",
"colors": "^1.4.0", "colors": "^1.4.0",
"dotenv": "^17.4.2", "dotenv": "^17.4.2",
"fluent-ffmpeg": "^2.1.3",
"mime-types": "^3.0.2", "mime-types": "^3.0.2",
"sharp": "^0.35.3" "sharp": "^0.35.3"
} }
@@ -589,6 +590,11 @@
"integrity": "sha512-fi7dp7dNayyh/vzqhf0ZdoPfC7tJvYfjaE8MBL1yR+iIsH7cFoqHt+DV70VU49OMCqLc7wQa+yVJcSmIRnV4wA==", "integrity": "sha512-fi7dp7dNayyh/vzqhf0ZdoPfC7tJvYfjaE8MBL1yR+iIsH7cFoqHt+DV70VU49OMCqLc7wQa+yVJcSmIRnV4wA==",
"license": "MIT" "license": "MIT"
}, },
"node_modules/async": {
"version": "0.2.10",
"resolved": "https://registry.npmjs.org/async/-/async-0.2.10.tgz",
"integrity": "sha512-eAkdoKxU6/LkKDBzLpT+t6Ff5EtfSF4wx1WfJiPEEV7WNLnDaRXk0oVysiEPm262roaachGexwUv94WhSgN5TQ=="
},
"node_modules/colors": { "node_modules/colors": {
"version": "1.4.0", "version": "1.4.0",
"resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz", "resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz",
@@ -619,6 +625,26 @@
"url": "https://dotenvx.com" "url": "https://dotenvx.com"
} }
}, },
"node_modules/fluent-ffmpeg": {
"version": "2.1.3",
"resolved": "https://registry.npmjs.org/fluent-ffmpeg/-/fluent-ffmpeg-2.1.3.tgz",
"integrity": "sha512-Be3narBNt2s6bsaqP6Jzq91heDgOEaDCJAXcE3qcma/EJBSy5FB4cvO31XBInuAuKBx8Kptf8dkhjK0IOru39Q==",
"deprecated": "Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.",
"license": "MIT",
"dependencies": {
"async": "^0.2.9",
"which": "^1.1.1"
},
"engines": {
"node": ">=18"
}
},
"node_modules/isexe": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz",
"integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==",
"license": "ISC"
},
"node_modules/mime-db": { "node_modules/mime-db": {
"version": "1.54.0", "version": "1.54.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.54.0.tgz", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.54.0.tgz",
@@ -711,6 +737,18 @@
"integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==", "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==",
"license": "0BSD", "license": "0BSD",
"optional": true "optional": true
},
"node_modules/which": {
"version": "1.3.1",
"resolved": "https://registry.npmjs.org/which/-/which-1.3.1.tgz",
"integrity": "sha512-HxJdYWq1MTIQbJ3nw0cqssHoTNU267KlrDuGZ1WYlxDStUtKUhOaJmh112/TZmHxxUfuJqPXSOm7tDyas0OSIQ==",
"license": "ISC",
"dependencies": {
"isexe": "^2.0.0"
},
"bin": {
"which": "bin/which"
}
} }
} }
} }
+1
View File
@@ -21,6 +21,7 @@
"@immich/sdk": "^3.1.0", "@immich/sdk": "^3.1.0",
"colors": "^1.4.0", "colors": "^1.4.0",
"dotenv": "^17.4.2", "dotenv": "^17.4.2",
"fluent-ffmpeg": "^2.1.3",
"mime-types": "^3.0.2", "mime-types": "^3.0.2",
"sharp": "^0.35.3" "sharp": "^0.35.3"
} }
+40 -12
View File
@@ -10,6 +10,16 @@ const sharp = require("sharp")
const albumMap = new Map() const albumMap = new Map()
const assetsList = {} 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() { async function main() {
immich.init({ immich.init({
apiKey: process.env.API_KEY, apiKey: process.env.API_KEY,
@@ -17,7 +27,7 @@ async function main() {
}) })
const user = await immich.getMyUser() const user = await immich.getMyUser()
console.log(`Logged as: ${user.name.cyan.bold}`) console.log(`Logged as: ${user.name.cyan.bold}`)
if (process.env.COMPRESSION == "true") { if (process.env.IMG_COMPRESSION == "true") {
console.log("") console.log("")
console.log("Processing files compression...") console.log("Processing files compression...")
await processFilesCompression() await processFilesCompression()
@@ -43,17 +53,24 @@ async function main() {
async function processFilesCompression() { async function processFilesCompression() {
const files = await getFilesRecursive(process.env.UPLOAD_DIR) const files = await getFilesRecursive(process.env.UPLOAD_DIR)
for (const file of files) { for (const file of files) {
try { switch (getMediaType(file)) {
console.log(`Compressing file: ${file.yellow.bold}`) case "image":
const compressedFile = await sharp(file) try {
.toFormat(process.env.COMPRESSION_OUTPUT_FORMAT, console.log(`Compressing image: ${file.yellow.bold}`)
{ quality: parseInt(process.env.COMPRESSION_OUTPUT_QUALITY) || 80 } const compressedFile = await sharp(file)
) .toFormat(process.env.IMG_COMPRESSION_OUTPUT_FORMAT,
.toFile(changeExtension(file, process.env.COMPRESSION_OUTPUT_FORMAT)) { quality: parseInt(process.env.IMG_COMPRESSION_OUTPUT_QUALITY) || 80 }
await fs.promises.rm(file) )
} catch (error) { .toFile(changeExtension(file, process.env.IMG_COMPRESSION_OUTPUT_FORMAT))
console.log(`Error occured while compressing file: ${file.red.bold}`) await fs.promises.rm(file)
console.log(error) } 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) 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() main()