mirror of
https://github.com/immich-app/immich.git
synced 2026-03-23 14:39:52 +03:00
add schemas sync variants formatting initial implementation use existing db, wip move to separate folder fix table definitions wip wiring it up
95 lines
2.4 KiB
Swift
95 lines
2.4 KiB
Swift
import Photos
|
|
|
|
extension PHAsset {
|
|
func toPlatformAsset() -> PlatformAsset {
|
|
return PlatformAsset(
|
|
id: localIdentifier,
|
|
name: title,
|
|
type: Int64(mediaType.rawValue),
|
|
createdAt: creationDate.map { Int64($0.timeIntervalSince1970) },
|
|
updatedAt: modificationDate.map { Int64($0.timeIntervalSince1970) },
|
|
width: Int64(pixelWidth),
|
|
height: Int64(pixelHeight),
|
|
durationInSeconds: Int64(duration),
|
|
orientation: 0,
|
|
isFavorite: isFavorite
|
|
)
|
|
}
|
|
|
|
var title: String {
|
|
return filename ?? originalFilename ?? "<unknown>"
|
|
}
|
|
|
|
var filename: String? {
|
|
return value(forKey: "filename") as? String
|
|
}
|
|
|
|
// This method is expected to be slow as it goes through the asset resources to fetch the originalFilename
|
|
var originalFilename: String? {
|
|
return getResource()?.originalFilename
|
|
}
|
|
|
|
func getResource() -> PHAssetResource? {
|
|
let resources = PHAssetResource.assetResources(for: self)
|
|
|
|
let filteredResources = resources.filter { $0.isMediaResource && isValidResourceType($0.type) }
|
|
|
|
guard !filteredResources.isEmpty else {
|
|
return nil
|
|
}
|
|
|
|
if filteredResources.count == 1 {
|
|
return filteredResources.first
|
|
}
|
|
|
|
if let currentResource = filteredResources.first(where: { $0.isCurrent }) {
|
|
return currentResource
|
|
}
|
|
|
|
if let fullSizeResource = filteredResources.first(where: { isFullSizeResourceType($0.type) }) {
|
|
return fullSizeResource
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func getLivePhotoResource() -> PHAssetResource? {
|
|
let resources = PHAssetResource.assetResources(for: self)
|
|
|
|
var livePhotoResource: PHAssetResource?
|
|
for resource in resources {
|
|
if resource.type == .fullSizePairedVideo {
|
|
return resource
|
|
}
|
|
|
|
if resource.type == .pairedVideo {
|
|
livePhotoResource = resource
|
|
}
|
|
}
|
|
|
|
return livePhotoResource
|
|
}
|
|
|
|
private func isValidResourceType(_ type: PHAssetResourceType) -> Bool {
|
|
switch mediaType {
|
|
case .image:
|
|
return [.photo, .alternatePhoto, .fullSizePhoto].contains(type)
|
|
case .video:
|
|
return [.video, .fullSizeVideo, .fullSizePairedVideo].contains(type)
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
|
|
private func isFullSizeResourceType(_ type: PHAssetResourceType) -> Bool {
|
|
switch mediaType {
|
|
case .image:
|
|
return type == .fullSizePhoto
|
|
case .video:
|
|
return type == .fullSizeVideo
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
}
|