From 091fd14a88b4dcf4a10327edc99fa80066b48355 Mon Sep 17 00:00:00 2001 From: Amir Abbas Date: Wed, 25 Jan 2017 16:04:16 +0330 Subject: [PATCH] OneDrive drive name bug fix. Added sorting method to `[FileObject]` array. - now return `Error` for uploading a file with size higher than limit in OneDrive / Dropbox instead of asserting --- FileProvider.podspec | 2 +- FileProvider.xcodeproj/project.pbxproj | 4 ++-- README.md | 12 +++++++++--- Sources/DropboxHelper.swift | 14 +++++++++++++- Sources/FileObject.swift | 13 ++++++++++++- Sources/OneDriveFileProvide.swift | 2 +- Sources/OneDriveHelper.swift | 14 +++++++++++++- 7 files changed, 51 insertions(+), 10 deletions(-) diff --git a/FileProvider.podspec b/FileProvider.podspec index eaea024..1f8e93c 100644 --- a/FileProvider.podspec +++ b/FileProvider.podspec @@ -16,7 +16,7 @@ Pod::Spec.new do |s| # s.name = "FileProvider" - s.version = "0.10.1" + s.version = "0.10.2" s.summary = "FileManager replacement for Local and Remote (WebDAV/Dropbox/OneDrive/SMB2) files on iOS and macOS." # This description is used to generate tags and improve search results. diff --git a/FileProvider.xcodeproj/project.pbxproj b/FileProvider.xcodeproj/project.pbxproj index f6e6316..09e0c69 100644 --- a/FileProvider.xcodeproj/project.pbxproj +++ b/FileProvider.xcodeproj/project.pbxproj @@ -595,7 +595,7 @@ 799396601D48B7BF00086753 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { - BUNDLE_VERSION_STRING = 0.10.1; + BUNDLE_VERSION_STRING = 0.10.2; CLANG_WARN_BOOL_CONVERSION = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_EMPTY_BODY = YES; @@ -625,7 +625,7 @@ 799396611D48B7BF00086753 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { - BUNDLE_VERSION_STRING = 0.10.1; + BUNDLE_VERSION_STRING = 0.10.2; CLANG_WARN_BOOL_CONVERSION = YES; CLANG_WARN_CONSTANT_CONVERSION = YES; CLANG_WARN_EMPTY_BODY = YES; diff --git a/README.md b/README.md index 7ed969e..9fa5e44 100644 --- a/README.md +++ b/README.md @@ -26,11 +26,17 @@ Local and WebDAV providers are fully tested and can be used in production enviro - [x] **LocalFileProvider** a wrapper around `FileManager` with some additions like searching and reading a portion of file. - [x] **WebDAVFileProvider** WebDAV protocol is defacto file transmission standard, replaced FTP. -- [x] **DropboxFileProvider** A wrapper around Dropbox Web API. For now it has limitation in uploading files up to 150MB. -- [x] **OneDriveFileProvider** A wrapper around OneDrive Web API, works with `onedrive.com` and compatible servers. For now it has limitation in uploading files up to 100MB. -- [ ] **SMBFileProvider** SMB2/3 introduced in 2006, which is a file and printer sharing protocol originated from Microsoft Windows and now is replacing AFP protocol on MacOS. I implemented data types and some basic functions but *main interface is not implemented yet!* SMB1/CIFS is depericated and very tricky to be implemented +- [x] **DropboxFileProvider** A wrapper around Dropbox Web API. +* For now it has limitation in uploading files up to 150MB. +- [x] **OneDriveFileProvider** A wrapper around OneDrive Web API, works with `onedrive.com` and compatible (business) servers. +* For now it has limitation in uploading files up to 100MB. +- [ ] **CloudFilePRovider** A wrapper around app's ubiquitous container to iCloud Drive in iOS 8+ API. +- [ ] **SMBFileProvider** SMB2/3 introduced in 2006, which is a file and printer sharing protocol originated from Microsoft Windows and now is replacing AFP protocol on MacOS. +* Data types and some basic functions are implemented but *main interface is not implemented yet!* +* SMB1/CIFS is depericated and very tricky to be implemented - [ ] **FTPFileProvider** while deprecated in 1990s, it's still in use on some Web hosts. - [ ] **AmazonS3FileProvider** +- [ ] **GoogleDriveFileProvider** ## Requirements diff --git a/Sources/DropboxHelper.swift b/Sources/DropboxHelper.swift index a893c14..08a2a86 100644 --- a/Sources/DropboxHelper.swift +++ b/Sources/DropboxHelper.swift @@ -98,7 +98,12 @@ internal extension DropboxFileProvider { } func upload_simple(_ targetPath: String, data: Data, modifiedDate: Date = Date(), overwrite: Bool, operation: FileOperationType, completionHandler: SimpleCompletionHandler) -> OperationHandle? { - assert(data.count < 150*1024*1024, "Maximum size of allowed size to upload is 150MB") + if data.count > 150 * 1024 * 1024 { + let error = FileProviderDropboxError(code: .payloadTooLarge, path: targetPath, errorDescription: nil) + completionHandler?(error) + self.delegateNotify(.create(path: targetPath), error: error) + return nil + } var requestDictionary = [String: Any]() let url: URL url = URL(string: "files/upload", relativeTo: contentURL)! @@ -125,6 +130,13 @@ internal extension DropboxFileProvider { } func upload_simple(_ targetPath: String, localFile: URL, modifiedDate: Date = Date(), overwrite: Bool, operation: FileOperationType, completionHandler: SimpleCompletionHandler) -> OperationHandle? { + let size = (try? localFile.resourceValues(forKeys: [.fileSizeKey]))?.fileSize ?? -1 + if size > 150 * 1024 * 1024 { + let error = FileProviderDropboxError(code: .payloadTooLarge, path: targetPath, errorDescription: nil) + completionHandler?(error) + self.delegateNotify(.create(path: targetPath), error: error) + return nil + } var requestDictionary = [String: Any]() let url: URL url = URL(string: "files/upload", relativeTo: contentURL)! diff --git a/Sources/FileObject.swift b/Sources/FileObject.swift index 4706318..9846a0f 100644 --- a/Sources/FileObject.swift +++ b/Sources/FileObject.swift @@ -135,7 +135,7 @@ open class FileObject { } } -/// Sorting FileObject array by given criteria, not thread-safe +/// Sorting FileObject array by given criteria, **not thread-safe** public struct FileObjectSorting { /// Determines sort kind by which item of File object @@ -222,6 +222,17 @@ public struct FileObjectSorting { } } +extension Array where Element: FileObject { + public func sorted(by type: FileObjectSorting.SortType, ascending: Bool = true, isDirectoriesFirst: Bool = false) -> [Element] { + let sorting = FileObjectSorting(type: type, ascending: ascending, isDirectoriesFirst: isDirectoriesFirst) + return sorting.sort(self) as! [Element] + } + + public mutating func sorted(by type: FileObjectSorting.SortType, ascending: Bool = true, isDirectoriesFirst: Bool = false) { + self = self.sorted(by: type, ascending: ascending, isDirectoriesFirst: isDirectoriesFirst) + } +} + extension URLFileResourceType { public init(fileTypeValue: FileAttributeType) { switch fileTypeValue { diff --git a/Sources/OneDriveFileProvide.swift b/Sources/OneDriveFileProvide.swift index 81b979b..a1d54a0 100644 --- a/Sources/OneDriveFileProvide.swift +++ b/Sources/OneDriveFileProvide.swift @@ -19,7 +19,7 @@ open class OneDriveFileProvider: NSObject, FileProviderBasicRemote { open let baseURL: URL? open var drive: String open var driveURL: URL { - return URL(string: "/drive/root:/", relativeTo: baseURL)! + return URL(string: "/drive/\(drive):/", relativeTo: baseURL)! } open var currentPath: String = "" diff --git a/Sources/OneDriveHelper.swift b/Sources/OneDriveHelper.swift index b9c0b8e..cdd4d4d 100644 --- a/Sources/OneDriveHelper.swift +++ b/Sources/OneDriveHelper.swift @@ -93,7 +93,12 @@ internal extension OneDriveFileProvider { } func upload_simple(_ targetPath: String, data: Data, modifiedDate: Date = Date(), overwrite: Bool, operation: FileOperationType, completionHandler: SimpleCompletionHandler) -> OperationHandle? { - assert(data.count < 100*1024*1024, "Maximum size of allowed size to upload is 100MB") + if data.count > 100 * 1024 * 1024 { + let error = FileProviderOneDriveError(code: .payloadTooLarge, path: targetPath, errorDescription: nil) + completionHandler?(error) + self.delegateNotify(.create(path: targetPath), error: error) + return nil + } let queryStr = overwrite ? "" : "?@name.conflictBehavior=fail" let url = URL(string: escaped(path: targetPath) + ":/content" + queryStr, relativeTo: driveURL)! var request = URLRequest(url: url) @@ -115,6 +120,13 @@ internal extension OneDriveFileProvider { } func upload_simple(_ targetPath: String, localFile: URL, modifiedDate: Date = Date(), overwrite: Bool, operation: FileOperationType, completionHandler: SimpleCompletionHandler) -> OperationHandle? { + let size = (try? localFile.resourceValues(forKeys: [.fileSizeKey]))?.fileSize ?? -1 + if size > 100 * 1024 * 1024 { + let error = FileProviderOneDriveError(code: .payloadTooLarge, path: targetPath, errorDescription: nil) + completionHandler?(error) + self.delegateNotify(.create(path: targetPath), error: error) + return nil + } let queryStr = overwrite ? "" : "?@name.conflictBehavior=fail" let url = URL(string: escaped(path: targetPath) + ":/content" + queryStr, relativeTo: driveURL)! var request = URLRequest(url: url)