2023-04-04 11:37:30 +03:00
/*
* Copyright ( C ) 2023 by Claudio Cambra < claudio . cambra @ nextcloud . com >
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 2 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful , but
* WITHOUT ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE . See the GNU General Public License
* for more details .
*/
import Foundation
import OSLog
2024-01-22 09:40:47 +03:00
import RealmSwift
2023-04-04 11:37:30 +03:00
extension NextcloudFilesDatabaseManager {
func localFileMetadataFromOcId ( _ ocId : String ) -> NextcloudLocalFileMetadataTable ? {
2024-01-22 09:40:47 +03:00
if let metadata = ncDatabase ( ) . objects ( NextcloudLocalFileMetadataTable . self ) . filter (
" ocId == %@ " , ocId
) . first {
2023-04-04 11:37:30 +03:00
return NextcloudLocalFileMetadataTable ( value : metadata )
}
return nil
}
func addLocalFileMetadataFromItemMetadata ( _ itemMetadata : NextcloudItemMetadataTable ) {
let database = ncDatabase ( )
do {
try database . write {
let newLocalFileMetadata = NextcloudLocalFileMetadataTable ( )
newLocalFileMetadata . ocId = itemMetadata . ocId
newLocalFileMetadata . fileName = itemMetadata . fileName
newLocalFileMetadata . account = itemMetadata . account
newLocalFileMetadata . etag = itemMetadata . etag
newLocalFileMetadata . exifDate = Date ( )
newLocalFileMetadata . exifLatitude = " -1 "
newLocalFileMetadata . exifLongitude = " -1 "
database . add ( newLocalFileMetadata , update : . all )
2024-01-22 09:40:47 +03:00
Logger . ncFilesDatabase . debug (
" Added local file metadata from item metadata. ocID: \( itemMetadata . ocId , privacy : . public ) , etag: \( itemMetadata . etag , privacy : . public ) , fileName: \( itemMetadata . fileName , privacy : . public ) "
)
2023-04-04 11:37:30 +03:00
}
2024-01-22 09:40:47 +03:00
} catch {
Logger . ncFilesDatabase . error (
" Could not add local file metadata from item metadata. ocID: \( itemMetadata . ocId , privacy : . public ) , etag: \( itemMetadata . etag , privacy : . public ) , fileName: \( itemMetadata . fileName , privacy : . public ) , received error: \( error . localizedDescription , privacy : . public ) "
)
2023-04-04 11:37:30 +03:00
}
}
func deleteLocalFileMetadata ( ocId : String ) {
let database = ncDatabase ( )
do {
try database . write {
2024-01-22 09:40:47 +03:00
let results = database . objects ( NextcloudLocalFileMetadataTable . self ) . filter (
" ocId == %@ " , ocId )
2023-04-04 11:37:30 +03:00
database . delete ( results )
}
2024-01-22 09:40:47 +03:00
} catch {
Logger . ncFilesDatabase . error (
" Could not delete local file metadata with ocId: \( ocId , privacy : . public ) , received error: \( error . localizedDescription , privacy : . public ) "
)
2023-04-04 11:37:30 +03:00
}
}
2024-01-22 09:40:47 +03:00
private func sortedLocalFileMetadatas ( _ metadatas : Results < NextcloudLocalFileMetadataTable > )
-> [ NextcloudLocalFileMetadataTable ]
{
2023-04-04 11:37:30 +03:00
let sortedMetadatas = metadatas . sorted ( byKeyPath : " fileName " , ascending : true )
return Array ( sortedMetadatas . map { NextcloudLocalFileMetadataTable ( value : $0 ) } )
}
func localFileMetadatas ( account : String ) -> [ NextcloudLocalFileMetadataTable ] {
2024-01-22 09:40:47 +03:00
let results = ncDatabase ( ) . objects ( NextcloudLocalFileMetadataTable . self ) . filter (
" account == %@ " , account )
2023-04-04 11:37:30 +03:00
return sortedLocalFileMetadatas ( results )
}
func localFileItemMetadatas ( account : String ) -> [ NextcloudItemMetadataTable ] {
let localFileMetadatas = localFileMetadatas ( account : account )
2024-01-22 09:40:47 +03:00
let localFileMetadatasOcIds = Array ( localFileMetadatas . map ( \ . ocId ) )
2023-04-04 11:37:30 +03:00
var itemMetadatas : [ NextcloudItemMetadataTable ] = [ ]
for ocId in localFileMetadatasOcIds {
guard let itemMetadata = itemMetadataFromOcId ( ocId ) else {
2024-01-22 09:40:47 +03:00
Logger . ncFilesDatabase . error (
" Could not find matching item metadata for local file metadata with ocId: \( ocId , privacy : . public ) with request from account: \( account ) "
)
continue
2023-04-04 11:37:30 +03:00
}
itemMetadatas . append ( NextcloudItemMetadataTable ( value : itemMetadata ) )
}
return itemMetadatas
}
}