2023-01-05 22:59:50 +03:00
/*
* Copyright ( C ) 2022 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 RealmSwift
2023-01-10 20:36:10 +03:00
import FileProvider
2023-01-13 16:53:40 +03:00
import NextcloudKit
2023-01-05 22:59:50 +03:00
class NextcloudFilesDatabaseManager : NSObject {
static let shared = {
return NextcloudFilesDatabaseManager ( ) ;
} ( )
2023-01-27 01:10:43 +03:00
let relativeDatabaseFolderPath = " Database/ "
let databaseFilename = " fileproviderextdatabase.realm "
2023-01-05 22:59:50 +03:00
let relativeDatabaseFilePath : String
var databasePath : URL ?
let schemaVersion : UInt64 = 100
override init ( ) {
self . relativeDatabaseFilePath = self . relativeDatabaseFolderPath + self . databaseFilename
2023-01-12 18:51:44 +03:00
guard let fileProviderDataDirUrl = pathForFileProviderExtData ( ) else {
2023-01-05 22:59:50 +03:00
super . init ( )
return
}
2023-01-27 01:10:43 +03:00
self . databasePath = fileProviderDataDirUrl . appendingPathComponent ( self . relativeDatabaseFilePath )
2023-01-05 22:59:50 +03:00
// D i s a b l e f i l e p r o t e c t i o n f o r d i r e c t o r y D B
// h t t p s : / / d o c s . m o n g o d b . c o m / r e a l m / s d k / i o s / e x a m p l e s / c o n f i g u r e - a n d - o p e n - a - r e a l m / # s t d - l a b e l - i o s - o p e n - a - l o c a l - r e a l m
2023-01-27 01:10:43 +03:00
let dbFolder = fileProviderDataDirUrl . appendingPathComponent ( self . relativeDatabaseFolderPath )
let dbFolderPath = dbFolder . path
2023-01-12 18:51:44 +03:00
do {
2023-01-27 01:10:43 +03:00
try FileManager . default . createDirectory ( at : dbFolder , withIntermediateDirectories : true )
2023-01-30 22:29:15 +03:00
try FileManager . default . setAttributes ( [ FileAttributeKey . protectionKey : FileProtectionType . completeUntilFirstUserAuthentication ] , ofItemAtPath : dbFolderPath )
} catch let error {
NSLog ( " Could not set permission level for File Provider database folder, received error: %@ " , error . localizedDescription )
}
2023-01-05 22:59:50 +03:00
let config = Realm . Configuration (
fileURL : self . databasePath ,
schemaVersion : self . schemaVersion ,
2023-01-27 01:08:16 +03:00
objectTypes : [ NextcloudItemMetadataTable . self , NextcloudDirectoryMetadataTable . self , NextcloudLocalFileMetadataTable . self ]
2023-01-05 22:59:50 +03:00
)
Realm . Configuration . defaultConfiguration = config
do {
let realm = try Realm ( )
2023-01-26 22:50:10 +03:00
NSLog ( " Successfully started Realm db for FileProviderExt " )
2023-01-05 22:59:50 +03:00
} catch let error as NSError {
2023-01-26 22:50:10 +03:00
NSLog ( " Error opening Realm db: %@ " , error . localizedDescription )
2023-01-05 22:59:50 +03:00
}
super . init ( )
}
2023-01-10 19:58:00 +03:00
2023-01-10 21:23:33 +03:00
private func ncDatabase ( ) -> Realm {
2023-01-10 19:58:00 +03:00
let realm = try ! Realm ( )
realm . refresh ( )
2023-01-10 21:23:33 +03:00
return realm
2023-01-10 19:58:00 +03:00
}
2023-01-10 20:36:10 +03:00
2023-02-01 20:16:46 +03:00
func anyItemMetadatasForAccount ( _ account : String ) -> Bool {
return ! ncDatabase ( ) . objects ( NextcloudItemMetadataTable . self ) . filter ( " account == %@ " , account ) . isEmpty
}
2023-01-12 23:16:49 +03:00
func itemMetadataFromOcId ( _ ocId : String ) -> NextcloudItemMetadataTable ? {
2023-01-26 20:47:34 +03:00
// R e a l m o b j e c t s a r e l i v e - f i r e , i . e . t h e y w i l l b e c h a n g e d a n d i n v a l i d a t e d a c c o r d i n g t o c h a n g e s i n t h e d b
// L e t ' s t h e r e f o r e c r e a t e a c o p y
if let itemMetadata = ncDatabase ( ) . objects ( NextcloudItemMetadataTable . self ) . filter ( " ocId == %@ " , ocId ) . first {
return NextcloudItemMetadataTable ( value : itemMetadata )
}
return nil
2023-01-10 21:23:33 +03:00
}
2023-01-23 19:25:00 +03:00
private func sortedItemMetadatas ( _ metadatas : Results < NextcloudItemMetadataTable > ) -> [ NextcloudItemMetadataTable ] {
2023-01-13 16:17:04 +03:00
let sortedMetadatas = metadatas . sorted ( byKeyPath : " fileName " , ascending : true )
2023-01-26 20:47:34 +03:00
return Array ( sortedMetadatas . map { NextcloudItemMetadataTable ( value : $0 ) } )
2023-01-13 16:17:04 +03:00
}
2023-01-23 19:25:00 +03:00
func itemMetadatas ( account : String , serverUrl : String ) -> [ NextcloudItemMetadataTable ] {
let metadatas = ncDatabase ( ) . objects ( NextcloudItemMetadataTable . self ) . filter ( " account == %@ AND serverUrl == %@ " , account , serverUrl )
return sortedItemMetadatas ( metadatas )
}
func itemMetadatas ( account : String , serverUrl : String , status : NextcloudItemMetadataTable . Status ) -> [ NextcloudItemMetadataTable ] {
2023-01-30 22:27:37 +03:00
let metadatas = ncDatabase ( ) . objects ( NextcloudItemMetadataTable . self ) . filter ( " account == %@ AND serverUrl == %@ AND status == %@ " , account , serverUrl , status . rawValue )
2023-01-23 19:25:00 +03:00
return sortedItemMetadatas ( metadatas )
}
2023-01-12 23:16:49 +03:00
func itemMetadataFromFileProviderItemIdentifier ( _ identifier : NSFileProviderItemIdentifier ) -> NextcloudItemMetadataTable ? {
2023-01-10 20:36:10 +03:00
let ocId = identifier . rawValue
2023-01-12 23:16:49 +03:00
return itemMetadataFromOcId ( ocId )
2023-01-10 20:36:10 +03:00
}
2023-01-10 22:24:34 +03:00
2023-01-25 22:18:51 +03:00
private func processItemMetadatasToDelete ( databaseToWriteTo : Realm ,
2023-02-02 22:12:03 +03:00
existingMetadatas : Results < NextcloudItemMetadataTable > ,
2023-01-25 22:18:51 +03:00
updatedMetadatas : [ NextcloudItemMetadataTable ] ) {
assert ( databaseToWriteTo . isInWriteTransaction )
for existingMetadata in existingMetadatas {
guard ! updatedMetadatas . contains ( where : { $0 . ocId = = existingMetadata . ocId } ) ,
let metadataToDelete = itemMetadataFromOcId ( existingMetadata . ocId ) else { continue }
2023-01-26 22:50:10 +03:00
NSLog ( " " "
2023-01-25 22:18:51 +03:00
Deleting metadata .
ocID : % @ ,
fileName : % @ ,
etag : % @
" " "
, metadataToDelete . ocId , metadataToDelete . fileName , metadataToDelete . etag )
2023-02-02 22:12:03 +03:00
// C a n ' t p a s s c o p i e s , w e n e e d t h e o r i g i n a l s f r o m t h e d a t a b a s e
databaseToWriteTo . delete ( ncDatabase ( ) . objects ( NextcloudItemMetadataTable . self ) . filter ( " ocId == %@ " , metadataToDelete . ocId ) )
2023-01-25 22:18:51 +03:00
}
}
2023-01-25 22:20:46 +03:00
private func processItemMetadatasToUpdate ( databaseToWriteTo : Realm ,
2023-02-02 22:12:03 +03:00
existingMetadatas : Results < NextcloudItemMetadataTable > ,
2023-01-25 22:20:46 +03:00
updatedMetadatas : [ NextcloudItemMetadataTable ] ) {
assert ( databaseToWriteTo . isInWriteTransaction )
for updatedMetadata in updatedMetadatas {
if let existingMetadata = existingMetadatas . first ( where : { $0 . ocId = = updatedMetadata . ocId } ) {
if existingMetadata . status = = NextcloudItemMetadataTable . Status . normal . rawValue &&
! existingMetadata . isInSameRemoteState ( updatedMetadata ) {
2023-01-26 20:47:34 +03:00
databaseToWriteTo . add ( NextcloudItemMetadataTable ( value : updatedMetadata ) , update : . all )
2023-01-26 22:50:10 +03:00
NSLog ( " " "
2023-01-25 22:20:46 +03:00
Updated existing metadata .
ocID : % @ ,
fileName : % @ ,
etag : % @
" " "
, updatedMetadata . ocId , updatedMetadata . fileName , updatedMetadata . etag )
}
// D o n ' t u p d a t e u n d e r o t h e r c i r c u m s t a n c e s i n w h i c h t h e m e t a d a t a a l r e a d y e x i s t s
} else { // T h i s i s a n e w m e t a d a t a
2023-01-26 20:47:34 +03:00
databaseToWriteTo . add ( NextcloudItemMetadataTable ( value : updatedMetadata ) , update : . all )
2023-01-26 22:50:10 +03:00
NSLog ( " " "
2023-01-25 22:20:46 +03:00
Created new metadata .
ocID : % @ ,
fileName : % @ ,
etag : % @
" " "
, updatedMetadata . ocId , updatedMetadata . fileName , updatedMetadata . etag )
}
}
}
2023-01-26 21:09:24 +03:00
func updateItemMetadatas ( account : String , serverUrl : String , updatedMetadatas : [ NextcloudItemMetadataTable ] ) {
2023-01-25 22:15:13 +03:00
let database = ncDatabase ( )
do {
try database . write {
2023-02-02 22:12:03 +03:00
let existingMetadatas = ncDatabase ( ) . objects ( NextcloudItemMetadataTable . self ) . filter ( " account == %@ AND serverUrl == %@ AND status == %@ " , account , serverUrl , NextcloudItemMetadataTable . Status . normal . rawValue )
2023-01-25 22:18:51 +03:00
processItemMetadatasToDelete ( databaseToWriteTo : database ,
existingMetadatas : existingMetadatas ,
updatedMetadatas : updatedMetadatas )
2023-01-25 22:20:46 +03:00
processItemMetadatasToUpdate ( databaseToWriteTo : database ,
existingMetadatas : existingMetadatas ,
updatedMetadatas : updatedMetadatas )
2023-01-25 22:15:13 +03:00
}
} catch let error {
2023-01-26 22:50:10 +03:00
NSLog ( " Could not update any metadatas, received error: %@ " , error . localizedDescription )
2023-01-25 22:15:13 +03:00
}
}
2023-02-14 15:48:08 +03:00
func setStatusForItemMetadata ( _ metadata : NextcloudItemMetadataTable , status : NextcloudItemMetadataTable . Status ) -> NextcloudItemMetadataTable ? {
let database = ncDatabase ( )
var result : NextcloudItemMetadataTable ?
do {
try database . write {
result = database . objects ( NextcloudItemMetadataTable . self ) . filter ( " ocId == %@ " , metadata . ocId ) . first
result ? . status = status . rawValue
}
} catch let error {
NSLog ( " Could not update status for item metadata with ocID: %@ and filename: %@, received error: %@ " , metadata . ocId , metadata . fileNameView , error . localizedDescription )
}
if result != nil {
return NextcloudItemMetadataTable ( value : result ! )
}
return nil
}
2023-02-16 16:44:16 +03:00
func addItemMetadata ( _ metadata : NextcloudItemMetadataTable ) {
let database = ncDatabase ( )
do {
try database . write {
database . add ( metadata , update : . all )
NSLog ( " " "
Created new metadata ( addItemMetadata ) .
ocID : % @ ,
fileName : % @ ,
etag : % @
" " "
, metadata . ocId , metadata . fileName , metadata . etag )
}
} catch let error {
NSLog ( " Could not add item metadata with ocID: %@ and filename: %@, received error: %@ " , metadata . ocId , metadata . fileNameView , error . localizedDescription )
}
}
2023-01-13 03:59:50 +03:00
func directoryMetadata ( account : String , serverUrl : String ) -> NextcloudDirectoryMetadataTable ? {
2023-01-26 20:47:34 +03:00
if let metadata = ncDatabase ( ) . objects ( NextcloudDirectoryMetadataTable . self ) . filter ( " account == %@ AND serverUrl == %@ " , account , serverUrl ) . first {
return NextcloudDirectoryMetadataTable ( value : metadata )
}
return nil
2023-01-13 03:59:50 +03:00
}
2023-01-26 18:07:46 +03:00
func directoryMetadata ( ocId : String ) -> NextcloudDirectoryMetadataTable ? {
2023-01-26 20:47:34 +03:00
if let metadata = ncDatabase ( ) . objects ( NextcloudDirectoryMetadataTable . self ) . filter ( " ocId == %@ " , ocId ) . first {
return NextcloudDirectoryMetadataTable ( value : metadata )
}
return nil
2023-01-26 18:07:46 +03:00
}
2023-01-12 23:16:49 +03:00
func parentDirectoryMetadataForItem ( _ itemMetadata : NextcloudItemMetadataTable ) -> NextcloudDirectoryMetadataTable ? {
2023-01-13 03:59:50 +03:00
return directoryMetadata ( account : itemMetadata . account , serverUrl : itemMetadata . serverUrl )
2023-01-10 22:24:34 +03:00
}
2023-01-12 23:36:51 +03:00
2023-01-26 20:54:52 +03:00
func directoryMetadatas ( account : String , parentDirectoryServerUrl : String ) -> [ NextcloudDirectoryMetadataTable ] {
let metadatas = ncDatabase ( ) . objects ( NextcloudDirectoryMetadataTable . self ) . filter ( " account == %@ AND parentDirectoryServerUrl == %@ " , account , parentDirectoryServerUrl )
let sortedMetadatas = metadatas . sorted ( byKeyPath : " serverUrl " , ascending : true )
return Array ( sortedMetadatas . map { NextcloudDirectoryMetadataTable ( value : $0 ) } )
}
2023-01-26 20:09:19 +03:00
private func processDirectoryMetadatasToDelete ( databaseToWriteTo : Realm ,
2023-02-02 22:12:03 +03:00
existingDirectoryMetadatas : Results < NextcloudDirectoryMetadataTable > ,
2023-01-26 20:09:19 +03:00
updatedDirectoryMetadatas : [ NextcloudDirectoryMetadataTable ] ) {
assert ( databaseToWriteTo . isInWriteTransaction )
for existingMetadata in existingDirectoryMetadatas {
guard ! updatedDirectoryMetadatas . contains ( where : { $0 . ocId = = existingMetadata . ocId } ) ,
let metadataToDelete = directoryMetadata ( ocId : existingMetadata . ocId ) else { continue }
2023-01-26 22:50:10 +03:00
NSLog ( " " "
2023-01-26 20:09:19 +03:00
Deleting directory metadata .
ocID : % @ ,
serverUrl : % @ ,
etag : % @
" " "
, metadataToDelete . ocId , metadataToDelete . serverUrl , metadataToDelete . etag )
2023-02-02 22:12:03 +03:00
// C a n ' t p a s s c o p i e s , w e n e e d t h e o r i g i n a l s f r o m t h e d a t a b a s e
databaseToWriteTo . delete ( databaseToWriteTo . objects ( NextcloudDirectoryMetadataTable . self ) . filter ( " ocId == %@ " , metadataToDelete . ocId ) )
2023-01-26 20:09:19 +03:00
}
}
private func processDirectoryMetadatasToUpdate ( databaseToWriteTo : Realm ,
2023-02-02 22:12:03 +03:00
existingDirectoryMetadatas : Results < NextcloudDirectoryMetadataTable > ,
2023-01-26 20:09:19 +03:00
updatedDirectoryMetadatas : [ NextcloudDirectoryMetadataTable ] ) {
assert ( databaseToWriteTo . isInWriteTransaction )
for updatedMetadata in updatedDirectoryMetadatas {
if let existingMetadata = existingDirectoryMetadatas . first ( where : { $0 . ocId = = updatedMetadata . ocId } ) {
if ! existingMetadata . isInSameRemoteState ( updatedMetadata ) {
2023-01-26 20:47:34 +03:00
databaseToWriteTo . add ( NextcloudDirectoryMetadataTable ( value : updatedMetadata ) , update : . all )
2023-01-26 22:50:10 +03:00
NSLog ( " " "
2023-01-26 20:09:19 +03:00
Updated existing directory metadata .
ocID : % @ ,
serverUrl : % @ ,
etag : % @
" " "
, updatedMetadata . ocId , updatedMetadata . serverUrl , updatedMetadata . etag )
}
// D o n ' t u p d a t e u n d e r o t h e r c i r c u m s t a n c e s i n w h i c h t h e m e t a d a t a a l r e a d y e x i s t s
} else { // T h i s i s a n e w m e t a d a t a
2023-01-26 20:47:34 +03:00
databaseToWriteTo . add ( NextcloudDirectoryMetadataTable ( value : updatedMetadata ) , update : . all )
2023-01-26 22:50:10 +03:00
NSLog ( " " "
2023-01-26 20:09:19 +03:00
Created new metadata .
ocID : % @ ,
serverUrl : % @ ,
etag : % @
" " "
, updatedMetadata . ocId , updatedMetadata . serverUrl , updatedMetadata . etag )
}
}
}
2023-01-26 21:09:24 +03:00
func updateDirectoryMetadatas ( account : String , parentDirectoryServerUrl : String , updatedDirectoryMetadatas : [ NextcloudDirectoryMetadataTable ] ) {
2023-01-26 18:07:46 +03:00
let database = ncDatabase ( )
2023-01-26 20:09:19 +03:00
2023-01-26 18:07:46 +03:00
do {
try database . write {
2023-02-02 22:12:03 +03:00
let existingDirectoryMetadatas = ncDatabase ( ) . objects ( NextcloudDirectoryMetadataTable . self ) . filter ( " account == %@ AND parentDirectoryServerUrl == %@ " , account , parentDirectoryServerUrl )
2023-01-26 20:09:19 +03:00
processDirectoryMetadatasToDelete ( databaseToWriteTo : database ,
existingDirectoryMetadatas : existingDirectoryMetadatas ,
updatedDirectoryMetadatas : updatedDirectoryMetadatas )
processDirectoryMetadatasToUpdate ( databaseToWriteTo : database ,
existingDirectoryMetadatas : existingDirectoryMetadatas ,
updatedDirectoryMetadatas : updatedDirectoryMetadatas )
2023-01-26 18:07:46 +03:00
}
} catch let error {
2023-01-26 22:50:10 +03:00
NSLog ( " Could not update directory metadatas, received error: %@ " , error . localizedDescription )
2023-01-26 18:07:46 +03:00
}
}
2023-02-20 15:37:10 +03:00
func directoryMetadataFromItemMetadata ( directoryItemMetadata : NextcloudItemMetadataTable , recordEtag : Bool = false ) -> NextcloudDirectoryMetadataTable {
2023-02-01 21:35:08 +03:00
var newDirectoryMetadata = NextcloudDirectoryMetadataTable ( )
let directoryOcId = directoryItemMetadata . ocId
2023-01-26 20:09:19 +03:00
2023-02-01 21:35:08 +03:00
if let existingDirectoryMetadata = directoryMetadata ( ocId : directoryOcId ) {
newDirectoryMetadata = existingDirectoryMetadata
}
2023-01-26 20:09:19 +03:00
2023-02-01 21:35:08 +03:00
if recordEtag {
newDirectoryMetadata . etag = directoryItemMetadata . etag
}
2023-01-26 20:09:19 +03:00
2023-02-01 21:35:08 +03:00
newDirectoryMetadata . ocId = directoryOcId
newDirectoryMetadata . fileId = directoryItemMetadata . fileId
newDirectoryMetadata . parentDirectoryServerUrl = directoryItemMetadata . serverUrl
newDirectoryMetadata . serverUrl = directoryItemMetadata . serverUrl + " / " + directoryItemMetadata . fileNameView
newDirectoryMetadata . account = directoryItemMetadata . account
newDirectoryMetadata . e2eEncrypted = directoryItemMetadata . e2eEncrypted
newDirectoryMetadata . favorite = directoryItemMetadata . favorite
newDirectoryMetadata . permissions = directoryItemMetadata . permissions
2023-01-26 20:09:19 +03:00
2023-02-01 21:35:08 +03:00
return newDirectoryMetadata
}
func updateDirectoryMetadatasFromItemMetadatas ( account : String , parentDirectoryServerUrl : String , updatedDirectoryItemMetadatas : [ NextcloudItemMetadataTable ] , recordEtag : Bool = false ) {
2023-01-26 20:09:19 +03:00
2023-02-01 21:35:08 +03:00
var updatedDirMetadatas : [ NextcloudDirectoryMetadataTable ] = [ ]
for directoryItemMetadata in updatedDirectoryItemMetadatas {
let newDirectoryMetadata = directoryMetadataFromItemMetadata ( directoryItemMetadata : directoryItemMetadata , recordEtag : recordEtag )
2023-01-26 20:09:19 +03:00
updatedDirMetadatas . append ( newDirectoryMetadata )
}
2023-01-26 21:09:24 +03:00
updateDirectoryMetadatas ( account : account , parentDirectoryServerUrl : parentDirectoryServerUrl , updatedDirectoryMetadatas : updatedDirMetadatas )
2023-01-26 20:09:19 +03:00
}
2023-02-20 15:36:27 +03:00
func addDirectoryMetadata ( _ metadata : NextcloudDirectoryMetadataTable ) {
let database = ncDatabase ( )
do {
try database . write {
database . add ( metadata , update : . all )
NSLog ( " " "
Created new metadata ( addDirectoryMetadata ) .
ocID : % @ ,
serverUrl : % @ ,
etag : % @
" " "
, metadata . ocId , metadata . serverUrl , metadata . etag )
}
} catch let error {
NSLog ( " Could not add item metadata with ocID: %@ and serverUrl: %@, received error: %@ " , metadata . ocId , metadata . serverUrl , error . localizedDescription )
}
}
2023-01-12 23:36:51 +03:00
func localFileMetadataFromOcId ( _ ocId : String ) -> NextcloudLocalFileMetadataTable ? {
2023-01-26 20:47:34 +03:00
if let metadata = ncDatabase ( ) . objects ( NextcloudLocalFileMetadataTable . self ) . filter ( " ocId == %@ " , ocId ) . first {
return NextcloudLocalFileMetadataTable ( value : metadata )
}
return nil
2023-01-12 23:36:51 +03:00
}
2023-01-13 16:53:40 +03:00
2023-02-16 17:43:44 +03:00
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 )
}
} catch let error {
NSLog ( " Could not add local file metadata from item metadata with ocID: %@ and filename: %@, received error: %@ " , itemMetadata . ocId , itemMetadata . fileNameView , error . localizedDescription )
}
}
2023-01-13 16:53:40 +03:00
@objc func convertNKFileToItemMetadata ( _ file : NKFile , account : String ) -> NextcloudItemMetadataTable {
let metadata = NextcloudItemMetadataTable ( )
metadata . account = account
metadata . checksums = file . checksums
metadata . commentsUnread = file . commentsUnread
metadata . contentType = file . contentType
if let date = file . creationDate {
2023-01-26 22:48:58 +03:00
metadata . creationDate = date as Date
2023-01-13 16:53:40 +03:00
} else {
2023-01-26 22:48:58 +03:00
metadata . creationDate = file . date as Date
2023-01-13 16:53:40 +03:00
}
metadata . dataFingerprint = file . dataFingerprint
2023-01-26 22:48:58 +03:00
metadata . date = file . date as Date
2023-01-13 16:53:40 +03:00
metadata . directory = file . directory
metadata . downloadURL = file . downloadURL
metadata . e2eEncrypted = file . e2eEncrypted
metadata . etag = file . etag
metadata . favorite = file . favorite
metadata . fileId = file . fileId
metadata . fileName = file . fileName
metadata . fileNameView = file . fileName
metadata . hasPreview = file . hasPreview
metadata . iconName = file . iconName
metadata . mountType = file . mountType
metadata . name = file . name
metadata . note = file . note
metadata . ocId = file . ocId
metadata . ownerId = file . ownerId
metadata . ownerDisplayName = file . ownerDisplayName
metadata . lock = file . lock
metadata . lockOwner = file . lockOwner
metadata . lockOwnerEditor = file . lockOwnerEditor
metadata . lockOwnerType = file . lockOwnerType
metadata . lockOwnerDisplayName = file . lockOwnerDisplayName
metadata . lockTime = file . lockTime
metadata . lockTimeOut = file . lockTimeOut
metadata . path = file . path
metadata . permissions = file . permissions
metadata . quotaUsedBytes = file . quotaUsedBytes
metadata . quotaAvailableBytes = file . quotaAvailableBytes
metadata . richWorkspace = file . richWorkspace
metadata . resourceType = file . resourceType
metadata . serverUrl = file . serverUrl
metadata . sharePermissionsCollaborationServices = file . sharePermissionsCollaborationServices
for element in file . sharePermissionsCloudMesh {
metadata . sharePermissionsCloudMesh . append ( element )
}
for element in file . shareType {
metadata . shareType . append ( element )
}
metadata . size = file . size
metadata . classFile = file . classFile
// FIXME: i O S 1 2 . 0 , * d o n ' t d e t e c t U T I t e x t / m a r k d o w n , t e x t / x - m a r k d o w n
if ( metadata . contentType = = " text/markdown " || metadata . contentType = = " text/x-markdown " ) && metadata . classFile = = NKCommon . typeClassFile . unknow . rawValue {
metadata . classFile = NKCommon . typeClassFile . document . rawValue
}
if let date = file . uploadDate {
2023-01-26 22:48:58 +03:00
metadata . uploadDate = date as Date
2023-01-13 16:53:40 +03:00
} else {
2023-01-26 22:48:58 +03:00
metadata . uploadDate = file . date as Date
2023-01-13 16:53:40 +03:00
}
metadata . urlBase = file . urlBase
metadata . user = file . user
metadata . userId = file . userId
// S u p p o r t f o r f i n d i n g t h e c o r r e c t f i l e n a m e f o r e 2 e e f i l e s s h o u l d g o h e r e
return metadata
}
func convertNKFilesToItemMetadatas ( _ files : [ NKFile ] , account : String , completionHandler : @ escaping ( _ directoryMetadata : NextcloudItemMetadataTable , _ childDirectoriesMetadatas : [ NextcloudItemMetadataTable ] , _ metadatas : [ NextcloudItemMetadataTable ] ) -> Void ) {
var directoryMetadataSet = false
var directoryMetadata = NextcloudItemMetadataTable ( )
var childDirectoriesMetadatas : [ NextcloudItemMetadataTable ] = [ ]
var metadatas : [ NextcloudItemMetadataTable ] = [ ]
for file in files {
let metadata = convertNKFileToItemMetadata ( file , account : account )
if metadatas . isEmpty && ! directoryMetadataSet {
directoryMetadata = metadata ;
directoryMetadataSet = true ;
} else {
metadatas . append ( metadata )
if metadata . directory {
childDirectoriesMetadatas . append ( metadata )
}
}
}
completionHandler ( directoryMetadata , childDirectoriesMetadatas , metadatas )
}
2023-01-05 22:59:50 +03:00
}