123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251 |
- //
- // FileProviderExtension.swift
- // Files
- //
- // Created by Marino Faggiana on 26/03/18.
- // Copyright © 2018 TWS. All rights reserved.
- //
- // Author Marino Faggiana <m.faggiana@twsweb.it>
- //
- // 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 3 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.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- //
- import FileProvider
- import UIKit
- import MobileCoreServices
- var ocNetworking: OCnetworking?
- var account = ""
- var accountUser = ""
- var accountUserID = ""
- var accountPassword = ""
- var accountUrl = ""
- var homeServerUrl = ""
- var directoryUser = ""
- // Directory
- var groupURL: URL?
- var fileProviderStorageURL: URL?
- // List
- var listUpdateItems = [NSFileProviderItem]()
- var listFavoriteIdentifierRank = [String:NSNumber]()
- var fileNamePathImport = [String]()
- // Metadata Temp for Import
- let FILEID_IMPORT_METADATA_TEMP = k_uploadSessionID + "FILE_PROVIDER_EXTENSION"
- var timerUpload: Timer?
- class FileProvider: NSFileProviderExtension, CCNetworkingDelegate {
-
- var fileManager = FileManager()
- override init() {
-
- super.init()
-
- setupActiveAccount()
-
- verifyUploadQueueInLock()
-
- if #available(iOSApplicationExtension 11.0, *) {
-
- listFavoriteIdentifierRank = NCManageDatabase.sharedInstance.getTableMetadatasDirectoryFavoriteIdentifierRank()
-
- // Timer for upload
- if timerUpload == nil {
-
- timerUpload = Timer.init(timeInterval: TimeInterval(k_timerProcessAutoDownloadUpload), repeats: true, block: { (Timer) in
-
- self.uploadFile()
- })
-
- RunLoop.main.add(timerUpload!, forMode: .defaultRunLoopMode)
- }
-
- } else {
-
- NSFileCoordinator().coordinate(writingItemAt: self.documentStorageURL, options: [], error: nil, byAccessor: { newURL in
- do {
- try fileManager.createDirectory(at: newURL, withIntermediateDirectories: true, attributes: nil)
- } catch let error {
- print("error: \(error)")
- }
- })
- }
- }
-
- // MARK: - Enumeration
-
- override func enumerator(for containerItemIdentifier: NSFileProviderItemIdentifier) throws -> NSFileProviderEnumerator {
-
- /* ONLY iOS 11*/
- guard #available(iOS 11, *) else {
- throw NSError(domain: NSCocoaErrorDomain, code: NSFileNoSuchFileError, userInfo:[:])
- }
-
- var maybeEnumerator: NSFileProviderEnumerator? = nil
- if (containerItemIdentifier == NSFileProviderItemIdentifier.rootContainer) {
- maybeEnumerator = FileProviderEnumerator(enumeratedItemIdentifier: containerItemIdentifier)
- } else if (containerItemIdentifier == NSFileProviderItemIdentifier.workingSet) {
- maybeEnumerator = FileProviderEnumeratorWorkingSet(enumeratedItemIdentifier: containerItemIdentifier)
- } else {
- // determine if the item is a directory or a file
- // - for a directory, instantiate an enumerator of its subitems
- // - for a file, instantiate an enumerator that observes changes to the file
- let item = try self.item(for: containerItemIdentifier)
-
- if item.typeIdentifier == kUTTypeFolder as String {
- maybeEnumerator = FileProviderEnumerator(enumeratedItemIdentifier: containerItemIdentifier)
- } else {
- maybeEnumerator = FileProviderEnumeratorFile(enumeratedItemIdentifier: containerItemIdentifier)
- }
- }
-
- guard let enumerator = maybeEnumerator else {
- throw NSError(domain: NSCocoaErrorDomain, code: NSFeatureUnsupportedError, userInfo:[:])
- }
-
- return enumerator
- }
-
- // MARK: - Item
- override func item(for identifier: NSFileProviderItemIdentifier) throws -> NSFileProviderItem {
-
- /* ONLY iOS 11*/
- guard #available(iOS 11, *) else {
- throw NSError(domain: NSCocoaErrorDomain, code: NSFileNoSuchFileError, userInfo:[:])
- }
- if identifier == .rootContainer {
-
- if let directory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account = %@ AND serverUrl = %@", account, homeServerUrl)) {
-
- let metadata = tableMetadata()
-
- metadata.account = account
- metadata.directory = true
- metadata.directoryID = directory.directoryID
- metadata.fileID = identifier.rawValue
- metadata.fileName = NCBrandOptions.sharedInstance.brand
- metadata.fileNameView = NCBrandOptions.sharedInstance.brand
- metadata.typeFile = k_metadataTypeFile_directory
-
- return FileProviderItem(metadata: metadata, parentItemIdentifier: NSFileProviderItemIdentifier(NSFileProviderItemIdentifier.rootContainer.rawValue))
- }
-
- } else {
-
- let fileID = identifier.rawValue
-
- if let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account = %@ AND fileID = %@", account, fileID)) {
- let parentItemIdentifier = getDirectoryParent(metadataDirectoryID: metadata.directoryID)
- if parentItemIdentifier != nil {
- let item = FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier!)
- return item
- }
- }
- }
-
- // implement the actual lookup
- throw NSFileProviderError(.noSuchItem)
- }
-
- override func urlForItem(withPersistentIdentifier identifier: NSFileProviderItemIdentifier) -> URL? {
-
- /* ONLY iOS 11*/
- guard #available(iOS 11, *) else {
- return nil
- }
-
- // resolve the given identifier to a file on disk
- guard let item = try? item(for: identifier) else {
- return nil
- }
-
- // in this implementation, all paths are structured as <base storage directory>/<item identifier>/<item file name>
-
- let manager = NSFileProviderManager.default
- var url = manager.documentStorageURL.appendingPathComponent(identifier.rawValue, isDirectory: true)
-
- if item.typeIdentifier == (kUTTypeFolder as String) {
- url = url.appendingPathComponent(item.filename, isDirectory:true)
- } else {
- url = url.appendingPathComponent(item.filename, isDirectory:false)
- }
-
- return url
- }
-
- override func persistentIdentifierForItem(at url: URL) -> NSFileProviderItemIdentifier? {
-
- // resolve the given URL to a persistent identifier using a database
- let pathComponents = url.pathComponents
-
- // exploit the fact that the path structure has been defined as
- // <base storage directory>/<item identifier>/<item file name> above
- assert(pathComponents.count > 2)
-
- let itemIdentifier = NSFileProviderItemIdentifier(pathComponents[pathComponents.count - 2])
- return itemIdentifier
- }
-
- // MARK: - Managing Shared Files
-
- override func providePlaceholder(at url: URL, completionHandler: @escaping (Error?) -> Void) {
-
- if #available(iOSApplicationExtension 11.0, *) {
- guard let identifier = persistentIdentifierForItem(at: url) else {
- completionHandler(NSFileProviderError(.noSuchItem))
- return
- }
- do {
- let fileProviderItem = try item(for: identifier)
- let placeholderURL = NSFileProviderManager.placeholderURL(for: url)
- try NSFileProviderManager.writePlaceholder(at: placeholderURL,withMetadata: fileProviderItem)
- completionHandler(nil)
- } catch let error {
- print("error: \(error)")
- completionHandler(error)
- }
-
- } else {
-
- let fileName = url.lastPathComponent
- let placeholderURL = NSFileProviderExtension.placeholderURL(for: self.documentStorageURL.appendingPathComponent(fileName))
- let fileSize = 0
- let metadata = [AnyHashable(URLResourceKey.fileSizeKey): fileSize]
- do {
- try NSFileProviderExtension.writePlaceholder(at: placeholderURL, withMetadata: metadata as! [URLResourceKey : Any])
- } catch let error {
- print("error: \(error)")
- }
- completionHandler(nil)
- }
- }
- override func startProvidingItem(at url: URL, completionHandler: @escaping ((_ error: Error?) -> Void)) {
-
- if #available(iOSApplicationExtension 11.0, *) {
- let pathComponents = url.pathComponents
- let identifier = NSFileProviderItemIdentifier(pathComponents[pathComponents.count - 2])
- let fileID = identifier.rawValue
- var fileSize = 0 as Double
- var localEtag = ""
- var localEtagFPE = ""
-
- // If identifier is a temp return
- if identifier.rawValue.contains(FILEID_IMPORT_METADATA_TEMP) {
- completionHandler(nil)
- return
- }
-
- guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account = %@ AND fileID = %@", account, fileID)) else {
- completionHandler(NSFileProviderError(.noSuchItem))
- return
- }
-
- let tableLocalFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "account = %@ AND fileID = %@", account, fileID))
- if tableLocalFile != nil {
- localEtag = tableLocalFile!.etag
- localEtagFPE = tableLocalFile!.etagFPE
- }
-
- if (localEtagFPE != "") {
-
- // Verify last version on "Local Table"
- if localEtag != localEtagFPE {
- if self.copyFile("\(directoryUser)/\(identifier.rawValue)", toPath: url.path) == nil {
- NCManageDatabase.sharedInstance.setLocalFile(fileID: identifier.rawValue, date: nil, exifDate: nil, exifLatitude: nil, exifLongitude: nil, fileName: nil, etag: nil, etagFPE: localEtag)
- }
- }
-
- do {
- let attributes = try fileManager.attributesOfItem(atPath: url.path)
- fileSize = attributes[FileAttributeKey.size] as! Double
- } catch let error {
- print("error: \(error)")
- }
-
- if (fileSize > 0) {
- completionHandler(nil)
- return
- }
- }
-
- guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
- completionHandler(NSFileProviderError(.noSuchItem))
- return
- }
-
- // delete prev file + ico on Directory User
- _ = self.deleteFile("\(directoryUser)/\(metadata.fileID)")
- _ = self.deleteFile("\(directoryUser)/\(metadata.fileID).ico")
- let task = ocNetworking?.downloadFileNameServerUrl("\(serverUrl)/\(metadata.fileName)", fileNameLocalPath: "\(directoryUser)/\(metadata.fileID)", communication: CCNetworking.shared().sharedOCCommunicationExtensionDownload(metadata.fileName), success: { (lenght, etag, date) in
-
- // copy download file to url
- _ = self.copyFile("\(directoryUser)/\(metadata.fileID)", toPath: url.path)
-
- // update DB Local
- metadata.date = date! as NSDate
- metadata.etag = etag!
- NCManageDatabase.sharedInstance.addLocalFile(metadata: metadata)
- NCManageDatabase.sharedInstance.setLocalFile(fileID: metadata.fileID, date: date! as NSDate, exifDate: nil, exifLatitude: nil, exifLongitude: nil, fileName: nil, etag: etag, etagFPE: etag)
-
- // Update DB Metadata
- _ = NCManageDatabase.sharedInstance.addMetadata(metadata)
- completionHandler(nil)
-
- }, failure: { (errorMessage, errorCode) in
- completionHandler(NSFileProviderError(.serverUnreachable))
- })
-
- if task != nil {
- NSFileProviderManager.default.register(task!, forItemWithIdentifier: NSFileProviderItemIdentifier(identifier.rawValue)) { (error) in }
- }
-
- } else {
-
- guard let fileData = try? Data(contentsOf: url) else {
- completionHandler(nil)
- return
- }
- do {
- _ = try fileData.write(to: url, options: NSData.WritingOptions())
- completionHandler(nil)
- } catch let error {
- print("error: \(error)")
- completionHandler(error)
- }
- }
- }
-
- override func itemChanged(at url: URL) {
-
- if #available(iOSApplicationExtension 11.0, *) {
-
- let fileName = url.lastPathComponent
- let pathComponents = url.pathComponents
- let metadataNet = CCMetadataNet()
- assert(pathComponents.count > 2)
- let identifier = NSFileProviderItemIdentifier(pathComponents[pathComponents.count - 2])
- let fileID = identifier.rawValue
-
- guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account = %@ AND fileID = %@", account, fileID)) else {
- return
- }
-
- guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
- return
- }
-
- metadataNet.account = account
- metadataNet.assetLocalIdentifier = FILEID_IMPORT_METADATA_TEMP + metadata.directoryID + fileName
- metadataNet.fileName = fileName
- metadataNet.path = url.path
- metadataNet.selector = selectorUploadFile
- metadataNet.selectorPost = ""
- metadataNet.serverUrl = serverUrl
- metadataNet.session = k_upload_session_extension
- metadataNet.taskStatus = Int(k_taskStatusResume)
-
- _ = NCManageDatabase.sharedInstance.addQueueUpload(metadataNet: metadataNet)
-
- self.uploadFile()
-
- } else {
-
- let fileSize = (try! fileManager.attributesOfItem(atPath: url.path)[FileAttributeKey.size] as! NSNumber).uint64Value
- NSLog("[LOG] Item changed at URL %@ %lu", url as NSURL, fileSize)
-
- guard let account = NCManageDatabase.sharedInstance.getAccountActive() else {
- self.stopProvidingItem(at: url)
- return
- }
- guard let fileName = CCUtility.getFileNameExt() else {
- self.stopProvidingItem(at: url)
- return
- }
- // -------> Fix : Clear FileName for twice Office 365
- CCUtility.setFileNameExt("")
- // --------------------------------------------------
- if (fileName != url.lastPathComponent) {
- self.stopProvidingItem(at: url)
- return
- }
- guard let serverUrl = CCUtility.getServerUrlExt() else {
- self.stopProvidingItem(at: url)
- return
- }
- guard let directoryID = NCManageDatabase.sharedInstance.getDirectoryID(serverUrl) else {
- self.stopProvidingItem(at: url)
- return
- }
-
- let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "fileName == %@ AND directoryID == %@", fileName, directoryID))
- if metadata != nil {
-
- // Update
- let uploadID = k_uploadSessionID + CCUtility.createRandomString(16)
- let directoryUser = CCUtility.getDirectoryActiveUser(account.user, activeUrl: account.url)
- let destinationDirectoryUser = "\(directoryUser!)/\(uploadID)"
-
- // copy sourceURL on directoryUser
- _ = self.copyFile(url.path, toPath: destinationDirectoryUser)
-
- // Prepare for send Metadata
- metadata!.sessionID = uploadID
- metadata!.session = k_upload_session
- metadata!.sessionTaskIdentifier = Int(k_taskIdentifierWaitStart)
- _ = NCManageDatabase.sharedInstance.updateMetadata(metadata!)
-
- } else {
-
- // New
- let directoryUser = CCUtility.getDirectoryActiveUser(account.user, activeUrl: account.url)
- let destinationDirectoryUser = "\(directoryUser!)/\(fileName)"
-
- _ = self.copyFile(url.path, toPath: destinationDirectoryUser)
- CCNetworking.shared().uploadFile(fileName, serverUrl: serverUrl, assetLocalIdentifier: nil, session: k_upload_session, taskStatus: Int(k_taskStatusResume), selector: nil, selectorPost: nil, errorCode: 0, delegate: self)
- }
- self.stopProvidingItem(at: url)
- }
- }
-
- override func stopProvidingItem(at url: URL) {
- // Called after the last claim to the file has been released. At this point, it is safe for the file provider to remove the content file.
- // Care should be taken that the corresponding placeholder file stays behind after the content file has been deleted.
-
- // Called after the last claim to the file has been released. At this point, it is safe for the file provider to remove the content file.
-
- // look up whether the file has local changes
- let fileHasLocalChanges = false
-
- if !fileHasLocalChanges {
- // remove the existing file to free up space
- do {
- _ = try fileManager.removeItem(at: url)
- } catch let error {
- print("error: \(error)")
- }
-
- // write out a placeholder to facilitate future property lookups
- self.providePlaceholder(at: url, completionHandler: { error in
- // handle any error, do any necessary cleanup
- })
- }
- }
-
- // MARK: - Accessing Thumbnails
-
- override func fetchThumbnails(for itemIdentifiers: [NSFileProviderItemIdentifier], requestedSize size: CGSize, perThumbnailCompletionHandler: @escaping (NSFileProviderItemIdentifier, Data?, Error?) -> Void, completionHandler: @escaping (Error?) -> Void) -> Progress {
-
- /* ONLY iOS 11*/
- guard #available(iOS 11, *) else {
- return Progress(totalUnitCount:0)
- }
- let progress = Progress(totalUnitCount: Int64(itemIdentifiers.count))
- var counterProgress: Int64 = 0
-
- for itemIdentifier in itemIdentifiers {
-
- let fileID = itemIdentifier.rawValue
-
- if let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account = %@ AND fileID = %@", account, fileID)) {
-
- if (metadata.typeFile == k_metadataTypeFile_image || metadata.typeFile == k_metadataTypeFile_video) {
-
- let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID)
- let fileName = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: serverUrl, activeUrl: accountUrl)
- let fileNameLocal = metadata.fileID
- ocNetworking?.downloadThumbnail(withDimOfThumbnail: "m", fileName: fileName, fileNameLocal: fileNameLocal, success: {
- do {
- let url = URL.init(fileURLWithPath: "\(directoryUser)/\(itemIdentifier.rawValue).ico")
- let data = try Data.init(contentsOf: url)
- perThumbnailCompletionHandler(itemIdentifier, data, nil)
- } catch let error {
- print("error: \(error)")
- perThumbnailCompletionHandler(itemIdentifier, nil, NSFileProviderError(.noSuchItem))
- }
-
- counterProgress += 1
- if (counterProgress == progress.totalUnitCount) {
- completionHandler(nil)
- }
-
- }, failure: { (errorMessage, errorCode) in
- perThumbnailCompletionHandler(itemIdentifier, nil, NSFileProviderError(.serverUnreachable))
-
- counterProgress += 1
- if (counterProgress == progress.totalUnitCount) {
- completionHandler(nil)
- }
- })
-
- } else {
-
- counterProgress += 1
- if (counterProgress == progress.totalUnitCount) {
- completionHandler(nil)
- }
- }
- } else {
- counterProgress += 1
- if (counterProgress == progress.totalUnitCount) {
- completionHandler(nil)
- }
- }
- }
-
- return progress
- }
-
- // MARK: - Actions
- override func createDirectory(withName directoryName: String, inParentItemIdentifier parentItemIdentifier: NSFileProviderItemIdentifier, completionHandler: @escaping (NSFileProviderItem?, Error?) -> Void) {
- /* ONLY iOS 11*/
- guard #available(iOS 11, *) else {
- return
- }
-
- var serverUrl = ""
-
- if parentItemIdentifier == .rootContainer {
-
- serverUrl = homeServerUrl
-
- } else {
-
- let fileID = parentItemIdentifier.rawValue
-
- guard let directoryParent = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account = %@ AND fileID = %@", account, fileID)) else {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- return
- }
-
- serverUrl = directoryParent.serverUrl
- }
-
- ocNetworking?.createFolder(directoryName, serverUrl: serverUrl, account: account, success: { (fileID, date) in
-
- let metadata = tableMetadata()
-
- metadata.account = account
- metadata.directory = true
- metadata.directoryID = NCManageDatabase.sharedInstance.getDirectoryID(serverUrl)!
- metadata.fileID = fileID!
- metadata.fileName = directoryName
- metadata.fileNameView = directoryName
- metadata.typeFile = k_metadataTypeFile_directory
-
- // METADATA
- guard let metadataDB = NCManageDatabase.sharedInstance.addMetadata(metadata) else {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- return
- }
-
- // DIRECTORY
- guard let _ = NCManageDatabase.sharedInstance.addDirectory(encrypted: false, favorite: false, fileID: fileID!, permissions: nil, serverUrl: serverUrl + "/" + directoryName) else {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- return
- }
-
- let parentItemIdentifier = getDirectoryParent(metadataDirectoryID: metadataDB.directoryID)
- if parentItemIdentifier != nil {
- let item = FileProviderItem(metadata: metadataDB, parentItemIdentifier: parentItemIdentifier!)
- completionHandler(item, nil)
- } else {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- }
-
- }, failure: { (errorMessage, errorCode) in
- completionHandler(nil, NSFileProviderError(.serverUnreachable))
- })
- }
-
- override func deleteItem(withIdentifier itemIdentifier: NSFileProviderItemIdentifier, completionHandler: @escaping (Error?) -> Void) {
-
- /* ONLY iOS 11*/
- guard #available(iOS 11, *) else {
- return
- }
-
- let fileID = itemIdentifier.rawValue
-
- guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account = %@ AND fileID = %@", account, fileID)) else {
- completionHandler(nil)
- return
- }
-
- guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
- completionHandler(nil)
- return
- }
-
- ocNetworking?.deleteFileOrFolder(metadata.fileName, serverUrl: serverUrl, success: {
-
- let fileNamePath = directoryUser + "/" + metadata.fileID
- do {
- try self.fileManager.removeItem(atPath: fileNamePath)
- } catch let error {
- print("error: \(error)")
- }
- do {
- try self.fileManager.removeItem(atPath: fileNamePath + ".ico")
- } catch let error {
- print("error: \(error)")
- }
- do {
- try self.fileManager.removeItem(atPath: fileProviderStorageURL!.path + "/" + metadata.fileID)
- } catch let error {
- print("error: \(error)")
- }
-
- if metadata.directory {
- let dirForDelete = CCUtility.stringAppendServerUrl(serverUrl, addFileName: metadata.fileName)
- NCManageDatabase.sharedInstance.deleteDirectoryAndSubDirectory(serverUrl: dirForDelete!)
- }
-
- NCManageDatabase.sharedInstance.deleteLocalFile(predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
- NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "fileID == %@", metadata.fileID), clearDateReadDirectoryID: nil)
-
- completionHandler(nil)
-
- }, failure: { (errorMessage, errorCode) in
-
- if errorCode == 404 {
- completionHandler(nil)
- } else {
- completionHandler(NSFileProviderError(.serverUnreachable))
- }
- })
- }
-
- override func reparentItem(withIdentifier itemIdentifier: NSFileProviderItemIdentifier, toParentItemWithIdentifier parentItemIdentifier: NSFileProviderItemIdentifier, newName: String?, completionHandler: @escaping (NSFileProviderItem?, Error?) -> Void) {
-
- /* ONLY iOS 11*/
- guard #available(iOS 11, *) else {
- return
- }
-
- let fileIDFrom = itemIdentifier.rawValue
- let fileIDTo = parentItemIdentifier.rawValue
-
- var serverUrlTo = ""
- var fileNameTo = ""
- var directoryIDTo = ""
-
- guard let itemFrom = try? item(for: itemIdentifier) else {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- return
- }
-
- guard let metadataFrom = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account = %@ AND fileID = %@", account, fileIDFrom)) else {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- return
- }
-
- guard let serverUrlFrom = NCManageDatabase.sharedInstance.getServerUrl(metadataFrom.directoryID) else {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- return
- }
-
- let fileNameFrom = serverUrlFrom + "/" + itemFrom.filename
- if parentItemIdentifier == NSFileProviderItemIdentifier.rootContainer {
- serverUrlTo = homeServerUrl
- } else {
- guard let metadataTo = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account = %@ AND fileID = %@", account, fileIDTo)) else {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- return
- }
- serverUrlTo = NCManageDatabase.sharedInstance.getServerUrl(metadataTo.directoryID)! + "/" + metadataTo.fileName
- }
-
- directoryIDTo = NCManageDatabase.sharedInstance.getDirectoryID(serverUrlTo)!
- fileNameTo = serverUrlTo + "/" + itemFrom.filename
-
- ocNetworking?.moveFileOrFolder(fileNameFrom, fileNameTo: fileNameTo, success: {
-
- if metadataFrom.directory {
-
- NCManageDatabase.sharedInstance.deleteDirectoryAndSubDirectory(serverUrl: serverUrlFrom)
- NCManageDatabase.sharedInstance.moveMetadata(fileName: metadataFrom.fileName, directoryID: metadataFrom.directoryID, directoryIDTo: directoryIDTo)
- _ = NCManageDatabase.sharedInstance.addDirectory(encrypted: false, favorite: false, fileID: nil, permissions: nil, serverUrl: serverUrlTo)
-
- } else {
- NCManageDatabase.sharedInstance.moveMetadata(fileName: metadataFrom.fileName, directoryID: metadataFrom.directoryID, directoryIDTo: directoryIDTo)
- }
-
- guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account = %@ AND fileID = %@", account, fileIDFrom)) else {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- return
- }
-
- let parentItemIdentifier = getDirectoryParent(metadataDirectoryID: metadata.directoryID)
- if parentItemIdentifier != nil {
- let item = FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier!)
- completionHandler(item, nil)
- } else {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- }
-
- }, failure: { (errorMessage, errorCode) in
- completionHandler(nil, NSFileProviderError(.serverUnreachable))
- })
- }
-
- override func renameItem(withIdentifier itemIdentifier: NSFileProviderItemIdentifier, toName itemName: String, completionHandler: @escaping (NSFileProviderItem?, Error?) -> Void) {
-
- /* ONLY iOS 11*/
- guard #available(iOS 11, *) else {
- return
- }
-
- let fileID = itemIdentifier.rawValue
- guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account = %@ AND fileID = %@", account, fileID)) else {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- return
- }
-
- guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- return
- }
-
- guard let directoryTable = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "serverUrl = %@", serverUrl)) else {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- return
- }
-
- let fileNameFrom = metadata.fileNameView
- let fileNamePathFrom = serverUrl + "/" + fileNameFrom
- let fileNamePathTo = serverUrl + "/" + itemName
-
- ocNetworking?.moveFileOrFolder(fileNamePathFrom, fileNameTo: fileNamePathTo, success: {
-
- metadata.fileName = itemName
- metadata.fileNameView = itemName
-
- guard let metadata = NCManageDatabase.sharedInstance.addMetadata(metadata) else {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- return
- }
-
- if metadata.directory {
-
- NCManageDatabase.sharedInstance.setDirectory(serverUrl: fileNamePathFrom, serverUrlTo: fileNamePathTo, etag: nil, fileID: nil, encrypted: directoryTable.e2eEncrypted)
- } else {
-
- do {
- try self.fileManager.moveItem(atPath: fileProviderStorageURL!.path + "/" + metadata.fileID + "/" + fileNameFrom, toPath: fileProviderStorageURL!.path + "/" + metadata.fileID + "/" + itemName)
- NCManageDatabase.sharedInstance.setLocalFile(fileID: metadata.fileID, date: nil, exifDate: nil, exifLatitude: nil, exifLongitude: nil, fileName: itemName, etag: nil, etagFPE: nil)
- } catch { }
- }
-
- let parentItemIdentifier = getDirectoryParent(metadataDirectoryID: metadata.directoryID)
- if parentItemIdentifier != nil {
- let item = FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier!)
- completionHandler(item, nil)
- } else {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- }
-
- }, failure: { (errorMessage, errorCode) in
- completionHandler(nil, NSFileProviderError(.serverUnreachable))
- })
- }
-
- override func setFavoriteRank(_ favoriteRank: NSNumber?, forItemIdentifier itemIdentifier: NSFileProviderItemIdentifier, completionHandler: @escaping (NSFileProviderItem?, Error?) -> Void) {
- /* ONLY iOS 11*/
- guard #available(iOS 11, *) else {
- return
- }
-
- completionHandler(nil, nil)
-
- /*
- guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account = %@ AND fileID = %@", account, itemIdentifier.rawValue)) else {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- return
- }
- guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- return
- }
- // Refresh Favorite Identifier Rank
- listFavoriteIdentifierRank = NCManageDatabase.sharedInstance.getTableMetadatasDirectoryFavoriteIdentifierRank()
- if favoriteRank == nil {
- listFavoriteIdentifierRank.removeValue(forKey: itemIdentifier.rawValue)
- } else {
- let rank = listFavoriteIdentifierRank[itemIdentifier.rawValue]
- if rank == nil {
- listFavoriteIdentifierRank[itemIdentifier.rawValue] = favoriteRank//NSNumber(value: Int64(newRank))
- }
- favorite = true
- }
-
- // Call the completion handler before performing any network activity or other long-running tasks. Defer these tasks to the background
- let item = FileProviderItem(metadata: metadata, serverUrl: serverUrl)
- completionHandler(item, nil)
-
- // Change Status ? Call API Nextcloud Network
- if (favorite == true && metadata.favorite == false) || (favorite == false && metadata.favorite == true) {
-
- DispatchQueue(label: "com.nextcloud", qos: .background, attributes: .concurrent, autoreleaseFrequency: .inherit, target: nil).async {
-
- ocNetworking?.settingFavorite(metadata.fileName, serverUrl: serverUrl, favorite: favorite, success: {
-
- // Change DB
- metadata.favorite = favorite
- _ = NCManageDatabase.sharedInstance.addMetadata(metadata)
-
- // Refresh Favorite Identifier Rank
- listFavoriteIdentifierRank = NCManageDatabase.sharedInstance.getTableMetadatasDirectoryFavoriteIdentifierRank()
-
- // Refresh Item
- self.refreshEnumerator(identifier: itemIdentifier, serverUrl: serverUrl)
-
- }, failure: { (errorMessage, errorCode) in
- print("errorMessage")
- })
- }
- }
- */
- }
-
- override func setLastUsedDate(_ lastUsedDate: Date?, forItemIdentifier itemIdentifier: NSFileProviderItemIdentifier, completionHandler: @escaping (NSFileProviderItem?, Error?) -> Void) {
- print("[LOG] setLastUsedDate")
- completionHandler(nil, nil)
- }
-
- override func setTagData(_ tagData: Data?, forItemIdentifier itemIdentifier: NSFileProviderItemIdentifier, completionHandler: @escaping (NSFileProviderItem?, Error?) -> Void) {
-
- /* ONLY iOS 11*/
- guard #available(iOS 11, *) else {
- return
- }
-
- let fileID = itemIdentifier.rawValue
- // Add, Remove (nil)
- NCManageDatabase.sharedInstance.addTag(fileID, tagIOS: tagData)
-
- guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account = %@ AND fileID = %@", account, fileID)) else {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- return
- }
-
- let parentItemIdentifier = getDirectoryParent(metadataDirectoryID: metadata.directoryID)
- if parentItemIdentifier != nil {
- let item = FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier!)
- completionHandler(item, nil)
- } else {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- }
-
- self.refreshEnumerator(identifier: itemIdentifier, serverUrl: "WorkingSet")
- }
-
- /*
- override func trashItem(withIdentifier itemIdentifier: NSFileProviderItemIdentifier, completionHandler: @escaping (NSFileProviderItem?, Error?) -> Void) {
- print("[LOG] trashitem")
- completionHandler(nil, nil)
- }
-
- override func untrashItem(withIdentifier itemIdentifier: NSFileProviderItemIdentifier, toParentItemIdentifier parentItemIdentifier: NSFileProviderItemIdentifier?, completionHandler: @escaping (NSFileProviderItem?, Error?) -> Void) {
- print("[LOG] untrashitem")
- completionHandler(nil, nil)
- }
- */
-
- override func importDocument(at fileURL: URL, toParentItemIdentifier parentItemIdentifier: NSFileProviderItemIdentifier, completionHandler: @escaping (NSFileProviderItem?, Error?) -> Void) {
-
- /* ONLY iOS 11*/
- guard #available(iOS 11, *) else {
- return
- }
-
- var directoryPredicate: NSPredicate
- var size = 0 as Double
- let metadata = tableMetadata()
- // get serverUrl
-
- if parentItemIdentifier == .rootContainer {
- directoryPredicate = NSPredicate(format: "account = %@ AND serverUrl = %@", account, homeServerUrl)
- } else {
- directoryPredicate = NSPredicate(format: "account = %@ AND fileID = %@", account, parentItemIdentifier.rawValue)
- }
-
- guard let directoryParent = NCManageDatabase.sharedInstance.getTableDirectory(predicate: directoryPredicate) else {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- return
- }
-
- let serverUrl = directoryParent.serverUrl
-
- // --------------------------------------------- Copy file here with security access
-
- if fileURL.startAccessingSecurityScopedResource() == false {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- return
- }
-
- let fileName = createFileName(fileURL.lastPathComponent, directoryID: directoryParent.directoryID, serverUrl: serverUrl)
- let fileNamePathDirectory = fileProviderStorageURL!.path + "/" + FILEID_IMPORT_METADATA_TEMP + directoryParent.directoryID + fileName
-
- do {
- try FileManager.default.createDirectory(atPath: fileNamePathDirectory, withIntermediateDirectories: true, attributes: nil)
- } catch { }
-
- _ = self.moveFile(fileURL.path, toPath: fileNamePathDirectory + "/" + fileName)
-
- fileURL.stopAccessingSecurityScopedResource()
-
- // ---------------------------------------------------------------------------------
-
- do {
- let attributes = try fileManager.attributesOfItem(atPath: fileNamePathDirectory + "/" + fileName)
- size = attributes[FileAttributeKey.size] as! Double
- } catch let error {
- print("error: \(error)")
- }
-
- // Metadata TEMP
- metadata.account = account
- metadata.date = NSDate()
- metadata.directory = false
- metadata.directoryID = directoryParent.directoryID
- metadata.etag = ""
- metadata.fileID = FILEID_IMPORT_METADATA_TEMP + directoryParent.directoryID + fileName
- metadata.size = size
- metadata.status = Double(k_metadataStatusHide)
- metadata.fileName = fileURL.lastPathComponent
- metadata.fileNameView = fileURL.lastPathComponent
- CCUtility.insertTypeFileIconName(fileName, metadata: metadata)
-
- if (size > 0) {
-
- let metadataNet = CCMetadataNet()
-
- metadataNet.account = account
- metadataNet.assetLocalIdentifier = FILEID_IMPORT_METADATA_TEMP + directoryParent.directoryID + fileName
- metadataNet.fileName = fileName
- metadataNet.path = fileNamePathDirectory + "/" + fileName
- metadataNet.selector = selectorUploadFile
- metadataNet.selectorPost = ""
- metadataNet.serverUrl = serverUrl
- metadataNet.session = k_upload_session_extension
- metadataNet.taskStatus = Int(k_taskStatusResume)
-
- _ = NCManageDatabase.sharedInstance.addQueueUpload(metadataNet: metadataNet)
- }
-
- guard let metadataDB = NCManageDatabase.sharedInstance.addMetadata(metadata) else {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- return
- }
-
- let item = FileProviderItem(metadata: metadataDB, parentItemIdentifier: parentItemIdentifier)
- completionHandler(item, nil)
- }
-
- // --------------------------------------------------------------------------------------------
- // MARK: - Upload
- // --------------------------------------------------------------------------------------------
-
- func uploadFileSuccessFailure(_ fileName: String!, fileID: String!, assetLocalIdentifier: String!, serverUrl: String!, selector: String!, selectorPost: String!, errorMessage: String!, errorCode: Int) {
-
- NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "fileID = %@", assetLocalIdentifier), clearDateReadDirectoryID: nil)
- if errorCode == 0 {
-
- NCManageDatabase.sharedInstance.deleteQueueUpload(assetLocalIdentifier: assetLocalIdentifier, selector: selector)
-
- if let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account = %@ AND fileID = %@", account, fileID)) {
-
- // Rename directory file
- if fileManager.fileExists(atPath: fileProviderStorageURL!.path + "/" + assetLocalIdentifier) {
- _ = moveFile(fileProviderStorageURL!.path + "/" + assetLocalIdentifier, toPath: fileProviderStorageURL!.path + "/" + fileID)
- }
-
- NCManageDatabase.sharedInstance.setLocalFile(fileID: fileID, date: nil, exifDate: nil, exifLatitude: nil, exifLongitude: nil, fileName: nil, etag: metadata.etag, etagFPE: metadata.etag)
-
- let parentItemIdentifier = getDirectoryParent(metadataDirectoryID: metadata.directoryID)
- if parentItemIdentifier != nil {
- let item = FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier!)
- self.refreshEnumerator(identifier: item.itemIdentifier, serverUrl: serverUrl)
- }
- }
-
- } else {
-
- NCManageDatabase.sharedInstance.unlockQueueUpload(assetLocalIdentifier: assetLocalIdentifier)
- }
-
- uploadFile()
- }
-
- func uploadFile() {
-
- let queueInLock = NCManageDatabase.sharedInstance.getQueueUploadInLock()
- if queueInLock != nil && queueInLock!.count == 0 {
-
- let metadataNetQueue = NCManageDatabase.sharedInstance.getQueueUploadLock(selector: selectorUploadFile, withPath: true)
- if metadataNetQueue != nil {
-
- if self.copyFile(metadataNetQueue!.path, toPath: directoryUser + "/" + metadataNetQueue!.fileName) == nil {
-
- CCNetworking.shared().uploadFile(metadataNetQueue!.fileName, serverUrl: metadataNetQueue!.serverUrl, assetLocalIdentifier: metadataNetQueue!.assetLocalIdentifier ,session: metadataNetQueue!.session, taskStatus: metadataNetQueue!.taskStatus, selector: metadataNetQueue!.selector, selectorPost: metadataNetQueue!.selectorPost, errorCode: 0, delegate: self)
-
- } else {
- // file not present, delete record Upload Queue
- NCManageDatabase.sharedInstance.deleteQueueUpload(path: metadataNetQueue!.path)
- }
- }
- }
- }
-
- func verifyUploadQueueInLock() {
-
- let tasks = CCNetworking.shared().getUploadTasksExtensionSession()
- if tasks!.count == 0 {
- let records = NCManageDatabase.sharedInstance.getQueueUpload(predicate: NSPredicate(format: "account = %@ AND selector = %@ AND lock == true AND path != nil", account, selectorUploadFile))
- if records != nil && records!.count > 0 {
- NCManageDatabase.sharedInstance.unlockAllQueueUploadInPath()
- }
- }
- }
-
- // --------------------------------------------------------------------------------------------
- // MARK: - User Function
- // --------------------------------------------------------------------------------------------
-
- func refreshEnumerator(identifier: NSFileProviderItemIdentifier, serverUrl: String) {
-
- /* ONLY iOS 11*/
- guard #available(iOS 11, *) else {
- return
- }
-
- let item = try? self.item(for: identifier)
- if item != nil {
- var found = false
- for updateItem in listUpdateItems {
- if updateItem.itemIdentifier.rawValue == identifier.rawValue {
- found = true
- }
- }
- if !found {
- listUpdateItems.append(item!)
- }
- }
-
- if serverUrl == homeServerUrl {
- NSFileProviderManager.default.signalEnumerator(for: .rootContainer, completionHandler: { (error) in
- print("send signal rootContainer")
- })
- } else if serverUrl == "WorkingSet" {
- NSFileProviderManager.default.signalEnumerator(for: .workingSet, completionHandler: { (error) in
- print("send signal workingSet")
- })
- } else {
- if let directory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account = %@ AND serverUrl = %@", account, serverUrl)) {
- let itemDirectory = NSFileProviderItemIdentifier(directory.fileID)
- NSFileProviderManager.default.signalEnumerator(for: itemDirectory, completionHandler: { (error) in
- print("send signal")
- })
- }
- }
- }
-
- func copyFile(_ atPath: String, toPath: String) -> Error? {
-
- var errorResult: Error?
-
- do {
- try fileManager.removeItem(atPath: toPath)
- } catch let error {
- print("error: \(error)")
- }
- do {
- try fileManager.copyItem(atPath: atPath, toPath: toPath)
- } catch let error {
- errorResult = error
- }
-
- return errorResult
- }
-
- func moveFile(_ atPath: String, toPath: String) -> Error? {
-
- var errorResult: Error?
-
- do {
- try fileManager.removeItem(atPath: toPath)
- } catch let error {
- print("error: \(error)")
- }
- do {
- try fileManager.moveItem(atPath: atPath, toPath: toPath)
- } catch let error {
- errorResult = error
- }
-
- return errorResult
- }
-
- func deleteFile(_ atPath: String) -> Error? {
-
- var errorResult: Error?
-
- do {
- try fileManager.removeItem(atPath: atPath)
- } catch let error {
- errorResult = error
- }
-
- return errorResult
- }
-
- func createFileName(_ fileName: String, directoryID: String, serverUrl: String) -> String {
-
- let serialQueue = DispatchQueue(label: "queueCreateFileName")
- var resultFileName = fileName
- serialQueue.sync {
-
- var exitLoop = false
-
- while exitLoop == false {
-
- if NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account = %@ AND fileNameView = %@ AND directoryID = %@", account, resultFileName, directoryID)) != nil || fileNamePathImport.contains(serverUrl+"/"+resultFileName) {
-
- var name = NSString(string: resultFileName).deletingPathExtension
- let ext = NSString(string: resultFileName).pathExtension
-
- let characters = Array(name)
-
- if characters.count < 2 {
- resultFileName = name + " " + "1" + "." + ext
- } else {
- let space = characters[characters.count-2]
- let numChar = characters[characters.count-1]
- var num = Int(String(numChar))
- if (space == " " && num != nil) {
- name = String(name.dropLast())
- num = num! + 1
- resultFileName = name + "\(num!)" + "." + ext
- } else {
- resultFileName = name + " " + "1" + "." + ext
- }
- }
-
- } else {
- exitLoop = true
- }
- }
-
- // add fileNamePathImport
- fileNamePathImport.append(serverUrl+"/"+resultFileName)
- }
-
- return resultFileName
- }
- }
- // --------------------------------------------------------------------------------------------
- // MARK: -
- // --------------------------------------------------------------------------------------------
- func setupActiveAccount() {
-
- guard let activeAccount = NCManageDatabase.sharedInstance.getAccountActive() else {
- return
- }
-
- account = activeAccount.account
- accountUser = activeAccount.user
- accountUserID = activeAccount.userID
- accountPassword = activeAccount.password
- accountUrl = activeAccount.url
- homeServerUrl = CCUtility.getHomeServerUrlActiveUrl(activeAccount.url)
- directoryUser = CCUtility.getDirectoryActiveUser(activeAccount.user, activeUrl: activeAccount.url)
-
- ocNetworking = OCnetworking.init(delegate: nil, metadataNet: nil, withUser: accountUser, withUserID: accountUserID, withPassword: accountPassword, withUrl: accountUrl)
-
- groupURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: NCBrandOptions.sharedInstance.capabilitiesGroups)
- fileProviderStorageURL = groupURL!.appendingPathComponent(k_assetLocalIdentifierFileProviderStorage)
-
- // Create dir File Provider Storage
- do {
- try FileManager.default.createDirectory(atPath: fileProviderStorageURL!.path, withIntermediateDirectories: true, attributes: nil)
- } catch let error as NSError {
- NSLog("Unable to create directory \(error.debugDescription)")
- }
- }
- func createFileIdentifierOnFileSystem(itemIdentifier: String, fileName: String) {
-
- let identifierPath = fileProviderStorageURL!.path + "/" + itemIdentifier
- let fileIdentifier = identifierPath + "/" + fileName
-
- do {
- try FileManager.default.createDirectory(atPath: identifierPath, withIntermediateDirectories: true, attributes: nil)
- } catch { }
-
- // If do not exists create file with size = 0
- if FileManager.default.fileExists(atPath: fileIdentifier) == false {
- FileManager.default.createFile(atPath: fileIdentifier, contents: nil, attributes: nil)
- }
- }
- func getDirectoryParent(metadataDirectoryID: String) -> NSFileProviderItemIdentifier? {
-
- if #available(iOSApplicationExtension 11.0, *) {
- if let directoryParent = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account = %@ AND directoryID = %@", account, metadataDirectoryID)) {
- if directoryParent.serverUrl == homeServerUrl {
- return NSFileProviderItemIdentifier(NSFileProviderItemIdentifier.rootContainer.rawValue)
- } else {
- if let metadataParent = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account = %@ AND fileID = %@", account, directoryParent.fileID)) {
- return NSFileProviderItemIdentifier(metadataParent.fileID)
- }
- }
- }
- }
-
- return nil
- }
|