commit 5386e218ff56691130e18fdec52894530a0c87e5 Author: sockenklaus Date: Tue Jul 8 01:16:48 2025 +0200 first diff --git a/SampleVideo_1280x720_10mb.mp4 b/SampleVideo_1280x720_10mb.mp4 new file mode 100644 index 0000000..3ce062e Binary files /dev/null and b/SampleVideo_1280x720_10mb.mp4 differ diff --git a/SampleVideo_1280x720_30mb.mp4 b/SampleVideo_1280x720_30mb.mp4 new file mode 100644 index 0000000..f29424e Binary files /dev/null and b/SampleVideo_1280x720_30mb.mp4 differ diff --git a/getAndReplacecAsset.js b/getAndReplacecAsset.js new file mode 100644 index 0000000..29de242 --- /dev/null +++ b/getAndReplacecAsset.js @@ -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()) \ No newline at end of file