mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2025-07-12 15:04:43 +02:00
feat: Populate LocalFile with real info
This commit is contained in:
parent
db7a8cef77
commit
fec1ec554b
4 changed files with 38 additions and 13 deletions
|
@ -58,7 +58,7 @@ public class AbsDownloader: CAPPlugin {
|
|||
let localUrl = itemDirectory.appendingPathComponent("\(filename)")
|
||||
|
||||
downloadTrack(serverUrl: serverUrl, localUrl: localUrl)
|
||||
return LocalFile(filename: filename, localUrl: localUrl)
|
||||
return LocalFile(filename, track.mimeType, localUrl)
|
||||
}
|
||||
|
||||
private func urlForTrack(item: LibraryItem, track: AudioTrack) -> URL {
|
||||
|
|
|
@ -120,9 +120,7 @@ class LocalFile: Object, Encodable {
|
|||
@Persisted var id: String = UUID().uuidString
|
||||
@Persisted var filename: String? = ""
|
||||
@Persisted var contentUrl: String
|
||||
@Persisted var basePath: String
|
||||
@Persisted var absolutePath: String
|
||||
@Persisted var simplePath: String
|
||||
@Persisted var mimeType: String? = ""
|
||||
@Persisted var size: Int64
|
||||
}
|
||||
|
|
|
@ -337,9 +337,7 @@ extension LocalFile {
|
|||
case id
|
||||
case filename
|
||||
case contentUrl
|
||||
case basePath
|
||||
case absolutePath
|
||||
case simplePath
|
||||
case mimeType
|
||||
case size
|
||||
}
|
||||
|
@ -349,22 +347,28 @@ extension LocalFile {
|
|||
try container.encode(id, forKey: .id)
|
||||
try container.encode(filename, forKey: .filename)
|
||||
try container.encode(contentUrl, forKey: .contentUrl)
|
||||
try container.encode(basePath, forKey: .basePath)
|
||||
try container.encode(absolutePath, forKey: .absolutePath)
|
||||
try container.encode(simplePath, forKey: .simplePath)
|
||||
try container.encode(mimeType, forKey: .mimeType)
|
||||
try container.encode(size, forKey: .size)
|
||||
}
|
||||
|
||||
convenience init(filename: String, localUrl: URL) {
|
||||
convenience init(_ filename: String, _ mimeType: String, _ localUrl: URL) {
|
||||
self.init()
|
||||
self.id = localUrl.absoluteString.toBase64()
|
||||
self.filename = filename
|
||||
self.contentUrl = localUrl.absoluteString
|
||||
// TODO: self.baseUrl
|
||||
self.absolutePath = localUrl.absoluteString
|
||||
self.simplePath = localUrl.path
|
||||
// TODO: self.mimeType
|
||||
// TODO: self.size
|
||||
self.absolutePath = localUrl.path
|
||||
self.size = localUrl.fileSize
|
||||
}
|
||||
|
||||
func isAudioFile() -> Bool {
|
||||
switch self.mimeType {
|
||||
case "application/octet-stream",
|
||||
"video/mp4":
|
||||
return true
|
||||
default:
|
||||
return self.mimeType?.starts(with: "audio") ?? false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -42,3 +42,26 @@ extension DispatchQueue {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension URL {
|
||||
var attributes: [FileAttributeKey : Any]? {
|
||||
do {
|
||||
return try FileManager.default.attributesOfItem(atPath: path)
|
||||
} catch let error as NSError {
|
||||
print("FileAttribute error: \(error)")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var fileSize: Int64 {
|
||||
return attributes?[.size] as? Int64 ?? Int64(0)
|
||||
}
|
||||
|
||||
var fileSizeString: String {
|
||||
return ByteCountFormatter.string(fromByteCount: Int64(fileSize), countStyle: .file)
|
||||
}
|
||||
|
||||
var creationDate: Date? {
|
||||
return attributes?[.creationDate] as? Date
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue