This commit is contained in:
Sockenklaus
2025-07-08 12:26:17 +02:00
parent 5386e218ff
commit 8f6d7bc570
85 changed files with 8744 additions and 8 deletions

View File

@@ -1,5 +1,15 @@
import fs from "fs"
import fs from "node:fs"
import { basename } from "node:path"
class UploadFile extends File {
constructor(filepath) {
super([], basename(filepath))
}
stream() {
return fs.createReadStream(this.filepath)
}
}
const apiKeySockenklaus = "bxmlNuUlPiY4YRRIcRuJ5eflZCU7aOHQPOgGmts4f8"
const apiKeyTestUser = "xOUY572Rd6Fw3qoE3EFBFy3G3sITaIQsgPPc8TAbQ"
@@ -13,7 +23,7 @@ const settings = {
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"
newFile: "./SampleVideo_1280x720_30mb.mp4"
}
const originalPath = args.tdarrPath.replace(settings.immichMount, "")
@@ -23,12 +33,67 @@ const headers = {
"x-api-key": settings.apiKey
}
const searchRequest = new Request(settings.apiBaseUrl + "search/metadata", {
method: "POST",
body: JSON.stringify({ originalPath: originalPath }),
headers: headers
})
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 = await fs.createReadStream(newFilePath)
const file = await fs.readFile(newFilePath)
const uploadData = new FormData()
uploadData.set("assetData", file)
uploadData.set("deviceAssetId", assetInfo.deviceAssetId)
uploadData.set("deviceId", assetInfo.deviceId)
uploadData.set("fileCreatedAt", assetInfo.fileCreatedAt)
uploadData.set("fileModifiedAt", assetInfo.fileModifiedAt)
const replaceRequest = new Request(settings.apiBaseUrl + "assets/"+assetInfo.id+"/original", {
method: "PUT",
headers: {
"Accept": "application/json",
"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))
/*
fetch(searchRequest)
.then(response => {
if(!response.ok){
@@ -83,6 +148,6 @@ fetch(searchRequest)
.catch(error => console.log(error))
*/
//await fetch(replaceRequest)
// .then(response => response.json())