123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667 |
- //
- // 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 = ""
- var groupURL: URL?
- class FileProvider: NSFileProviderExtension {
-
- var uploading = [String]()
-
- override init() {
-
- super.init()
-
- 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)
-
- if #available(iOSApplicationExtension 11.0, *) {
-
- // Only iOS 11
-
- } else {
-
- NSFileCoordinator().coordinate(writingItemAt: self.documentStorageURL, options: [], error: nil, byAccessor: { newURL in
- try? FileManager.default.createDirectory(at: newURL, withIntermediateDirectories: true, attributes: nil)
- })
- }
- }
-
- 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, serverUrl: homeServerUrl)
- }
-
- } else {
-
- if let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account = %@ AND fileID = %@", account, identifier.rawValue)) {
- if let directory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account = %@ AND directoryID = %@", account, metadata.directoryID)) {
-
- if (!metadata.directory) {
-
- let identifierPathUrl = groupURL!.appendingPathComponent("File Provider Storage").appendingPathComponent(metadata.fileID)
- let toPath = "\(identifierPathUrl.path)/\(metadata.fileNameView)"
- let atPath = "\(directoryUser)/\(metadata.fileID)"
-
- if !FileManager.default.fileExists(atPath: toPath) {
- try? FileManager.default.createDirectory(atPath: identifierPathUrl.path, withIntermediateDirectories: true, attributes: nil)
-
- if FileManager.default.fileExists(atPath: atPath) {
- try? FileManager.default.copyItem(atPath: atPath, toPath: toPath)
- } else {
- FileManager.default.createFile(atPath: toPath, contents: nil, attributes: nil)
- }
- }
- }
-
- return FileProviderItem(metadata: metadata, serverUrl: directory.serverUrl)
- }
- }
- }
-
- // TODO: 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
- }
-
- 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 {
- 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 {
- }
- completionHandler(nil)
- }
- }
- override func startProvidingItem(at url: URL, completionHandler: @escaping ((_ error: Error?) -> Void)) {
-
- if #available(iOSApplicationExtension 11.0, *) {
- var fileSize : UInt64 = 0
-
- do {
- let attr = try FileManager.default.attributesOfItem(atPath: url.path)
- fileSize = attr[FileAttributeKey.size] as! UInt64
- } catch {
- print("Error: \(error)")
- completionHandler(NSFileProviderError(.noSuchItem))
- }
-
- // Do not exists
- if fileSize == 0 {
-
- let pathComponents = url.pathComponents
- let identifier = pathComponents[pathComponents.count - 2]
-
- guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account = %@ AND fileID = %@", account, identifier)) else {
- completionHandler(NSFileProviderError(.noSuchItem))
- return
- }
-
- guard let directory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account = %@ AND directoryID = %@", account, metadata.directoryID)) else {
- completionHandler(NSFileProviderError(.noSuchItem))
- return
- }
-
- _ = ocNetworking?.downloadFileNameServerUrl("\(directory.serverUrl)/\(metadata.fileName)", fileNameLocalPath: "\(directoryUser)/\(metadata.fileID)", success: { (lenght) in
-
- if (lenght > 0) {
-
- // copy download file to url
- try? FileManager.default.removeItem(atPath: url.path)
- try? FileManager.default.copyItem(atPath: "\(directoryUser)/\(metadata.fileID)", toPath: url.path)
- // create thumbnail
- CCGraphics.createNewImage(from: metadata.fileID, directoryUser: directoryUser, fileNameTo: metadata.fileID, extension: (metadata.fileNameView as NSString).pathExtension, size: "m", imageForUpload: false, typeFile: metadata.typeFile, writePreview: true, optimizedFileName: CCUtility.getOptimizedPhoto())
-
- NCManageDatabase.sharedInstance.addLocalFile(metadata: metadata)
- if (metadata.typeFile == k_metadataTypeFile_image) {
- CCExifGeo.sharedInstance().setExifLocalTableEtag(metadata, directoryUser: directoryUser, activeAccount: account)
- }
- }
-
- completionHandler(nil)
-
- }, failure: { (message, errorCode) in
- completionHandler(NSFileProviderError(.serverUnreachable))
- })
-
- } else {
-
- // Exists
- completionHandler(nil)
- }
-
- } 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 as NSError {
- print("error writing file to URL")
- completionHandler(error)
- }
- }
- }
-
- override func itemChanged(at url: URL) {
-
- if #available(iOSApplicationExtension 11.0, *) {
-
- let fileSize = (try! FileManager.default.attributesOfItem(atPath: url.path)[FileAttributeKey.size] as! NSNumber).uint64Value
- NSLog("[LOG] Item changed at URL %@ %lu", url as NSURL, fileSize)
- if (fileSize == 0) {
- return
- }
-
- let fileName = url.lastPathComponent
- let pathComponents = url.pathComponents
- assert(pathComponents.count > 2)
- let identifier = NSFileProviderItemIdentifier(pathComponents[pathComponents.count - 2])
- if (uploading.contains(identifier.rawValue) == true) {
- return
- }
-
- if let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account = %@ AND fileID = %@", account, identifier.rawValue)) {
-
- guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
- return
- }
-
- uploading.append(identifier.rawValue)
-
- _ = ocNetworking?.uploadFileNameServerUrl(serverUrl+"/"+fileName, fileNameLocalPath: url.path, success: { (fileID, etag, date) in
-
- let toPath = "\(directoryUser)/\(metadata.fileID)"
- try? FileManager.default.removeItem(atPath: toPath)
- try? FileManager.default.copyItem(atPath: url.path, toPath: toPath)
- // create thumbnail
- CCGraphics.createNewImage(from: metadata.fileID, directoryUser: directoryUser, fileNameTo: metadata.fileID, extension: (metadata.fileNameView as NSString).pathExtension, size: "m", imageForUpload: false, typeFile: metadata.typeFile, writePreview: true, optimizedFileName: CCUtility.getOptimizedPhoto())
-
- metadata.date = date! as NSDate
-
- do {
- let attributes = try FileManager.default.attributesOfItem(atPath: url.path)
- metadata.size = attributes[FileAttributeKey.size] as! Double
- } catch {
- }
-
- guard let metadataDB = NCManageDatabase.sharedInstance.addMetadata(metadata) else {
- return
- }
-
- // item
- _ = FileProviderItem(metadata: metadataDB, serverUrl: serverUrl)
-
- self.uploading = self.uploading.filter() { $0 != identifier.rawValue }
-
- 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")
- })
- }
-
- }, failure: { (message, errorCode) in
-
- self.uploading = self.uploading.filter() { $0 != identifier.rawValue }
- })
- }
-
- } else {
-
- let fileSize = (try! FileManager.default.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
- do {
- try FileManager.default.removeItem(atPath: destinationDirectoryUser)
- } catch _ {
- print("file do not exists")
- }
-
- do {
- try FileManager.default.copyItem(atPath: url.path, toPath: destinationDirectoryUser)
- } catch _ {
- print("file do not exists")
- self.stopProvidingItem(at: url)
- return
- }
-
- // 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)"
-
- do {
- try FileManager.default.removeItem(atPath: destinationDirectoryUser)
- } catch _ {
- print("file do not exists")
- }
- do {
- try FileManager.default.copyItem(atPath: url.path, toPath: destinationDirectoryUser)
- } catch _ {
- print("file do not exists")
- self.stopProvidingItem(at: url)
- return
- }
-
- CCNetworking.shared().uploadFile(fileName, serverUrl: serverUrl, 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.
-
- // TODO: look up whether the file has local changes
- let fileHasLocalChanges = false
-
- if !fileHasLocalChanges {
- // remove the existing file to free up space
- do {
- _ = try FileManager.default.removeItem(at: url)
- } catch {
- // Handle error
- }
-
- // write out a placeholder to facilitate future property lookups
- self.providePlaceholder(at: url, completionHandler: { error in
- // TODO: handle any error, do any necessary cleanup
- })
- }
- }
-
- // MARK: - Actions
-
- /* TODO: implement the actions for items here
- each of the actions follows the same pattern:
- - make a note of the change in the local model
- - schedule a server request as a background task to inform the server of the change
- - call the completion block with the modified item in its post-modification state
- */
-
- // MARK: - Enumeration
-
- override func enumerator(for containerItemIdentifier: NSFileProviderItemIdentifier) throws -> NSFileProviderEnumerator {
-
- let maybeEnumerator = FileProviderEnumerator(enumeratedItemIdentifier: containerItemIdentifier)
-
- return maybeEnumerator
- }
-
- 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 item in itemIdentifiers {
-
- if let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account = %@ AND fileID = %@", account, item.rawValue)) {
-
- 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)/\(item.rawValue).ico")
- let data = try Data.init(contentsOf: url)
- perThumbnailCompletionHandler(item, data, nil)
- } catch {
- perThumbnailCompletionHandler(item, nil, NSFileProviderError(.noSuchItem))
- }
-
- counterProgress += 1
- if (counterProgress == progress.totalUnitCount) {
- completionHandler(nil)
- }
-
- }, failure: { (message, errorCode) in
- perThumbnailCompletionHandler(item, 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
- }
-
- override func createDirectory(withName directoryName: String, inParentItemIdentifier parentItemIdentifier: NSFileProviderItemIdentifier, completionHandler: @escaping (NSFileProviderItem?, Error?) -> Void) {
- /* ONLY iOS 11*/
- guard #available(iOS 11, *) else {
- return
- }
-
- guard let directoryParent = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account = %@ AND fileID = %@", account, parentItemIdentifier.rawValue)) else {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- return
- }
-
- ocNetworking?.createFolder(directoryName, serverUrl: directoryParent.serverUrl, account: account, success: { (fileID, date) in
-
- guard let newTableDirectory = NCManageDatabase.sharedInstance.addDirectory(encrypted: false, favorite: false, fileID: fileID, permissions: nil, serverUrl: directoryParent.serverUrl+"/"+directoryName) else {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- return
- }
-
- let metadata = tableMetadata()
-
- metadata.account = account
- metadata.directory = true
- metadata.directoryID = newTableDirectory.directoryID
- metadata.fileID = fileID!
- metadata.fileName = directoryName
- metadata.fileNameView = directoryName
- metadata.typeFile = k_metadataTypeFile_directory
-
- let item = FileProviderItem(metadata: metadata, serverUrl: directoryParent.serverUrl)
-
- completionHandler(item, nil)
-
- }, failure: { (message, errorCode) in
- completionHandler(nil, NSFileProviderError(.serverUnreachable))
- })
- }
-
- override func importDocument(at fileURL: URL, toParentItemIdentifier parentItemIdentifier: NSFileProviderItemIdentifier, completionHandler: @escaping (NSFileProviderItem?, Error?) -> Void) {
-
- /* ONLY iOS 11*/
- guard #available(iOS 11, *) else {
- return
- }
-
- let fileName = fileURL.lastPathComponent
- let fileCoordinator = NSFileCoordinator()
- var error: NSError?
- var directoryPredicate: NSPredicate
-
- 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
- }
-
- if fileURL.startAccessingSecurityScopedResource() == false {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- return
- }
-
- let fileNameLocalPath = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(fileURL.lastPathComponent)!
-
- fileCoordinator.coordinate(readingItemAt: fileURL, options: NSFileCoordinator.ReadingOptions.withoutChanges, error: &error) { (url) in
-
- do {
- try FileManager.default.removeItem(atPath: fileNameLocalPath.path)
- } catch _ {
- print("file do not exists")
- }
-
- do {
- try FileManager.default.copyItem(atPath: url.path, toPath: fileNameLocalPath.path)
- } catch _ {
- print("file do not exists")
- }
- }
-
- fileURL.stopAccessingSecurityScopedResource()
- _ = ocNetworking?.uploadFileNameServerUrl(directoryParent.serverUrl+"/"+fileName, fileNameLocalPath: fileNameLocalPath.path, success: { (fileID, etag, date) in
-
- let metadata = tableMetadata()
-
- metadata.account = account
- metadata.date = date! as NSDate
- metadata.directory = false
- metadata.directoryID = directoryParent.directoryID
- metadata.etag = etag!
- metadata.fileID = fileID!
- metadata.fileName = fileName
- metadata.fileNameView = fileName
- do {
- let attributes = try FileManager.default.attributesOfItem(atPath: fileURL.path)
- metadata.size = attributes[FileAttributeKey.size] as! Double
- } catch {
- }
-
- CCUtility.insertTypeFileIconName(fileName, metadata: metadata)
-
- guard let metadataDB = NCManageDatabase.sharedInstance.addMetadata(metadata) else {
- completionHandler(nil, NSFileProviderError(.noSuchItem))
- return
- }
- // Copy on ItemIdentifier path
- let identifierPathUrl = groupURL!.appendingPathComponent("File Provider Storage").appendingPathComponent(metadata.fileID)
- let toPath = "\(identifierPathUrl.path)/\(metadata.fileNameView)"
-
- if !FileManager.default.fileExists(atPath: identifierPathUrl.path) {
- do {
- try FileManager.default.createDirectory(atPath: identifierPathUrl.path, withIntermediateDirectories: true, attributes: nil)
- } catch let error {
- print("error creating filepath: \(error)")
- }
- }
-
- try? FileManager.default.removeItem(atPath: toPath)
- try? FileManager.default.copyItem(atPath: fileNameLocalPath.path, toPath: toPath)
- // add item
- let item = FileProviderItem(metadata: metadataDB, serverUrl: directoryParent.serverUrl)
-
- completionHandler(item, nil)
- }, failure: { (message, errorCode) in
- completionHandler(nil, NSFileProviderError(.serverUnreachable))
- })
- }
- }
|