more stuff
This commit is contained in:
@@ -1,15 +1,6 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
import fetch, { FormData, Request, fileFromSync } from 'node-fetch'
|
||||
import axios from 'axios'
|
||||
|
||||
const apiKeySockenklaus = "bxmlNuUlPiY4YRRIcRuJ5eflZCU7aOHQPOgGmts4f8"
|
||||
const apiKeyTestUser = "xOUY572Rd6Fw3qoE3EFBFy3G3sITaIQsgPPc8TAbQ"
|
||||
@@ -22,8 +13,8 @@ const settings = {
|
||||
}
|
||||
|
||||
const args = {
|
||||
tdarrPath: "/immich/6c0138fb-62c4-4a99-aeed-721a845f90f3/2025/07/SampleVideo_1280x720_10mb.mp4",
|
||||
newFile: "./SampleVideo_1280x720_30mb.mp4"
|
||||
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, "")
|
||||
@@ -63,20 +54,24 @@ async function getAssetByOriginalPath(path) {
|
||||
}
|
||||
|
||||
async function replaceAsset(assetInfo, newFilePath) {
|
||||
//const file = await fs.createReadStream(newFilePath)
|
||||
const file = await fs.readFile(newFilePath)
|
||||
const file = fileFromSync(newFilePath)
|
||||
|
||||
const fileName = newFilePath.replace(/^.*[\\/]/, '')
|
||||
|
||||
const uploadData = new FormData()
|
||||
uploadData.set("assetData", file)
|
||||
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
|
||||
@@ -91,63 +86,4 @@ 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){
|
||||
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())
|
||||
console.log(await replaceAsset(assetInfo, args.newFile))
|
||||
Reference in New Issue
Block a user