89 lines
2.7 KiB
JavaScript
89 lines
2.7 KiB
JavaScript
import fs from "node:fs"
|
|
import fetch, { FormData, Request, fileFromSync } from 'node-fetch'
|
|
import axios from 'axios'
|
|
|
|
const apiKeySockenklaus = "bxmlNuUlPiY4YRRIcRuJ5eflZCU7aOHQPOgGmts4f8"
|
|
const apiKeyTestUser = "xOUY572Rd6Fw3qoE3EFBFy3G3sITaIQsgPPc8TAbQ"
|
|
|
|
const settings = {
|
|
apiBaseUrl: "https://immich.sockenklaus.duckdns.org/api/",
|
|
apiKey: apiKeyTestUser,
|
|
immichMount: "/immich/"
|
|
|
|
}
|
|
|
|
const args = {
|
|
tdarrPath: "/immich/6c0138fb-62c4-4a99-aeed-721a845f90f3/2025/07/SampleVideo_1280x720_30mb.mp4",
|
|
newFile: "./SampleVideo_1280x720_10mb.mp4"
|
|
}
|
|
|
|
const originalPath = args.tdarrPath.replace(settings.immichMount, "")
|
|
|
|
const headers = {
|
|
"Accept": "application/json",
|
|
"x-api-key": settings.apiKey
|
|
}
|
|
|
|
async function getAssetByOriginalPath(path) {
|
|
const searchRequest = new Request(settings.apiBaseUrl + "search/metadata", {
|
|
method: "POST",
|
|
body: JSON.stringify({ originalPath: path }),
|
|
headers: headers
|
|
})
|
|
|
|
try {
|
|
const responsePromise = await fetch(searchRequest)
|
|
const responseBody = await responsePromise.json()
|
|
|
|
if(responseBody.assets.total !== 1) {
|
|
console.error(`Found ${body.assets.total} assets, stopping here`)
|
|
return {}
|
|
}
|
|
|
|
return {
|
|
id: responseBody.assets.items[0].id,
|
|
deviceAssetId: responseBody.assets.items[0].deviceAssetId,
|
|
deviceId: responseBody.assets.items[0].deviceId,
|
|
fileCreatedAt: responseBody.assets.items[0].fileCreatedAt,
|
|
fileModifiedAt: responseBody.assets.items[0].fileModifiedAt
|
|
}
|
|
}
|
|
catch(error) {
|
|
console.error(error)
|
|
}
|
|
}
|
|
|
|
async function replaceAsset(assetInfo, newFilePath) {
|
|
const file = fileFromSync(newFilePath)
|
|
|
|
const fileName = newFilePath.replace(/^.*[\\/]/, '')
|
|
|
|
const uploadData = new FormData()
|
|
uploadData.set("assetData", file, fileName)
|
|
uploadData.set("deviceAssetId", assetInfo.deviceAssetId)
|
|
uploadData.set("deviceId", assetInfo.deviceId)
|
|
uploadData.set("fileCreatedAt", assetInfo.fileCreatedAt)
|
|
uploadData.set("fileModifiedAt", assetInfo.fileModifiedAt)
|
|
|
|
console.log(uploadData)
|
|
|
|
const replaceRequest = new Request(settings.apiBaseUrl + "assets/"+assetInfo.id+"/original", {
|
|
method: "PUT",
|
|
headers: {
|
|
"Accept": "application/json",
|
|
//"Content-Type": "multipart/form-data",
|
|
"x-api-key": settings.apiKey
|
|
},
|
|
body: uploadData
|
|
})
|
|
const responsePromise = await fetch(replaceRequest)
|
|
const responseBody = await responsePromise.json()
|
|
|
|
return responseBody
|
|
}
|
|
|
|
const assetInfo = await getAssetByOriginalPath(originalPath)
|
|
|
|
console.log(JSON.stringify(assetInfo, undefined, 2))
|
|
|
|
console.log(await replaceAsset(assetInfo, args.newFile)) |