mirror of
https://github.com/immich-app/immich.git
synced 2026-05-18 03:10:24 +03:00
upload asset button
This commit is contained in:
@@ -363,6 +363,7 @@ protocol NativeSyncApi {
|
||||
func getAssetsCountSince(albumId: String, timestamp: Int64) throws -> Int64
|
||||
func getAssetsForAlbum(albumId: String, updatedTimeCond: Int64?) throws -> [PlatformAsset]
|
||||
func hashAssets(assetIds: [String], allowNetworkAccess: Bool, completion: @escaping (Result<[HashResult], Error>) -> Void)
|
||||
func uploadAsset(completion: @escaping (Result<Bool, Error>) -> Void)
|
||||
func cancelHashing() throws
|
||||
}
|
||||
|
||||
@@ -519,6 +520,21 @@ class NativeSyncApiSetup {
|
||||
} else {
|
||||
hashAssetsChannel.setMessageHandler(nil)
|
||||
}
|
||||
let uploadAssetChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.immich_mobile.NativeSyncApi.uploadAsset\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
uploadAssetChannel.setMessageHandler { _, reply in
|
||||
api.uploadAsset { result in
|
||||
switch result {
|
||||
case .success(let res):
|
||||
reply(wrapResult(res))
|
||||
case .failure(let error):
|
||||
reply(wrapError(error))
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
uploadAssetChannel.setMessageHandler(nil)
|
||||
}
|
||||
let cancelHashingChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.immich_mobile.NativeSyncApi.cancelHashing\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
|
||||
if let api = api {
|
||||
cancelHashingChannel.setMessageHandler { _, reply in
|
||||
|
||||
@@ -363,4 +363,38 @@ class NativeSyncApiImpl: NativeSyncApi {
|
||||
PHAssetResourceManager.default().cancelDataRequest(requestId)
|
||||
})
|
||||
}
|
||||
|
||||
func uploadAsset(completion: @escaping (Result<Bool, Error>) -> Void) {
|
||||
let bufferSize = 200 * 1024 * 1024
|
||||
var buffer = Data(count: bufferSize)
|
||||
buffer.withUnsafeMutableBytes { bufferPointer in
|
||||
arc4random_buf(bufferPointer.baseAddress!, bufferSize)
|
||||
}
|
||||
var hasher = Insecure.SHA1()
|
||||
hasher.update(data: buffer)
|
||||
let checksum = Data(hasher.finalize()).base64EncodedString()
|
||||
let tempDirectory = FileManager.default.temporaryDirectory
|
||||
|
||||
let tempFileURL = tempDirectory.appendingPathComponent("buffer.tmp")
|
||||
do {
|
||||
try buffer.write(to: tempFileURL)
|
||||
print("File saved to: \(tempFileURL.path)")
|
||||
} catch {
|
||||
print("Error writing file: \(error)")
|
||||
return completion(Result.failure(error))
|
||||
}
|
||||
|
||||
let config = URLSessionConfiguration.background(withIdentifier: "app.mertalev.immich.upload")
|
||||
let session = URLSession(configuration: config)
|
||||
|
||||
var request = URLRequest(url: URL(string: "https://<hardcoded-host>/api/upload")!)
|
||||
request.httpMethod = "POST"
|
||||
request.setValue("<hardcoded-api-key>", forHTTPHeaderField: "X-Api-Key")
|
||||
request.setValue("filename=\"test-image.jpg\", device-asset-id=\"rufh\", device-id=\"test\", file-created-at=\"2025-01-02T00:00:00.000Z\", file-modified-at=\"2025-01-01T00:00:00.000Z\", is-favorite, icloud-id=\"example-icloud-id\"", forHTTPHeaderField: "X-Immich-Asset-Data")
|
||||
request.setValue("sha=:\(checksum):", forHTTPHeaderField: "Repr-Digest")
|
||||
|
||||
let task = session.uploadTask(with: request, fromFile: tempFileURL)
|
||||
task.resume()
|
||||
completion(Result.success(true))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user