Generate video thumbnails

This commit is contained in:
Christoph Hagen
2025-01-25 22:14:31 +01:00
parent 200fdc813d
commit 06b4c1ed76
10 changed files with 254 additions and 54 deletions

View File

@ -26,6 +26,8 @@ final class Storage: ObservableObject {
private let tagsFolderName = "tags"
private let videoThumbnailFolderName = "thumbnails"
private let outputPathFileName = "outputPath.bin"
private let settingsDataFileName = "settings.json"
@ -238,7 +240,10 @@ final class Storage: ObservableObject {
guard contentScope.deleteFile(at: filePath(file: fileId)) else {
return false
}
return contentScope.deleteFile(at: fileInfoPath(file: fileId))
guard contentScope.deleteFile(at: fileInfoPath(file: fileId)) else {
return false
}
return contentScope.deleteFile(at: videoThumbnailPath(fileId))
}
/**
@ -248,7 +253,11 @@ final class Storage: ObservableObject {
guard let contentScope else {
return false
}
return contentScope.deleteFile(at: filePath(file: fileId))
guard contentScope.deleteFile(at: filePath(file: fileId)) else {
return false
}
// Delete video thumbnail, which may not exist (not generated / not a video)
return contentScope.deleteFile(at: videoThumbnailPath(fileId))
}
/**
@ -366,6 +375,39 @@ final class Storage: ObservableObject {
return contentScope.write(fileContent, to: path)
}
func with<T>(file fileId: String, perform operation: (URL) async -> T?) async -> T? {
guard let contentScope else { return nil }
let path = filePath(file: fileId)
return await contentScope.with(relativePath: path, perform: operation)
}
// MARK: Video thumbnails
func hasVideoThumbnail(for videoId: String) -> Bool {
guard let contentScope else { return false }
let path = videoThumbnailPath(videoId)
return contentScope.hasFile(at: path)
}
private func videoThumbnailPath(_ videoId: String) -> String {
guard !videoId.hasSuffix("jpg") else {
return videoThumbnailFolderName + "/" + videoId
}
return videoThumbnailFolderName + "/" + videoId + ".jpg"
}
func save(thumbnail: Data, for videoId: String) -> Bool {
guard let contentScope else { return false }
let path = videoThumbnailPath(videoId)
return contentScope.write(thumbnail, to: path)
}
func getVideoThumbnail(for videoId: String) -> Data? {
guard let contentScope else { return nil }
let path = videoThumbnailPath(videoId)
return contentScope.readData(at: path)
}
// MARK: Settings
func loadSettings() -> Settings.Data? {