Introduce initial project scaffold: Express webhook server (server.js) for Immich uploads, DB pool (modules/database.js) using pg, immich repository (repositories/immichRepo.js) with get/update functions, image processing via sharp (modules/image_processor.js), a small execution helper (modules/execution_helper.js), and package.json + package-lock. Server validates a bearer token, filters non-image assets, waits briefly, compresses the image and updates DB path. Requires env vars: DB_HOST, DB_USER, DB_PORT, DB_NAME, DB_PASS, PASS_CODE, IMMICH_DATA_PATH, IMG_COMPRESSION_OUTPUT_FORMAT, IMG_COMPRESSION_OUTPUT_QUALITY, PORT.
44 lines
1.3 KiB
JavaScript
44 lines
1.3 KiB
JavaScript
import path from "node:path"
|
|
import fs from "node:fs/promises"
|
|
import sharp from "sharp"
|
|
|
|
const IMAGE_EXTENSIONS = new Set([ ".jpg", ".jpeg", ".png", ".heic", ".heif", ".gif", ".tiff", ".bmp", ".svg", ".avif", ".raw", ".dng" ])
|
|
const VIDEO_EXTENSIONS = new Set([ ".mp4", ".mov", ".avi", ".mkv", ".webm", ".flv", ".wmv", ".m4v", ".3gp", ".ts" ])
|
|
|
|
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"
|
|
}
|
|
|
|
function changeExtension(filePath, newExt) {
|
|
const formattedExt = newExt.startsWith(".") ? newExt : `.${newExt}`
|
|
|
|
const parsed = path.parse(filePath)
|
|
parsed.base = `${parsed.name}${formattedExt}`
|
|
parsed.ext = formattedExt
|
|
|
|
return path.format(parsed)
|
|
}
|
|
|
|
async function processCompression($file) {
|
|
const file = $file.replace("/data", process.env.IMMICH_DATA_PATH)
|
|
const newFilePath = changeExtension(file, process.env.IMG_COMPRESSION_OUTPUT_FORMAT)
|
|
const compressedFile = await sharp(file)
|
|
.toFormat(process.env.IMG_COMPRESSION_OUTPUT_FORMAT,
|
|
{ quality: parseInt(process.env.IMG_COMPRESSION_OUTPUT_QUALITY) || 80 }
|
|
)
|
|
.toFile(newFilePath)
|
|
}
|
|
|
|
export default {
|
|
getMediaType,
|
|
changeExtension,
|
|
processCompression
|
|
}
|