This commit is contained in:
sockenklaus
2025-07-08 01:16:48 +02:00
commit 5386e218ff
3 changed files with 88 additions and 0 deletions

Binary file not shown.

Binary file not shown.

88
getAndReplacecAsset.js Normal file
View File

@@ -0,0 +1,88 @@
import fs from "fs"
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_10mb.mp4",
newFile: "/home/socrates/Entwicklung/immich-api-test/javascript/SampleVideo_1280x720_30mb.mp4"
}
const originalPath = args.tdarrPath.replace(settings.immichMount, "")
const headers = {
"Accept": "application/json",
"x-api-key": settings.apiKey
}
const searchRequest = new Request(settings.apiBaseUrl + "search/metadata", {
method: "POST",
body: JSON.stringify({ originalPath: originalPath }),
headers: headers
})
fetch(searchRequest)
.then(response => {
if(!response.ok){
throw new Error(`Search failed: ${response.status} - ${response.statusText}`)
}
return response.json()
})
.then(body => {
console.log(JSON.stringify(body, undefined, 2))
if (body.assets.total !== 1) {
console.log(`Found ${body.assets.total} assets, stopping here`)
return Promise.resolve(null)
}
const id = body.assets.items[0].id
const file = fs.readFileSync(args.newFile)
const uploadData = new FormData()
uploadData.set("assetData", file)
uploadData.set("deviceAssetId", body.assets.items[0].deviceAssetId)
uploadData.set("deviceId", body.assets.items[0].deviceId)
uploadData.set("fileCreatedAt", body.assets.items[0].fileCreatedAt)
uploadData.set("fileModifiedAt", body.assets.items[0].fileModifiedAt)
//console.dir(uploadData.get("assetData"))
const replaceRequest = new Request(settings.apiBaseUrl + "assets/"+id+"/original", {
method: "PUT",
headers: {
"Accept": "application/json",
"x-api-key": settings.apiKey
},
body: uploadData
})
return fetch(replaceRequest)
})
.then(putResponse => {
if(!putResponse) {
throw new Error(`No put request was made`)
}
if(!putResponse.ok) {
throw new Error(`Replace failed: ${putResponse.status} - ${putResponse.statusText}`)
}
return putResponse.json()
})
.then(replaceBody => {
console.log(JSON.stringify(replaceBody, undefined, 2))
})
.catch(error => console.log(error))
//await fetch(replaceRequest)
// .then(response => response.json())