Compare commits
183 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e72d7ff088 | |||
| ccb7961171 | |||
| e4fc6b24c0 | |||
| f22af8d002 | |||
| e5e5faa4e8 | |||
| f2cd571d7a | |||
| 4202f5e1bd | |||
| 3040215ce3 | |||
| bd2f2b3954 | |||
| 9d19768e1c | |||
| bcc774d3a8 | |||
| ad9768a584 | |||
| a089cbc21c | |||
| 090baa3b61 | |||
| fc75c85b14 | |||
| 6c34a4e9a8 | |||
| 37ce9c95fc | |||
| b39c1c4e82 | |||
| 8f0cbf8513 | |||
| 2690551b7f | |||
| a6550b0ec3 | |||
| 4b0fffc691 | |||
| a3a584c7d5 | |||
| 90f846c88f | |||
| be38e2731d | |||
| f384d267cb | |||
| 8e617c552d | |||
| 173fba56d7 | |||
| 09cb34352f | |||
| fd89a04c8e | |||
| 1bf42b489d | |||
| 50bbe7f2fa | |||
| 7e8bc05cb1 | |||
| 516144ead6 | |||
| e6b8e60321 | |||
| 5868766d6f | |||
| 8cc6bf56b6 | |||
| 729d9b9896 | |||
| 634a40cbc4 | |||
| 19f9113a00 | |||
| a3931982eb | |||
| 9b8db9e160 | |||
| 611366be7a | |||
| fec0346e49 | |||
| bd77c6ecd1 | |||
| f20bcf7e51 | |||
| 3c87dfb69b | |||
| d29826c56c | |||
| 3f09c6ee2c | |||
| b2b82abd16 | |||
| 0fb3fe5466 | |||
| 26038004ac | |||
| 463663275a | |||
| 7c25bcdef7 | |||
| df781dbe10 | |||
| f99d40c7b5 | |||
| 76e0865cdc | |||
| f3d049f608 | |||
| d0b4d13c01 | |||
| 783e58cf5c | |||
| fe0ff89ec6 | |||
| bf75db8f45 | |||
| 06c24d634d | |||
| 9d45b1f9c6 | |||
| 56bc1703ba | |||
| 76ff978df4 | |||
| 435ed37d93 | |||
| 3d563c3bfa | |||
| 628ed96654 | |||
| 9e4a803345 | |||
| 0e53036855 | |||
| 87317c1d42 | |||
| f0c6cf155a | |||
| 4c35e446a7 | |||
| 47516c63ad | |||
| 6cd5b7d8c5 | |||
| 598444c90f | |||
| 370866442a | |||
| 9cc9a6a96a | |||
| 7959edf671 | |||
| 2969af28b8 | |||
| d41e86b30a | |||
| 5d4414897b | |||
| aaa1def9e7 | |||
| 1827946a20 | |||
| 024a3637b4 | |||
| 0a9cce0cf9 | |||
| b20fc3efe5 | |||
| d379c563d8 | |||
| 682dd40072 | |||
| 3f35e600cd | |||
| 434945da87 | |||
| 9cd2fa2e3a | |||
| a3cb468c29 | |||
| 7be6985454 | |||
| a58f0ecbe6 | |||
| 04ac3e22e7 | |||
| c8de7fdb69 | |||
| 223dca1d1c | |||
| 229770108e | |||
| 42b879d4e2 | |||
| 5800c9a2ec | |||
| 7c9b2bf09a | |||
| ba08cdda1d | |||
| b380685932 | |||
| 5a5beb6891 | |||
| c7201a8be7 | |||
| d2a03369fd | |||
| d08098008a | |||
| 3c5303214e | |||
| ad8f333d16 | |||
| 5e8f653c52 | |||
| e4cd7ddf22 | |||
| 1efb0e3fe5 | |||
| 1292856646 | |||
| e39f9c29ec | |||
| f43199c22a | |||
| 3d44de0b40 | |||
| 21b5214481 | |||
| f0b4925db2 | |||
| 0da957d25f | |||
| 0de558c160 | |||
| cad68da076 | |||
| 39da09edd4 | |||
| 5129aee1b5 | |||
| 478f0819b5 | |||
| b2a7800f7c | |||
| 96c102d156 | |||
| 8aedd8e72a | |||
| 5b55debe8a | |||
| 0fa062a946 | |||
| 879f86c1f9 | |||
| 80d5f02bbd | |||
| b8a7721b2f | |||
| 44b4784cd3 | |||
| 7d9e2247f2 | |||
| 0ace562442 | |||
| 63d831ef90 | |||
| d6b91348a3 | |||
| fd9d4c1ab4 | |||
| 41e266c2a9 | |||
| 6127f4a7d9 | |||
| faca943beb | |||
| 9345436fa2 | |||
| 6228d88d41 | |||
| 5b395915a9 | |||
| e4f12a502b | |||
| 8faad0ec65 | |||
| bd5569d213 | |||
| 14ed279879 | |||
| fd293f7bdb | |||
| 3d9625e243 | |||
| 0d017ebfc4 | |||
| fd36df67f3 | |||
| 5be8c4ef5e | |||
| 0374fd7688 | |||
| 5ddfa43555 | |||
| 21850bb548 | |||
| b166e111e0 | |||
| 1dd7561215 | |||
| f94719deb0 | |||
| b13df0a977 | |||
| 24af7aa4c2 | |||
| 06039ad993 | |||
| d8fec3e346 | |||
| 5c93bc8731 | |||
| 61ba245189 | |||
| 02e6cd37dd | |||
| 55608fb8d0 | |||
| 3e3582f6fa | |||
| dd7a9d20b6 | |||
| d4a9b4a34f | |||
| 34c663e62c | |||
| 1415dda987 | |||
| cd465c1288 | |||
| a605b0cd85 | |||
| bf7043de29 | |||
| ff5e13931f | |||
| 75af738d2e | |||
| f54a1253e4 | |||
| 1394a92662 | |||
| dab171c755 | |||
| ea5de2e2aa |
@@ -1 +0,0 @@
|
||||
3.0
|
||||
+10
-11
@@ -1,5 +1,5 @@
|
||||
language: objective-c
|
||||
osx_image: xcode8.2
|
||||
osx_image: xcode9
|
||||
xcode_project: $PROJECTNAME.xcodeproj
|
||||
env:
|
||||
global:
|
||||
@@ -8,18 +8,16 @@ env:
|
||||
- IOS_FRAMEWORK_SCHEME="$PROJECTNAME iOS"
|
||||
- MACOS_FRAMEWORK_SCHEME="$PROJECTNAME OSX"
|
||||
- TVOS_FRAMEWORK_SCHEME="$PROJECTNAME tvOS"
|
||||
- TEST_FRAMEWORK_SCHEME="$PROJECTNAMETests"
|
||||
- IOS_SDK=iphonesimulator
|
||||
- MACOS_SDK=macosx
|
||||
- TVOS_SDK=appletvsimulator
|
||||
matrix:
|
||||
- DESTINATION="OS=10.2,name=iPad Air 2" SCHEME="$IOS_FRAMEWORK_SCHEME" SDK="$IOS_SDK" RUN_TESTS="NO" BUILD_EXAMPLE="NO" POD="YES" CARTHAGEDEPLOY="NO"
|
||||
- DESTINATION="OS=9.0,name=iPhone 6" SCHEME="$IOS_FRAMEWORK_SCHEME" SDK="$IOS_SDK" RUN_TESTS="NO" BUILD_EXAMPLE="NO" POD="NO" CARTHAGEDEPLOY="YES"
|
||||
- DESTINATION="OS=8.1,name=iPhone 4S" SCHEME="$IOS_FRAMEWORK_SCHEME" SDK="$IOS_SDK" RUN_TESTS="NO" BUILD_EXAMPLE="NO" POD="NO" CARTHAGEDEPLOY="NO"
|
||||
|
||||
- DESTINATION="OS=10.1,name=Apple TV 1080p" SCHEME="$TVOS_FRAMEWORK_SCHEME" SDK="$TVOS_SDK" RUN_TESTS="NO" BUILD_EXAMPLE="NO" POD="NO" CARTHAGEDEPLOY="NO"
|
||||
# - DESTINATION="OS=10.0,name=Apple TV 1080p" SCHEME="$TVOS_FRAMEWORK_SCHEME" SDK="$TVOS_SDK" RUN_TESTS="NO" BUILD_EXAMPLE="NO" POD="NO" CARTHAGEDEPLOY="NO"
|
||||
|
||||
- DESTINATION="arch=x86_64" SCHEME="$MACOS_FRAMEWORK_SCHEME" SDK="$MACOS_SDK" RUN_TESTS="NO" BUILD_EXAMPLE="NO" POD="NO" CARTHAGEDEPLOY="NO"
|
||||
- DESTINATION="OS=11.0,name=iPad Pro (10.5-inch)" SCHEME="$IOS_FRAMEWORK_SCHEME" SDK="$IOS_SDK" CARTHAGEDEPLOY="YES"
|
||||
- DESTINATION="OS=11.0,name=iPhone 8" SCHEME="$IOS_FRAMEWORK_SCHEME" SDK="$IOS_SDK" POD="YES"
|
||||
- DESTINATION="arch=x86_64" SCHEME="$MACOS_FRAMEWORK_SCHEME" SDK="$MACOS_SDK"
|
||||
- DESTINATION="arch=x86_64" SCHEME="FilesProviderTests" SDK="$MACOS_SDK" RUN_TESTS="YES"
|
||||
- DESTINATION="OS=11.0,name=Apple TV 1080p" SCHEME="$TVOS_FRAMEWORK_SCHEME" SDK="$TVOS_SDK"
|
||||
before_install:
|
||||
- gem install xcpretty --no-rdoc --no-ri --no-document --quiet
|
||||
- gem install cocoapods --no-rdoc --no-ri --no-document --quiet
|
||||
@@ -56,7 +54,7 @@ script:
|
||||
after_success:
|
||||
# Run `pod trunk push` if specified
|
||||
- if [ $POD == "YES" ] && [ -n "$TRAVIS_TAG" ]; then
|
||||
pod trunk push;
|
||||
pod trunk push --allow-warnings;
|
||||
fi
|
||||
|
||||
# - bash <(curl -s https://codecov.io/bash)
|
||||
@@ -75,6 +73,7 @@ deploy:
|
||||
file: $FRAMEWORK_NAME.zip
|
||||
skip_cleanup: true
|
||||
on:
|
||||
repo: amosavian/$PROJECTNAME
|
||||
# repo: amosavian/$PROJECTNAME
|
||||
repo: amosavian/FileProvider
|
||||
tags: true
|
||||
condition: "$CARTHAGEDEPLOY = YES"
|
||||
+273
@@ -0,0 +1,273 @@
|
||||

|
||||
|
||||
# Concepts and Design
|
||||
|
||||
## Protocols and base classes
|
||||
|
||||
Every provider class conforms to some of the protocols each defines which operations are doable by a particular provider.
|
||||
This allows developers to work with any provider class and to use a simple downcast to
|
||||
intended protocol via `as?` optional keyword and call intended method.
|
||||
|
||||
### FileProviderBasic
|
||||
|
||||
This protocols consists basic variables necessary for all providers and functions to query contents and status of provider.
|
||||
|
||||
`type` static property should return a string which is usually provider's name.
|
||||
Value can be usedfor display purposes and to compare two different instances' type.
|
||||
|
||||
`baseURL` determines root url of provider instance.
|
||||
Some providers doesn't map url to files thus this variable is set to nil. `url(of:)` default implementation
|
||||
uses this url to create a url which points to specified path.
|
||||
|
||||
`dispatch_queue` and `operation_queue` are dispatch and operation queues used to have async operation.
|
||||
As a rule of thumb, file operations are done in operation queue and querying are done by dispatch queue.
|
||||
Some objects and classes like `NSFileCoordinator` uses OperationQueue for async operations.
|
||||
|
||||
`delegate` is a used to inform controller about operation status/progress to update UI.
|
||||
Avoid calling delegate methods directly and use `delegateNotify()` method in your implementation instead.
|
||||
|
||||
`credential` property stores user and password necessary to access provider.
|
||||
Local provider would ignore it.
|
||||
|
||||
If listing query is paginated `contentsOfDirectory()` and `searchFiles()` may return both truncated
|
||||
list array and error in case 2nd page can't be retrieved.
|
||||
Thus it's safe to check `error` first to ensure list is complete. Truncated result is usable until full list is retrieved.
|
||||
|
||||
`storageProperties()` returns `VolumeObject` see below for more information.
|
||||
|
||||
When implementing `searchFiles()`, check `fileObject.mapPredicate()` using `query.evaluate(with:)`.
|
||||
You may use `query` parameter to create a search string if provider supports search functionality.
|
||||
|
||||
Practically, `searchFiles(path: path, recursive: false, query: NSPredicate(format: "TRUEPREDICATE"))` should be equal with `contentsOfDirectory()` method.
|
||||
Consequently, if provider enlisting and search backend are same (e.g. iCloud, OneDrive or Google)
|
||||
implement `contentsOfDirectory()` as a wrapper around `searchFiles()`,
|
||||
otherwise implement'em independently for optimization reason.
|
||||
|
||||
Avoid `isReachable()` to check connectivity and reachability.
|
||||
Instead do operation and allow it o fail if there is a connection problem.
|
||||
It may be deprecated at any time.
|
||||
|
||||
`url(of:)` and `relativePathOf(url:)` have default implementation which build a url using `baseURL` property.
|
||||
In case that `baseURL` is `nil`, it will wrap path inside a `URL` instance.
|
||||
You may override them if more functionality is needed (e.g. OneDrive) or appending path to baseURL can't be mapped to file url directly.
|
||||
|
||||
### FileProviderBasicRemote
|
||||
|
||||
Adds a `session` and `cache` object for providers that need internet connection and `URLSession` object.
|
||||
If your provider is a HTTP based api, subclass `HTTPFileProvider` class otherwise
|
||||
(e.g. FTP or SMB) conform it to this protocol.
|
||||
|
||||
### FileProviderOperations
|
||||
|
||||
This protocol encapsulates methods for copying, moving, renaming, removing and downloading/uploading files.
|
||||
|
||||
If your provider is a subclass of `HTTPFileProvider`, you may implement
|
||||
`request(for:, overwrite:, attributes:)` abstract method which handles default implementation for these methods.
|
||||
In this case, you need to create a `URLRequest` object based on `operation` parameter,
|
||||
usually done a switch case statement.
|
||||
See [`WebDAVFileProvder`](Sources/WebDAVFileProvider.swift) and [`OneDriveFileProvider`](Sources/OneDriveFileProvider) classes to see an example.
|
||||
|
||||
### FileProviderReadWrite
|
||||
|
||||
This protocol declares three methods to read and write files.
|
||||
|
||||
You must care about memory when using these functions. If you must handle big data,
|
||||
write it to a temporary file and use `copyItem(localFile:, to:)` or `copyItem(path:, toLocalURL:)` methods accordingly.
|
||||
|
||||
### FileProviderMonitor
|
||||
|
||||
This protocol allows developer to update UI when file list is changed.
|
||||
It's not implemented for all providers and some providers like FTP and WebDAV don't support such functionality.
|
||||
|
||||
If you are implementing it for a HTTP-based provider,
|
||||
use `longpoolSession` to create a monitor request to avoid expiring requests frequently.
|
||||
|
||||
Implementation details vary based on provider specifications.
|
||||
|
||||
### FileProvideUndoable
|
||||
|
||||
Implementing this protocol is a little hard as you must save operation inside `undoManager` for any operation.
|
||||
|
||||
|
||||
### FileProviderSharing
|
||||
|
||||
`publicLink(to:, completionHandler:)` method allows user to share file with other people.
|
||||
Not all providers support this functionality and implementation details vary based on provider specification.
|
||||
|
||||
### ExtendedFileProvider
|
||||
|
||||
This protocol provides a way to fetch files' thumbnail and metadata.
|
||||
Providers which have endpoint to get meta data (like Dropbox) implements this protocol.
|
||||
Due to extensive network overload of fetching thumbnail,
|
||||
It's recommended to check file using `thumbnailOfFileSupported(path)` method
|
||||
to find out either provider supports thumbnail generation for specified file or not.
|
||||
These methods only check file extension as a indicator and won't check file using provider.
|
||||
A `true` result does not indicate that the file really has a thumbnail or not.
|
||||
|
||||
Implementation of `thumbnailOfFile(path:, dimension:)` must provide a NSImage/UIImage with size
|
||||
according to `dimension` parameter. If server supports requesting thumbnail with specified size,
|
||||
implementation would pass requested dimension to server,
|
||||
otherwise implementation must resize image using `ExtendedFileProvider.scaleDown(image:, toSize:)` method to resize image.
|
||||
|
||||
`ExtendedFileProvider.convertToImage(pdfURL:, page:)` and `ExtendedFileProvider.convertToImage(pdfPage:)` methods can convert pdf file into an image.
|
||||
Please note `pdfURL` parameter must be a local file url.
|
||||
|
||||
`propertiesOfFile()` method will extract meta data of specified file and return it as a dictionary orders by `keys` parameter.
|
||||
Keys may vary according to file type, e.g. EXIF data for an image or ID3 tags for a music file.
|
||||
|
||||
To extend thumbnail generation behavior for local file to types which are not supported by Apple platform,
|
||||
you may change `LocalFileInformationGenerator` struct static properties.
|
||||
Some methods in this struct are unimplemented to allow developer to use third-party libraries
|
||||
to provide meta data and thumbnail for other types of file.
|
||||
|
||||
### FileProvider
|
||||
|
||||
This protocol does not define any method, but indicates that class conforms to `FileProviderBasic `,
|
||||
`FileProviderOperations`, `FileProviderReadWrite` and `NSCopying` protocols.
|
||||
|
||||
### FileOperationType
|
||||
|
||||
This enum holds operation type and associated information like source and destination path.
|
||||
Developer is exposed to this enum in delegate methods.
|
||||
Internally it's extensively used to refactor operation methods and to store operation info in
|
||||
related `URLSessionTask` inside `taskDescription` property.
|
||||
|
||||
As a associated enum, it can not be bridged to Objective-C.
|
||||
|
||||
### FileObject
|
||||
|
||||
`FileObject` class stores file properties in a dictionary with `URLResourceKey` as key type.
|
||||
All other properties are computed variables and simply cast value to a strict swift type.
|
||||
|
||||
Provider will create and return instance of `FileObject` class or its descendants in `contentsOfDirectory()`, `attributesOfItem()` and `searchFiles()` methods.
|
||||
Provider **must** set `name` and `path` properties.
|
||||
|
||||
`path`'s value can be a unix-style hierarchal path or other ways to point a file supported by server.
|
||||
Some providers like Dropbox and OneDrive define accessing to file by id or revision.
|
||||
As a convention, these alternative paths are structured like `type:identifier` e.g. `rev:abcd1234` or `id:abcd1234`.
|
||||
Google only allows to address file with `id:abcd1234` and does not provide unix-style path.
|
||||
|
||||
- **Important:** Never rely on `path` last component to extract file name, instead use `name` property.
|
||||
Providers like `Google` have only file id in path thus using `path.lastPathComponent` to display file name may lead to confusion and improper result.
|
||||
|
||||
### VolumeObject
|
||||
|
||||
`VolumeObject` class is identical to `FileObject` structurally but only uses `URLResourceKey`'s keys which begin with `volume`.
|
||||
An implementation of provider must return this enumerated in `storageProperties()` with properties like total size and free space of storage.
|
||||
|
||||
There is not correspondent key in storage for `usage` property,
|
||||
indeed it's calculated by subtracting available space from total space.
|
||||
|
||||
## Progress handling
|
||||
|
||||
Almost all methods return a `Progress` instance which encapsulates progress fraction and a way to cancel entire operation.
|
||||
It's upon your provider's implementation to update progress `totalUnitCount` and `completedUnitCount` properties and assign a cancellation handler to Progress object.
|
||||
Cancellation handler may call `Operation`'s or `URLSessionTask`'s `cancel()` method to interrupt operation.
|
||||
|
||||
A typical progress handling in this library is like this:
|
||||
|
||||
```swift
|
||||
// totalUnitCount must be set to file size for downloading/uploading operation.
|
||||
// totalUnitCount must be set to 1 for a simple remote file operation.
|
||||
let progress = Progress(totalUnitCount: size)
|
||||
// allow updating progress inside URLSession's delegate methods
|
||||
progress.setUserInfoObject(operation, forKey: .fileProvderOperationTypeKey)
|
||||
// kind must be set to .file for downloading/uploading operation.
|
||||
progress.kind = .file
|
||||
progress.setUserInfoObject(Progress.FileOperationKind.downloading, forKey: .fileOperationKindKey)
|
||||
// progres.cancel() will call task.cancel() method.
|
||||
progress.cancellationHandler = { [weak task] in
|
||||
task?.cancel()
|
||||
}
|
||||
// Set .startingTimeKey to calculate estimated remaining time and speed in delegate.
|
||||
progress.setUserInfoObject(Date(), forKey: .startingTimeKey)
|
||||
```
|
||||
|
||||
Please note `.fileProvderOperationTypeKey` and `.startingTimeKey` are custom user info assigned to progress
|
||||
to allow updating progress inside URLSession's delegate methods and calculating estimated remaining time and speed.
|
||||
Your implementation must set these user info objects if you are using `SessionDelegate` object.
|
||||
|
||||
You may update cancellation handler if another task is added.
|
||||
|
||||
## Error handling
|
||||
|
||||
This library doesn't manipulate errors returned by Foundation methods and uses `URLError` and `CococaError` to report error as far as there is a corresponding defined error.
|
||||
You can use `urlError(_ :, code:)` and `cocoaError(_:, code:)` convenience methods to create error object.
|
||||
|
||||
For HTTP providers, `FileProviderHTTPError` protocol defines a way to encapsulate HTTP status code and returned description by server.
|
||||
You may declare a struct conforming to `FileProviderHTTPError` with an initializer to interpret server response.
|
||||
`serverError(with:, path:, data:)` must be implemented in `HTTPFileProvider` subclasses and will be called by HTTP provider default implementation to digest error.
|
||||
|
||||
**NEVER** define a custom enum for errors. Instead use Foundation errors like `URLError` and `CococaError` as
|
||||
they provide comprehensive localized description for error.
|
||||
Alternatively use `FileProviderHTTPError` conforming struct for HTTP providers.
|
||||
|
||||
## Implementing HTTP-based Custom Provider
|
||||
|
||||
This library provide `HTTPFileProvider` abstract class to easily implement provider which connects to a cloud/server that
|
||||
uses http protocol for connection.
|
||||
That's almost all REST and web based providers like Dropbox, Box, Google Drive, etc.
|
||||
|
||||
`HTTPFileProvider` encapsulates much of downloading/uploading logic and provides `paginated` method
|
||||
to allow enlisting/searching files in providers which return result in progressively. (e.g. Dropbox and OneDrive)
|
||||
|
||||
By subclassing `HTTPFileProvider` class, you must override half a dozen of methods, mainly querying methods.
|
||||
Your implementation may cause a **crash** if you fail to override these methods.
|
||||
|
||||
### Methods and properties to override
|
||||
|
||||
`type` static property, which returns name of provider.
|
||||
|
||||
`init?(coder:)` decodes and initialize instance using `NSCoder`.
|
||||
Your implementation must read `aDecoder` correspondent keys and initialize a new object using your provider's initilizer.
|
||||
|
||||
`copy(with:)` method must create a new instance and assign properties from source (`self`) to copied object.
|
||||
|
||||
`contentsOfDirectory()` and `searchFiles()` methods must send listing query to server and decode json/xml response into a `FileObject`.
|
||||
Providers may subclass `FileObject` and implement an initializer to encapsulate decoding logic.
|
||||
If server response is paginated, use `paginated()` method. See below and inline help to find how to use it.
|
||||
|
||||
`attributesOfItem()` must send file attribute fetching query to server and decode response into a `FileObject` or its descendants instance.
|
||||
|
||||
`storageProperties()` does querying account/cloud quota and encapsulates it into a `VolumeObject` instance.
|
||||
You don't need to subclass `VolumeObject`.
|
||||
If your server does not support such functionality, simply call completion handler with `nil` as result.
|
||||
|
||||
`request(for:, overwrite:, attributes:)` creates a `URLRequest` for requested operation.
|
||||
It will be called by create, copy/move and remove functions.
|
||||
You may set `httpMethod` and `httpBody` and header values of request regarding operation type and associated variables.
|
||||
|
||||
- **Important:** NEVER forget to call `urlrequest.setValue(authentication: credential, with:)` to set provider's credential
|
||||
if server uses OAuth/OAuth2 authentication method.
|
||||
You may need to set other http headers according to server specifications.
|
||||
|
||||
- **Important**: `copyItem(path:, toLocalURL:)` and `copyItem(localFile:, to:)` methods will call `request(for:)` method with
|
||||
source/destination property set to a local file url, begins with `file://`.
|
||||
You must handle these separately.
|
||||
See `WebDAVProvider` [source](Sources/WebDAVFileProvider.swift) as an example.
|
||||
|
||||
`serverError(with:, path:, data:)` method will digest http status code and server response data to create an error conforming to `FileProviderHTTPError` protocol.
|
||||
|
||||
### Optional overridable methods
|
||||
|
||||
`isReachable()` default implementation tries to fetch storage properties and will return true if result is non-nil.
|
||||
You may need to override this method if server does not support `storageProperties()` or there is a more optimized way to check reachability.
|
||||
|
||||
`copyItem(localFile:, to:)` and `writeContents(path:, contents:)` can be overrided if server requires upload session.
|
||||
See `OneDriveProvider`'s [source](Sources/OneDriveProvider) to see how create and handle an upload session.
|
||||
|
||||
### Paginated enlisting
|
||||
|
||||
`paginated()` method defines an easy way to communicate to servers which list responses are paginated.
|
||||
Here is method signature:
|
||||
|
||||
`pageHandler` closure gets server response as `Data`, decodes it and returns `FileObject` array or an error,
|
||||
and if there is another sequel page, passes token of new page (a string tha can be a id or url) to `newToken`.
|
||||
If there is no more sequel page, `newToken` must be nil.
|
||||
|
||||
This token will be delivered to `requestHandler` closure which returns a `URLRequest` according to token.
|
||||
A nil token indicates it's the first page.
|
||||
`completionHandler` is the closure which user passed to `contentsOfDirectory()` or `searchFiles()` methods.
|
||||
|
||||
`pageHandler` must update `progress` by adding the number of new files enlisted to `completedUnitCount` property.
|
||||
This closure may filter results according to `query` parameter, using `query.evaluate(with:)`.
|
||||
@@ -0,0 +1,83 @@
|
||||
# Authentication
|
||||
|
||||
Dropbox and OneDrive are using OAuth2 authentication method. Here is sample codes to get Bearer token using [OAuthSwift](https://github.com/OAuthSwift/OAuthSwift)
|
||||
|
||||
## Handling url scheme in App delegate
|
||||
|
||||
Add these lines to your application delegate:
|
||||
|
||||
```swift
|
||||
extension AppDelegate {
|
||||
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {
|
||||
if url.host == "oauth-callback" {
|
||||
OAuthSwift.handle(url: url)
|
||||
}
|
||||
|
||||
// HANDLING OTHER PATERNS
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Dropbox
|
||||
|
||||
Your client id and secret must be given by Dropbox developer portal. Bearer tokens created by this method are permanent.
|
||||
|
||||
```swift
|
||||
let appScheme = "YOUR_APP_SCHEME"
|
||||
oauth = OAuth2Swift(consumerKey: "CLIENT_ID",
|
||||
consumerSecret: "CLIENT_SECRET",
|
||||
authorizeUrl: "https://www.dropbox.com/oauth2/authorize",
|
||||
responseType: "token")!
|
||||
oauth.authorizeURLHandler = SafariURLHandler(viewController: self, oauthSwift: oauth)
|
||||
_ = oauth.authorize(withCallbackURL: URL(string: "\(appScheme)://oauth-callback/dropbox")!,
|
||||
scope: "", state:"DROPBOX",
|
||||
success: { credential, response, parameters in
|
||||
let urlcredential = URLCredential(user: user ?? "anonymous", password: credential.oauthToken, persistence: .permanent)
|
||||
// TODO: Save credential in keychain
|
||||
// TODO: Create Dropbox provider using urlcredential
|
||||
}, failure: { error in
|
||||
print(error.localizedDescription)
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
## OneDrive
|
||||
|
||||
Your client id must be given by Microsoft developer portal. OneDrive doesn't need client secret for native apps, but will need to refresh token every one hour.
|
||||
|
||||
We must save refresh token in adition to bearer token and use it when we get `.unauthorized` 401 HTTP error in completion handlers to get a new bearer token.
|
||||
|
||||
```swift
|
||||
let appScheme = "YOUR_APP_SCHEME"
|
||||
oauth = OAuth2Swift.init(consumerKey: "CLIENT_ID",
|
||||
consumerSecret: "",
|
||||
authorizeUrl: "https://login.microsoftonline.com/common/oauth2/v2.0/authorize",
|
||||
accessTokenUrl: "https://login.microsoftonline.com/common/oauth2/v2.0/token",
|
||||
responseType: "code")!
|
||||
oauth.authorizeURLHandler = SafariURLHandler(viewController: self, oauthSwift: oauth)
|
||||
if let refreshToken = {SAVED_REFRESH_TOKEN} {
|
||||
oauth.renewAccessToken(withRefreshToken: token,
|
||||
success: { credential, response, parameters in
|
||||
let urlcredential = URLCredential(user: user ?? "anonymous", password: credential.oauthToken, persistence: .permanent)
|
||||
let refreshToken = credential.oauthRefreshToken
|
||||
// TODO: Save refreshToken in keychain
|
||||
// TODO: Save credential in keychain
|
||||
// TODO: Create OneDrive provider using urlcredential
|
||||
}, failure: { error in
|
||||
print(error.localizedDescription)
|
||||
// TODO: Clear saved refresh token and call this method again to get authorization token
|
||||
})
|
||||
} else {
|
||||
_ = oauth.authorize(
|
||||
withCallbackURL: URL(string: "\(appScheme)://oauth-callback/onedrive")!,
|
||||
scope: "offline_access User.Read Files.ReadWrite.All", state: "ONEDRIVE",
|
||||
success: { credential, response, parameters in
|
||||
let credential = URLCredential(user: user ?? "anonymous", password: credential.oauthToken, persistence: .permanent)
|
||||
// TODO: Save refreshToken in keychain
|
||||
// TODO: Save credential in keychain
|
||||
// TODO: Create OneDrive provider using credential
|
||||
}, failure: { error in
|
||||
print(error.localizedDescription)
|
||||
})
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,117 @@
|
||||
//
|
||||
// Sample-iOS.swift
|
||||
// FileProvider
|
||||
//
|
||||
// Created by Amir Abbas Mousavian.
|
||||
// Copyright © 2018 Mousavian. Distributed under MIT license.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
import FilesProvider
|
||||
|
||||
class ViewController: UIViewController, FileProviderDelegate {
|
||||
|
||||
let server: URL = URL(string: "https://server-webdav.com")!
|
||||
let username = "username"
|
||||
let password = "password"
|
||||
|
||||
var webdav: WebDAVFileProvider?
|
||||
|
||||
@IBOutlet weak var uploadProgressView: UIProgressView
|
||||
@IBOutlet weak var downloadProgressView: UIProgressView
|
||||
|
||||
override func viewDidLoad() {
|
||||
|
||||
super.viewDidLoad()
|
||||
|
||||
let credential = URLCredential(user: username, password: password, persistence: .permanent)
|
||||
|
||||
webdav = WebDAVFileProvider(baseURL: server, credential: credential)!
|
||||
webdav?.delegate = self as FileProviderDelegate
|
||||
}
|
||||
|
||||
override func didReceiveMemoryWarning() {
|
||||
super.didReceiveMemoryWarning()
|
||||
}
|
||||
|
||||
@IBAction func createFolder(_ sender: Any) {
|
||||
webdav?.create(folder: "new folder", at: "/", completionHandler: nil)
|
||||
}
|
||||
|
||||
@IBAction func createFile(_ sender: Any) {
|
||||
let data = "Hello world from sample.txt!".data(encoding: .utf8)
|
||||
webdav?.writeContents(path: "sample.txt", content: data, atomically: true, completionHandler: nil)
|
||||
}
|
||||
|
||||
@IBAction func getData(_ sender: Any) {
|
||||
webdav?.contents(path: "sample.txt", completionHandler: {
|
||||
contents, error in
|
||||
if let contents = contents {
|
||||
print(String(data: contents, encoding: .utf8))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@IBAction func remove(_ sender: Any) {
|
||||
webdav?.removeItem(path: "sample.txt", completionHandler: nil)
|
||||
}
|
||||
|
||||
@IBAction func download(_ sender: Any) {
|
||||
let localURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("fileprovider.png")
|
||||
let remotePath = "fileprovider.png"
|
||||
|
||||
let progress = webdav?.copyItem(path: remotePath, toLocalURL: localURL, completionHandler: nil)
|
||||
downloadProgressView.observedProgress = progress
|
||||
}
|
||||
|
||||
@IBAction func upload(_ sender: Any) {
|
||||
let localURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("fileprovider.png")
|
||||
let remotePath = "/fileprovider.png"
|
||||
|
||||
let progress = webdav?.copyItem(localFile: localURL, to: remotePath, completionHandler: nil)
|
||||
uploadProgressView.observedProgress = progress
|
||||
}
|
||||
|
||||
func fileproviderSucceed(_ fileProvider: FileProviderOperations, operation: FileOperationType) {
|
||||
switch operation {
|
||||
case .copy(source: let source, destination: let dest):
|
||||
print("\(source) copied to \(dest).")
|
||||
case .remove(path: let path):
|
||||
print("\(path) has been deleted.")
|
||||
default:
|
||||
if let destination = operation.destination {
|
||||
print("\(operation.actionDescription) from \(operation.source) to \(destination) succeed.")
|
||||
} else {
|
||||
print("\(operation.actionDescription) on \(operation.source) succeed.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func fileproviderFailed(_ fileProvider: FileProviderOperations, operation: FileOperationType, error: Error) {
|
||||
switch operation {
|
||||
case .copy(source: let source, destination: let dest):
|
||||
print("copying \(source) to \(dest) has been failed.")
|
||||
case .remove:
|
||||
print("file can't be deleted.")
|
||||
default:
|
||||
if let destination = operation.destination {
|
||||
print("\(operation.actionDescription) from \(operation.source) to \(destination) failed.")
|
||||
} else {
|
||||
print("\(operation.actionDescription) on \(operation.source) failed.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func fileproviderProgress(_ fileProvider: FileProviderOperations, operation: FileOperationType, progress: Float) {
|
||||
switch operation {
|
||||
case .copy(source: let source, destination: let dest) where dest.hasPrefix("file://"):
|
||||
print("Downloading \(source) to \((dest as NSString).lastPathComponent): \(progress * 100) completed.")
|
||||
case .copy(source: let source, destination: let dest) where source.hasPrefix("file://"):
|
||||
print("Uploading \((source as NSString).lastPathComponent) to \(dest): \(progress * 100) completed.")
|
||||
case .copy(source: let source, destination: let dest):
|
||||
print("Copy \(source) to \(dest): \(progress * 100) completed.")
|
||||
default:
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Before Width: | Height: | Size: 24 KiB After Width: | Height: | Size: 24 KiB |
@@ -1,5 +1,5 @@
|
||||
#
|
||||
# Be sure to run `pod spec lint FileProvider.podspec' to ensure this is a
|
||||
# Be sure to run `pod spec lint FilesProvider.podspec' to ensure this is a
|
||||
# valid spec and to remove all comments including this before submitting the spec.
|
||||
#
|
||||
# To learn more about Podspec attributes see http://docs.cocoapods.org/specification.html
|
||||
@@ -15,8 +15,8 @@ Pod::Spec.new do |s|
|
||||
# summary should be tweet-length, and the description more in depth.
|
||||
#
|
||||
|
||||
s.name = "FileProvider"
|
||||
s.version = "0.15.1"
|
||||
s.name = "FilesProvider"
|
||||
s.version = "0.25.1"
|
||||
s.summary = "FileManager replacement for Local and Remote (WebDAV/FTP/Dropbox/OneDrive/SMB2) files on iOS and macOS."
|
||||
|
||||
# This description is used to generate tags and improve search results.
|
||||
@@ -55,9 +55,8 @@ Pod::Spec.new do |s|
|
||||
# profile URL.
|
||||
#
|
||||
|
||||
s.author = { "Amir Abbas Mousavian" => "a.mosavian@gmail.com" }
|
||||
# Or just: s.author = "Amir Abbas Mousavian"
|
||||
# s.authors = { "Amir Abbas Mousavian" => "a.mosavian@gmail.com" }
|
||||
s.authors = { "Amir Abbas Mousavian" => "a.mosavian@gmail.com" }
|
||||
s.social_media_url = "https://twitter.com/amosavian"
|
||||
|
||||
# ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
|
||||
@@ -66,10 +65,7 @@ Pod::Spec.new do |s|
|
||||
# the deployment target. You can optionally include the target after the platform.
|
||||
#
|
||||
|
||||
# s.platform = :ios
|
||||
# s.platform = :ios, "8.0"
|
||||
|
||||
# When using multiple platforms
|
||||
s.swift_version = "4.1"
|
||||
s.ios.deployment_target = "8.0"
|
||||
s.osx.deployment_target = "10.10"
|
||||
# s.watchos.deployment_target = "2.0"
|
||||
@@ -120,9 +116,12 @@ Pod::Spec.new do |s|
|
||||
#
|
||||
|
||||
# s.framework = "SomeFramework"
|
||||
# s.frameworks = "SomeFramework", "AnotherFramework"
|
||||
s.frameworks = "AVFoundation", "ImageIO", "CoreGraphics"
|
||||
s.ios.framework = "UIKit"
|
||||
s.tvos.framework = "UIKit"
|
||||
s.osx.framework = "AppKit"
|
||||
|
||||
# s.library = "iconv"
|
||||
s.library = "xml2"
|
||||
# s.libraries = "iconv", "xml2"
|
||||
|
||||
|
||||
+265
-166
@@ -38,6 +38,12 @@
|
||||
7936BC121E880F5700A6C81C /* FTPFileProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7936BC111E880F5700A6C81C /* FTPFileProvider.swift */; };
|
||||
7936BC131E880F5700A6C81C /* FTPFileProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7936BC111E880F5700A6C81C /* FTPFileProvider.swift */; };
|
||||
7936BC141E880F5700A6C81C /* FTPFileProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7936BC111E880F5700A6C81C /* FTPFileProvider.swift */; };
|
||||
793CCE2A1F4B8C5C00BC8288 /* FoundationExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 793CCE291F4B8C5C00BC8288 /* FoundationExtensions.swift */; };
|
||||
793CCE2B1F4B8C5C00BC8288 /* FoundationExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 793CCE291F4B8C5C00BC8288 /* FoundationExtensions.swift */; };
|
||||
793CCE2C1F4B8C5C00BC8288 /* FoundationExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 793CCE291F4B8C5C00BC8288 /* FoundationExtensions.swift */; };
|
||||
793CCE2E1F4B8C7B00BC8288 /* HashMAC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 793CCE2D1F4B8C7B00BC8288 /* HashMAC.swift */; };
|
||||
793CCE2F1F4B8C7B00BC8288 /* HashMAC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 793CCE2D1F4B8C7B00BC8288 /* HashMAC.swift */; };
|
||||
793CCE301F4B8C7B00BC8288 /* HashMAC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 793CCE2D1F4B8C7B00BC8288 /* HashMAC.swift */; };
|
||||
79480FF61E3ABDD0007E7275 /* CloudFileProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79480FF51E3ABDD0007E7275 /* CloudFileProvider.swift */; };
|
||||
79480FF71E3ABDD0007E7275 /* CloudFileProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79480FF51E3ABDD0007E7275 /* CloudFileProvider.swift */; };
|
||||
79480FF81E3ABDD0007E7275 /* CloudFileProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79480FF51E3ABDD0007E7275 /* CloudFileProvider.swift */; };
|
||||
@@ -50,9 +56,9 @@
|
||||
794C220E1D591A4B00EC49B8 /* SMB2QueryTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 794C220D1D591A4B00EC49B8 /* SMB2QueryTypes.swift */; };
|
||||
794C220F1D591A4B00EC49B8 /* SMB2QueryTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 794C220D1D591A4B00EC49B8 /* SMB2QueryTypes.swift */; };
|
||||
794C22101D591A4B00EC49B8 /* SMB2QueryTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 794C220D1D591A4B00EC49B8 /* SMB2QueryTypes.swift */; };
|
||||
796807551E7BF17E00BBB87B /* FileProviderExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 796807541E7BF17E00BBB87B /* FileProviderExtensions.swift */; };
|
||||
796807561E7BF17E00BBB87B /* FileProviderExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 796807541E7BF17E00BBB87B /* FileProviderExtensions.swift */; };
|
||||
796807571E7BF17E00BBB87B /* FileProviderExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 796807541E7BF17E00BBB87B /* FileProviderExtensions.swift */; };
|
||||
7958155A1F478ED9003344DD /* HTTPFileProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 795815591F478ED9003344DD /* HTTPFileProvider.swift */; };
|
||||
7958155B1F478ED9003344DD /* HTTPFileProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 795815591F478ED9003344DD /* HTTPFileProvider.swift */; };
|
||||
7958155C1F478ED9003344DD /* HTTPFileProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 795815591F478ED9003344DD /* HTTPFileProvider.swift */; };
|
||||
798654331E8874BC002FA550 /* FTPHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 798654321E8874BC002FA550 /* FTPHelper.swift */; };
|
||||
799396AA1D48C02300086753 /* DropboxFileProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 799396931D48C02300086753 /* DropboxFileProvider.swift */; };
|
||||
799396AB1D48C02300086753 /* DropboxFileProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 799396931D48C02300086753 /* DropboxFileProvider.swift */; };
|
||||
@@ -66,9 +72,6 @@
|
||||
799396B91D48C02300086753 /* SMBFileProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 799396981D48C02300086753 /* SMBFileProvider.swift */; };
|
||||
799396BA1D48C02300086753 /* SMBFileProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 799396981D48C02300086753 /* SMBFileProvider.swift */; };
|
||||
799396BB1D48C02300086753 /* SMBFileProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 799396981D48C02300086753 /* SMBFileProvider.swift */; };
|
||||
799396BC1D48C02300086753 /* CIFSTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7993969A1D48C02300086753 /* CIFSTypes.swift */; };
|
||||
799396BD1D48C02300086753 /* CIFSTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7993969A1D48C02300086753 /* CIFSTypes.swift */; };
|
||||
799396BE1D48C02300086753 /* CIFSTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7993969A1D48C02300086753 /* CIFSTypes.swift */; };
|
||||
799396BF1D48C02300086753 /* SMB2DataTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7993969B1D48C02300086753 /* SMB2DataTypes.swift */; };
|
||||
799396C01D48C02300086753 /* SMB2DataTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7993969B1D48C02300086753 /* SMB2DataTypes.swift */; };
|
||||
799396C11D48C02300086753 /* SMB2DataTypes.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7993969B1D48C02300086753 /* SMB2DataTypes.swift */; };
|
||||
@@ -116,12 +119,14 @@
|
||||
79BD63BE1E2CC3C20035128C /* ImageIO.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 79BD63BD1E2CC3C20035128C /* ImageIO.framework */; };
|
||||
79BD63C01E2CC3CD0035128C /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 79BD63BF1E2CC3CD0035128C /* CoreGraphics.framework */; };
|
||||
79BD63C21E2CC3D30035128C /* AVFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 79BD63C11E2CC3D30035128C /* AVFoundation.framework */; };
|
||||
79BD63C51E2D17880035128C /* OneDriveFileProvide.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79BD63C31E2D17880035128C /* OneDriveFileProvide.swift */; };
|
||||
79BD63C61E2D17880035128C /* OneDriveFileProvide.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79BD63C31E2D17880035128C /* OneDriveFileProvide.swift */; };
|
||||
79BD63C71E2D17880035128C /* OneDriveFileProvide.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79BD63C31E2D17880035128C /* OneDriveFileProvide.swift */; };
|
||||
79BD63C51E2D17880035128C /* OneDriveFileProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79BD63C31E2D17880035128C /* OneDriveFileProvider.swift */; };
|
||||
79BD63C61E2D17880035128C /* OneDriveFileProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79BD63C31E2D17880035128C /* OneDriveFileProvider.swift */; };
|
||||
79BD63C71E2D17880035128C /* OneDriveFileProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79BD63C31E2D17880035128C /* OneDriveFileProvider.swift */; };
|
||||
79BD63C81E2D17880035128C /* OneDriveHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79BD63C41E2D17880035128C /* OneDriveHelper.swift */; };
|
||||
79BD63C91E2D17880035128C /* OneDriveHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79BD63C41E2D17880035128C /* OneDriveHelper.swift */; };
|
||||
79BD63CA1E2D17880035128C /* OneDriveHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79BD63C41E2D17880035128C /* OneDriveHelper.swift */; };
|
||||
79D903541FAB647400D61D31 /* FilesProviderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79D903531FAB647400D61D31 /* FilesProviderTests.swift */; };
|
||||
79D903561FAB647400D61D31 /* FilesProvider.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 799396751D48B80D00086753 /* FilesProvider.framework */; };
|
||||
79F4678B1E8B80F200C91A85 /* FTPHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 798654321E8874BC002FA550 /* FTPHelper.swift */; };
|
||||
79F4678C1E8B80F200C91A85 /* FTPHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 798654321E8874BC002FA550 /* FTPHelper.swift */; };
|
||||
79F5745B1DFDB10B00179ABF /* FileObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79F5745A1DFDB10A00179ABF /* FileObject.swift */; };
|
||||
@@ -129,6 +134,16 @@
|
||||
79F5745D1DFDB10B00179ABF /* FileObject.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79F5745A1DFDB10A00179ABF /* FileObject.swift */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXContainerItemProxy section */
|
||||
79D903571FAB647400D61D31 /* PBXContainerItemProxy */ = {
|
||||
isa = PBXContainerItemProxy;
|
||||
containerPortal = 7993965C1D48B7BF00086753 /* Project object */;
|
||||
proxyType = 1;
|
||||
remoteGlobalIDString = 799396741D48B80D00086753;
|
||||
remoteInfo = "FilesProvider OSX";
|
||||
};
|
||||
/* End PBXContainerItemProxy section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
7902C0851D61B56D00564440 /* RemoteSession.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RemoteSession.swift; sourceTree = "<group>"; };
|
||||
791950F41DE58A5400B4426E /* libxml2.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libxml2.tbd; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS10.1.sdk/usr/lib/libxml2.tbd; sourceTree = DEVELOPER_DIR; };
|
||||
@@ -140,15 +155,17 @@
|
||||
7924B1A81D89F79200589DB7 /* FPSStreamTask.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FPSStreamTask.swift; sourceTree = "<group>"; };
|
||||
792572401DF23BDA006A1526 /* LocalHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LocalHelper.swift; sourceTree = "<group>"; };
|
||||
7936BC111E880F5700A6C81C /* FTPFileProvider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FTPFileProvider.swift; sourceTree = "<group>"; };
|
||||
793CCE291F4B8C5C00BC8288 /* FoundationExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FoundationExtensions.swift; sourceTree = "<group>"; };
|
||||
793CCE2D1F4B8C7B00BC8288 /* HashMAC.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HashMAC.swift; sourceTree = "<group>"; };
|
||||
79480FF51E3ABDD0007E7275 /* CloudFileProvider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CloudFileProvider.swift; sourceTree = "<group>"; };
|
||||
794C21FD1D58912A00EC49B8 /* DropboxHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DropboxHelper.swift; sourceTree = "<group>"; };
|
||||
794C22091D5893F800EC49B8 /* SMB2Notification.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SMB2Notification.swift; sourceTree = "<group>"; };
|
||||
794C220D1D591A4B00EC49B8 /* SMB2QueryTypes.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SMB2QueryTypes.swift; sourceTree = "<group>"; };
|
||||
796807541E7BF17E00BBB87B /* FileProviderExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FileProviderExtensions.swift; sourceTree = "<group>"; };
|
||||
795815591F478ED9003344DD /* HTTPFileProvider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HTTPFileProvider.swift; sourceTree = "<group>"; };
|
||||
798654321E8874BC002FA550 /* FTPHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FTPHelper.swift; sourceTree = "<group>"; };
|
||||
799396671D48B7F600086753 /* FileProvider.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = FileProvider.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
799396751D48B80D00086753 /* FileProvider.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = FileProvider.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
799396821D48B82700086753 /* FileProvider.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = FileProvider.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
799396671D48B7F600086753 /* FilesProvider.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = FilesProvider.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
799396751D48B80D00086753 /* FilesProvider.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = FilesProvider.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
799396821D48B82700086753 /* FilesProvider.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = FilesProvider.framework; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
7993968B1D48B8C700086753 /* Info-iOS.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "Info-iOS.plist"; sourceTree = "<group>"; };
|
||||
7993968C1D48B8C700086753 /* Info-MacOS.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "Info-MacOS.plist"; sourceTree = "<group>"; };
|
||||
7993968D1D48B8C700086753 /* Info-tvOS.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "Info-tvOS.plist"; sourceTree = "<group>"; };
|
||||
@@ -182,8 +199,11 @@
|
||||
79BD63BD1E2CC3C20035128C /* ImageIO.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ImageIO.framework; path = Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS10.1.sdk/System/Library/Frameworks/ImageIO.framework; sourceTree = DEVELOPER_DIR; };
|
||||
79BD63BF1E2CC3CD0035128C /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS10.1.sdk/System/Library/Frameworks/CoreGraphics.framework; sourceTree = DEVELOPER_DIR; };
|
||||
79BD63C11E2CC3D30035128C /* AVFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AVFoundation.framework; path = Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS10.1.sdk/System/Library/Frameworks/AVFoundation.framework; sourceTree = DEVELOPER_DIR; };
|
||||
79BD63C31E2D17880035128C /* OneDriveFileProvide.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OneDriveFileProvide.swift; sourceTree = "<group>"; };
|
||||
79BD63C31E2D17880035128C /* OneDriveFileProvider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OneDriveFileProvider.swift; sourceTree = "<group>"; };
|
||||
79BD63C41E2D17880035128C /* OneDriveHelper.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OneDriveHelper.swift; sourceTree = "<group>"; };
|
||||
79D903511FAB647400D61D31 /* FilesProviderTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = FilesProviderTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
|
||||
79D903531FAB647400D61D31 /* FilesProviderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FilesProviderTests.swift; sourceTree = "<group>"; };
|
||||
79D903551FAB647400D61D31 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
|
||||
79F5745A1DFDB10A00179ABF /* FileObject.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FileObject.swift; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
@@ -221,6 +241,14 @@
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
79D9034E1FAB647400D61D31 /* Frameworks */ = {
|
||||
isa = PBXFrameworksBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
79D903561FAB647400D61D31 /* FilesProvider.framework in Frameworks */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
/* End PBXFrameworksBuildPhase section */
|
||||
|
||||
/* Begin PBXGroup section */
|
||||
@@ -255,12 +283,22 @@
|
||||
path = AEXML;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
793CCE281F4B8C3600BC8288 /* Extensions */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
793CCE291F4B8C5C00BC8288 /* FoundationExtensions.swift */,
|
||||
793CCE2D1F4B8C7B00BC8288 /* HashMAC.swift */,
|
||||
);
|
||||
path = Extensions;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
7993965B1D48B7BF00086753 = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
79E34A101E2AC6C600E1293B /* Extra */,
|
||||
799396911D48C02300086753 /* Sources */,
|
||||
7993968A1D48B8C700086753 /* Pod */,
|
||||
79D903521FAB647400D61D31 /* Tests */,
|
||||
799396681D48B7F600086753 /* Products */,
|
||||
791950F31DE58A5300B4426E /* Frameworks */,
|
||||
);
|
||||
@@ -269,9 +307,10 @@
|
||||
799396681D48B7F600086753 /* Products */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
799396671D48B7F600086753 /* FileProvider.framework */,
|
||||
799396751D48B80D00086753 /* FileProvider.framework */,
|
||||
799396821D48B82700086753 /* FileProvider.framework */,
|
||||
799396671D48B7F600086753 /* FilesProvider.framework */,
|
||||
799396751D48B80D00086753 /* FilesProvider.framework */,
|
||||
799396821D48B82700086753 /* FilesProvider.framework */,
|
||||
79D903511FAB647400D61D31 /* FilesProviderTests.xctest */,
|
||||
);
|
||||
name = Products;
|
||||
sourceTree = "<group>";
|
||||
@@ -290,26 +329,27 @@
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
7924B18B1D89DAE000589DB7 /* AEXML */,
|
||||
793CCE281F4B8C3600BC8288 /* Extensions */,
|
||||
799396991D48C02300086753 /* SMBTypes */,
|
||||
799396941D48C02300086753 /* FileProvider.h */,
|
||||
799396951D48C02300086753 /* FileProvider.swift */,
|
||||
796807541E7BF17E00BBB87B /* FileProviderExtensions.swift */,
|
||||
79F5745A1DFDB10A00179ABF /* FileObject.swift */,
|
||||
799396961D48C02300086753 /* LocalFileProvider.swift */,
|
||||
792572401DF23BDA006A1526 /* LocalHelper.swift */,
|
||||
79480FF51E3ABDD0007E7275 /* CloudFileProvider.swift */,
|
||||
79BD638B1E2CC2300035128C /* ExtendedLocalFileProvider.swift */,
|
||||
7924B1A81D89F79200589DB7 /* FPSStreamTask.swift */,
|
||||
7902C0851D61B56D00564440 /* RemoteSession.swift */,
|
||||
795815591F478ED9003344DD /* HTTPFileProvider.swift */,
|
||||
799396931D48C02300086753 /* DropboxFileProvider.swift */,
|
||||
794C21FD1D58912A00EC49B8 /* DropboxHelper.swift */,
|
||||
79BD63C31E2D17880035128C /* OneDriveFileProvider.swift */,
|
||||
79BD63C41E2D17880035128C /* OneDriveHelper.swift */,
|
||||
799396A61D48C02300086753 /* WebDAVFileProvider.swift */,
|
||||
7936BC111E880F5700A6C81C /* FTPFileProvider.swift */,
|
||||
798654321E8874BC002FA550 /* FTPHelper.swift */,
|
||||
79BD63C31E2D17880035128C /* OneDriveFileProvide.swift */,
|
||||
79BD63C41E2D17880035128C /* OneDriveHelper.swift */,
|
||||
7924B1A81D89F79200589DB7 /* FPSStreamTask.swift */,
|
||||
799396971D48C02300086753 /* SMBClient.swift */,
|
||||
799396981D48C02300086753 /* SMBFileProvider.swift */,
|
||||
799396A61D48C02300086753 /* WebDAVFileProvider.swift */,
|
||||
);
|
||||
path = Sources;
|
||||
sourceTree = "<group>";
|
||||
@@ -334,6 +374,15 @@
|
||||
path = SMBTypes;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
79D903521FAB647400D61D31 /* Tests */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
79D903531FAB647400D61D31 /* FilesProviderTests.swift */,
|
||||
79D903551FAB647400D61D31 /* Info.plist */,
|
||||
);
|
||||
path = Tests;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
79E34A101E2AC6C600E1293B /* Extra */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
@@ -368,9 +417,9 @@
|
||||
/* End PBXHeadersBuildPhase section */
|
||||
|
||||
/* Begin PBXNativeTarget section */
|
||||
799396661D48B7F600086753 /* FileProvider iOS */ = {
|
||||
799396661D48B7F600086753 /* FilesProvider iOS */ = {
|
||||
isa = PBXNativeTarget;
|
||||
buildConfigurationList = 7993966F1D48B7F600086753 /* Build configuration list for PBXNativeTarget "FileProvider iOS" */;
|
||||
buildConfigurationList = 7993966F1D48B7F600086753 /* Build configuration list for PBXNativeTarget "FilesProvider iOS" */;
|
||||
buildPhases = (
|
||||
799396621D48B7F600086753 /* Sources */,
|
||||
799396631D48B7F600086753 /* Frameworks */,
|
||||
@@ -381,14 +430,14 @@
|
||||
);
|
||||
dependencies = (
|
||||
);
|
||||
name = "FileProvider iOS";
|
||||
name = "FilesProvider iOS";
|
||||
productName = "FileProvider iOS";
|
||||
productReference = 799396671D48B7F600086753 /* FileProvider.framework */;
|
||||
productReference = 799396671D48B7F600086753 /* FilesProvider.framework */;
|
||||
productType = "com.apple.product-type.framework";
|
||||
};
|
||||
799396741D48B80D00086753 /* FileProvider OSX */ = {
|
||||
799396741D48B80D00086753 /* FilesProvider OSX */ = {
|
||||
isa = PBXNativeTarget;
|
||||
buildConfigurationList = 7993967A1D48B80D00086753 /* Build configuration list for PBXNativeTarget "FileProvider OSX" */;
|
||||
buildConfigurationList = 7993967A1D48B80D00086753 /* Build configuration list for PBXNativeTarget "FilesProvider OSX" */;
|
||||
buildPhases = (
|
||||
799396701D48B80D00086753 /* Sources */,
|
||||
799396711D48B80D00086753 /* Frameworks */,
|
||||
@@ -399,14 +448,14 @@
|
||||
);
|
||||
dependencies = (
|
||||
);
|
||||
name = "FileProvider OSX";
|
||||
name = "FilesProvider OSX";
|
||||
productName = "FileProvider OSX";
|
||||
productReference = 799396751D48B80D00086753 /* FileProvider.framework */;
|
||||
productReference = 799396751D48B80D00086753 /* FilesProvider.framework */;
|
||||
productType = "com.apple.product-type.framework";
|
||||
};
|
||||
799396811D48B82700086753 /* FileProvider tvOS */ = {
|
||||
799396811D48B82700086753 /* FilesProvider tvOS */ = {
|
||||
isa = PBXNativeTarget;
|
||||
buildConfigurationList = 799396871D48B82700086753 /* Build configuration list for PBXNativeTarget "FileProvider tvOS" */;
|
||||
buildConfigurationList = 799396871D48B82700086753 /* Build configuration list for PBXNativeTarget "FilesProvider tvOS" */;
|
||||
buildPhases = (
|
||||
7993967D1D48B82700086753 /* Sources */,
|
||||
7993967E1D48B82700086753 /* Frameworks */,
|
||||
@@ -417,22 +466,41 @@
|
||||
);
|
||||
dependencies = (
|
||||
);
|
||||
name = "FileProvider tvOS";
|
||||
name = "FilesProvider tvOS";
|
||||
productName = "FileProvider tvOS";
|
||||
productReference = 799396821D48B82700086753 /* FileProvider.framework */;
|
||||
productReference = 799396821D48B82700086753 /* FilesProvider.framework */;
|
||||
productType = "com.apple.product-type.framework";
|
||||
};
|
||||
79D903501FAB647400D61D31 /* FilesProviderTests */ = {
|
||||
isa = PBXNativeTarget;
|
||||
buildConfigurationList = 79D9035B1FAB647400D61D31 /* Build configuration list for PBXNativeTarget "FilesProviderTests" */;
|
||||
buildPhases = (
|
||||
79D9034D1FAB647400D61D31 /* Sources */,
|
||||
79D9034E1FAB647400D61D31 /* Frameworks */,
|
||||
79D9034F1FAB647400D61D31 /* Resources */,
|
||||
);
|
||||
buildRules = (
|
||||
);
|
||||
dependencies = (
|
||||
79D903581FAB647400D61D31 /* PBXTargetDependency */,
|
||||
);
|
||||
name = FilesProviderTests;
|
||||
productName = FilesProviderTests;
|
||||
productReference = 79D903511FAB647400D61D31 /* FilesProviderTests.xctest */;
|
||||
productType = "com.apple.product-type.bundle.unit-test";
|
||||
};
|
||||
/* End PBXNativeTarget section */
|
||||
|
||||
/* Begin PBXProject section */
|
||||
7993965C1D48B7BF00086753 /* Project object */ = {
|
||||
isa = PBXProject;
|
||||
attributes = {
|
||||
LastUpgradeCheck = 0810;
|
||||
LastSwiftUpdateCheck = 0910;
|
||||
LastUpgradeCheck = 0930;
|
||||
TargetAttributes = {
|
||||
799396661D48B7F600086753 = {
|
||||
CreatedOnToolsVersion = 7.3.1;
|
||||
LastSwiftMigration = 0800;
|
||||
LastSwiftMigration = 0900;
|
||||
};
|
||||
799396741D48B80D00086753 = {
|
||||
CreatedOnToolsVersion = 7.3.1;
|
||||
@@ -440,9 +508,13 @@
|
||||
799396811D48B82700086753 = {
|
||||
CreatedOnToolsVersion = 7.3.1;
|
||||
};
|
||||
79D903501FAB647400D61D31 = {
|
||||
CreatedOnToolsVersion = 9.1;
|
||||
ProvisioningStyle = Automatic;
|
||||
};
|
||||
};
|
||||
};
|
||||
buildConfigurationList = 7993965F1D48B7BF00086753 /* Build configuration list for PBXProject "FileProvider" */;
|
||||
buildConfigurationList = 7993965F1D48B7BF00086753 /* Build configuration list for PBXProject "FilesProvider" */;
|
||||
compatibilityVersion = "Xcode 3.2";
|
||||
developmentRegion = English;
|
||||
hasScannedForEncodings = 0;
|
||||
@@ -454,9 +526,10 @@
|
||||
projectDirPath = "";
|
||||
projectRoot = "";
|
||||
targets = (
|
||||
799396661D48B7F600086753 /* FileProvider iOS */,
|
||||
799396741D48B80D00086753 /* FileProvider OSX */,
|
||||
799396811D48B82700086753 /* FileProvider tvOS */,
|
||||
799396661D48B7F600086753 /* FilesProvider iOS */,
|
||||
799396741D48B80D00086753 /* FilesProvider OSX */,
|
||||
799396811D48B82700086753 /* FilesProvider tvOS */,
|
||||
79D903501FAB647400D61D31 /* FilesProviderTests */,
|
||||
);
|
||||
};
|
||||
/* End PBXProject section */
|
||||
@@ -483,6 +556,13 @@
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
79D9034F1FAB647400D61D31 /* Resources */ = {
|
||||
isa = PBXResourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
/* End PBXResourcesBuildPhase section */
|
||||
|
||||
/* Begin PBXSourcesBuildPhase section */
|
||||
@@ -490,7 +570,6 @@
|
||||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
796807551E7BF17E00BBB87B /* FileProviderExtensions.swift in Sources */,
|
||||
799396B31D48C02300086753 /* LocalFileProvider.swift in Sources */,
|
||||
799396D41D48C02300086753 /* SMB2Tree.swift in Sources */,
|
||||
7924B1A21D89DAE000589DB7 /* Options.swift in Sources */,
|
||||
@@ -498,10 +577,11 @@
|
||||
7936BC121E880F5700A6C81C /* FTPFileProvider.swift in Sources */,
|
||||
79480FF61E3ABDD0007E7275 /* CloudFileProvider.swift in Sources */,
|
||||
79F5745B1DFDB10B00179ABF /* FileObject.swift in Sources */,
|
||||
79BD63C51E2D17880035128C /* OneDriveFileProvide.swift in Sources */,
|
||||
79BD63C51E2D17880035128C /* OneDriveFileProvider.swift in Sources */,
|
||||
7924B1991D89DAE000589DB7 /* Element.swift in Sources */,
|
||||
799396C81D48C02300086753 /* SMB2IOCtl.swift in Sources */,
|
||||
799396D71D48C02300086753 /* SMB2Types.swift in Sources */,
|
||||
7958155A1F478ED9003344DD /* HTTPFileProvider.swift in Sources */,
|
||||
798654331E8874BC002FA550 /* FTPHelper.swift in Sources */,
|
||||
7924B1B21D89FCDA00589DB7 /* FPSStreamTask.swift in Sources */,
|
||||
799396C51D48C02300086753 /* SMB2FileOperation.swift in Sources */,
|
||||
@@ -510,7 +590,6 @@
|
||||
799396B91D48C02300086753 /* SMBFileProvider.swift in Sources */,
|
||||
794C220E1D591A4B00EC49B8 /* SMB2QueryTypes.swift in Sources */,
|
||||
794C21FE1D58912A00EC49B8 /* DropboxHelper.swift in Sources */,
|
||||
799396BC1D48C02300086753 /* CIFSTypes.swift in Sources */,
|
||||
794C220A1D5893F800EC49B8 /* SMB2Notification.swift in Sources */,
|
||||
799396D11D48C02300086753 /* SMB2SetInfo.swift in Sources */,
|
||||
7924B1961D89DAE000589DB7 /* Document.swift in Sources */,
|
||||
@@ -519,10 +598,12 @@
|
||||
799396E01D48C02300086753 /* WebDAVFileProvider.swift in Sources */,
|
||||
799396DA1D48C02300086753 /* SMBErrorType.swift in Sources */,
|
||||
799396C21D48C02300086753 /* SMB2FileHandle.swift in Sources */,
|
||||
793CCE2E1F4B8C7B00BC8288 /* HashMAC.swift in Sources */,
|
||||
799396CB1D48C02300086753 /* SMB2Query.swift in Sources */,
|
||||
799396AA1D48C02300086753 /* DropboxFileProvider.swift in Sources */,
|
||||
79BD638C1E2CC2300035128C /* ExtendedLocalFileProvider.swift in Sources */,
|
||||
7924B1B31D89FD6400589DB7 /* SMBClient.swift in Sources */,
|
||||
793CCE2A1F4B8C5C00BC8288 /* FoundationExtensions.swift in Sources */,
|
||||
7924B1A51D89DAE000589DB7 /* Parser.swift in Sources */,
|
||||
799396B01D48C02300086753 /* FileProvider.swift in Sources */,
|
||||
7924B19C1D89DAE000589DB7 /* Error.swift in Sources */,
|
||||
@@ -533,7 +614,6 @@
|
||||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
796807561E7BF17E00BBB87B /* FileProviderExtensions.swift in Sources */,
|
||||
799396B41D48C02300086753 /* LocalFileProvider.swift in Sources */,
|
||||
799396D51D48C02300086753 /* SMB2Tree.swift in Sources */,
|
||||
7924B1A31D89DAE000589DB7 /* Options.swift in Sources */,
|
||||
@@ -541,10 +621,11 @@
|
||||
7936BC131E880F5700A6C81C /* FTPFileProvider.swift in Sources */,
|
||||
79480FF71E3ABDD0007E7275 /* CloudFileProvider.swift in Sources */,
|
||||
79F5745C1DFDB10B00179ABF /* FileObject.swift in Sources */,
|
||||
79BD63C61E2D17880035128C /* OneDriveFileProvide.swift in Sources */,
|
||||
79BD63C61E2D17880035128C /* OneDriveFileProvider.swift in Sources */,
|
||||
7924B1B01D89F7DE00589DB7 /* FPSStreamTask.swift in Sources */,
|
||||
7924B19A1D89DAE000589DB7 /* Element.swift in Sources */,
|
||||
799396C91D48C02300086753 /* SMB2IOCtl.swift in Sources */,
|
||||
7958155B1F478ED9003344DD /* HTTPFileProvider.swift in Sources */,
|
||||
79F4678B1E8B80F200C91A85 /* FTPHelper.swift in Sources */,
|
||||
799396D81D48C02300086753 /* SMB2Types.swift in Sources */,
|
||||
799396C61D48C02300086753 /* SMB2FileOperation.swift in Sources */,
|
||||
@@ -553,7 +634,6 @@
|
||||
799396BA1D48C02300086753 /* SMBFileProvider.swift in Sources */,
|
||||
794C220F1D591A4B00EC49B8 /* SMB2QueryTypes.swift in Sources */,
|
||||
794C21FF1D58912A00EC49B8 /* DropboxHelper.swift in Sources */,
|
||||
799396BD1D48C02300086753 /* CIFSTypes.swift in Sources */,
|
||||
794C220B1D5893F800EC49B8 /* SMB2Notification.swift in Sources */,
|
||||
799396D21D48C02300086753 /* SMB2SetInfo.swift in Sources */,
|
||||
7924B1971D89DAE000589DB7 /* Document.swift in Sources */,
|
||||
@@ -562,10 +642,12 @@
|
||||
799396E11D48C02300086753 /* WebDAVFileProvider.swift in Sources */,
|
||||
799396DB1D48C02300086753 /* SMBErrorType.swift in Sources */,
|
||||
799396C31D48C02300086753 /* SMB2FileHandle.swift in Sources */,
|
||||
793CCE2F1F4B8C7B00BC8288 /* HashMAC.swift in Sources */,
|
||||
799396CC1D48C02300086753 /* SMB2Query.swift in Sources */,
|
||||
7924B1AD1D89F7D800589DB7 /* SMBClient.swift in Sources */,
|
||||
79BD638D1E2CC2300035128C /* ExtendedLocalFileProvider.swift in Sources */,
|
||||
799396AB1D48C02300086753 /* DropboxFileProvider.swift in Sources */,
|
||||
793CCE2B1F4B8C5C00BC8288 /* FoundationExtensions.swift in Sources */,
|
||||
7924B1A61D89DAE000589DB7 /* Parser.swift in Sources */,
|
||||
799396B11D48C02300086753 /* FileProvider.swift in Sources */,
|
||||
7924B19D1D89DAE000589DB7 /* Error.swift in Sources */,
|
||||
@@ -576,7 +658,6 @@
|
||||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
796807571E7BF17E00BBB87B /* FileProviderExtensions.swift in Sources */,
|
||||
799396B51D48C02300086753 /* LocalFileProvider.swift in Sources */,
|
||||
799396D61D48C02300086753 /* SMB2Tree.swift in Sources */,
|
||||
7924B1A41D89DAE000589DB7 /* Options.swift in Sources */,
|
||||
@@ -584,10 +665,11 @@
|
||||
7936BC141E880F5700A6C81C /* FTPFileProvider.swift in Sources */,
|
||||
79480FF81E3ABDD0007E7275 /* CloudFileProvider.swift in Sources */,
|
||||
79F5745D1DFDB10B00179ABF /* FileObject.swift in Sources */,
|
||||
79BD63C71E2D17880035128C /* OneDriveFileProvide.swift in Sources */,
|
||||
79BD63C71E2D17880035128C /* OneDriveFileProvider.swift in Sources */,
|
||||
7924B1B11D89F7DF00589DB7 /* FPSStreamTask.swift in Sources */,
|
||||
7924B19B1D89DAE000589DB7 /* Element.swift in Sources */,
|
||||
799396CA1D48C02300086753 /* SMB2IOCtl.swift in Sources */,
|
||||
7958155C1F478ED9003344DD /* HTTPFileProvider.swift in Sources */,
|
||||
79F4678C1E8B80F200C91A85 /* FTPHelper.swift in Sources */,
|
||||
799396D91D48C02300086753 /* SMB2Types.swift in Sources */,
|
||||
799396C71D48C02300086753 /* SMB2FileOperation.swift in Sources */,
|
||||
@@ -596,7 +678,6 @@
|
||||
799396BB1D48C02300086753 /* SMBFileProvider.swift in Sources */,
|
||||
794C22101D591A4B00EC49B8 /* SMB2QueryTypes.swift in Sources */,
|
||||
794C22001D58912A00EC49B8 /* DropboxHelper.swift in Sources */,
|
||||
799396BE1D48C02300086753 /* CIFSTypes.swift in Sources */,
|
||||
794C220C1D5893F800EC49B8 /* SMB2Notification.swift in Sources */,
|
||||
799396D31D48C02300086753 /* SMB2SetInfo.swift in Sources */,
|
||||
7924B1981D89DAE000589DB7 /* Document.swift in Sources */,
|
||||
@@ -605,32 +686,59 @@
|
||||
799396E21D48C02300086753 /* WebDAVFileProvider.swift in Sources */,
|
||||
799396DC1D48C02300086753 /* SMBErrorType.swift in Sources */,
|
||||
799396C41D48C02300086753 /* SMB2FileHandle.swift in Sources */,
|
||||
793CCE301F4B8C7B00BC8288 /* HashMAC.swift in Sources */,
|
||||
799396CD1D48C02300086753 /* SMB2Query.swift in Sources */,
|
||||
7924B1AE1D89F7D900589DB7 /* SMBClient.swift in Sources */,
|
||||
79BD638E1E2CC2300035128C /* ExtendedLocalFileProvider.swift in Sources */,
|
||||
799396AC1D48C02300086753 /* DropboxFileProvider.swift in Sources */,
|
||||
793CCE2C1F4B8C5C00BC8288 /* FoundationExtensions.swift in Sources */,
|
||||
7924B1A71D89DAE000589DB7 /* Parser.swift in Sources */,
|
||||
799396B21D48C02300086753 /* FileProvider.swift in Sources */,
|
||||
7924B19E1D89DAE000589DB7 /* Error.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
79D9034D1FAB647400D61D31 /* Sources */ = {
|
||||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
79D903541FAB647400D61D31 /* FilesProviderTests.swift in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
/* End PBXSourcesBuildPhase section */
|
||||
|
||||
/* Begin PBXTargetDependency section */
|
||||
79D903581FAB647400D61D31 /* PBXTargetDependency */ = {
|
||||
isa = PBXTargetDependency;
|
||||
target = 799396741D48B80D00086753 /* FilesProvider OSX */;
|
||||
targetProxy = 79D903571FAB647400D61D31 /* PBXContainerItemProxy */;
|
||||
};
|
||||
/* End PBXTargetDependency section */
|
||||
|
||||
/* Begin XCBuildConfiguration section */
|
||||
799396601D48B7BF00086753 /* Debug */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
BUNDLE_VERSION_STRING = 0.15.1;
|
||||
BUNDLE_VERSION_STRING = 0.25.1;
|
||||
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
|
||||
CLANG_WARN_BOOL_CONVERSION = YES;
|
||||
CLANG_WARN_COMMA = YES;
|
||||
CLANG_WARN_CONSTANT_CONVERSION = YES;
|
||||
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
|
||||
CLANG_WARN_EMPTY_BODY = YES;
|
||||
CLANG_WARN_ENUM_CONVERSION = YES;
|
||||
CLANG_WARN_INFINITE_RECURSION = YES;
|
||||
CLANG_WARN_INT_CONVERSION = YES;
|
||||
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
|
||||
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
|
||||
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
|
||||
CLANG_WARN_STRICT_PROTOTYPES = YES;
|
||||
CLANG_WARN_SUSPICIOUS_MOVE = YES;
|
||||
CLANG_WARN_UNREACHABLE_CODE = YES;
|
||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
|
||||
COPY_PHASE_STRIP = NO;
|
||||
DEBUG_INFORMATION_FORMAT = dwarf;
|
||||
ENABLE_BITCODE = YES;
|
||||
ENABLE_STRICT_OBJC_MSGSEND = YES;
|
||||
@@ -638,26 +746,36 @@
|
||||
GCC_NO_COMMON_BLOCKS = YES;
|
||||
GCC_OPTIMIZATION_LEVEL = 0;
|
||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
|
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES;
|
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
|
||||
GCC_WARN_UNDECLARED_SELECTOR = YES;
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES;
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
||||
GCC_WARN_UNUSED_FUNCTION = YES;
|
||||
GCC_WARN_UNUSED_VARIABLE = YES;
|
||||
ONLY_ACTIVE_ARCH = YES;
|
||||
SWIFT_VERSION = 3.0;
|
||||
PRODUCT_NAME = FilesProvider;
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
|
||||
SWIFT_VERSION = 4.0;
|
||||
};
|
||||
name = Debug;
|
||||
};
|
||||
799396611D48B7BF00086753 /* Release */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
BUNDLE_VERSION_STRING = 0.15.1;
|
||||
BUNDLE_VERSION_STRING = 0.25.1;
|
||||
CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES;
|
||||
CLANG_WARN_BOOL_CONVERSION = YES;
|
||||
CLANG_WARN_COMMA = YES;
|
||||
CLANG_WARN_CONSTANT_CONVERSION = YES;
|
||||
CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES;
|
||||
CLANG_WARN_EMPTY_BODY = YES;
|
||||
CLANG_WARN_ENUM_CONVERSION = YES;
|
||||
CLANG_WARN_INFINITE_RECURSION = YES;
|
||||
CLANG_WARN_INT_CONVERSION = YES;
|
||||
CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
|
||||
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
|
||||
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
|
||||
CLANG_WARN_STRICT_PROTOTYPES = YES;
|
||||
CLANG_WARN_SUSPICIOUS_MOVE = YES;
|
||||
CLANG_WARN_UNREACHABLE_CODE = YES;
|
||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
|
||||
@@ -666,13 +784,14 @@
|
||||
ENABLE_STRICT_OBJC_MSGSEND = YES;
|
||||
GCC_NO_COMMON_BLOCKS = YES;
|
||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
|
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES;
|
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
|
||||
GCC_WARN_UNDECLARED_SELECTOR = YES;
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES;
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
||||
GCC_WARN_UNUSED_FUNCTION = YES;
|
||||
GCC_WARN_UNUSED_VARIABLE = YES;
|
||||
PRODUCT_NAME = FilesProvider;
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
|
||||
SWIFT_VERSION = 3.0;
|
||||
SWIFT_VERSION = 4.0;
|
||||
};
|
||||
name = Release;
|
||||
};
|
||||
@@ -684,15 +803,6 @@
|
||||
CLANG_ANALYZER_NONNULL = YES;
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
CLANG_ENABLE_OBJC_ARC = YES;
|
||||
CLANG_WARN_BOOL_CONVERSION = YES;
|
||||
CLANG_WARN_CONSTANT_CONVERSION = YES;
|
||||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
|
||||
CLANG_WARN_EMPTY_BODY = YES;
|
||||
CLANG_WARN_ENUM_CONVERSION = YES;
|
||||
CLANG_WARN_INT_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
|
||||
CLANG_WARN_UNREACHABLE_CODE = YES;
|
||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
|
||||
CURRENT_PROJECT_VERSION = 1;
|
||||
DEFINES_MODULE = YES;
|
||||
DYLIB_COMPATIBILITY_VERSION = 1;
|
||||
@@ -706,22 +816,14 @@
|
||||
"DEBUG=1",
|
||||
"$(inherited)",
|
||||
);
|
||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
|
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
|
||||
GCC_WARN_UNDECLARED_SELECTOR = YES;
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
||||
GCC_WARN_UNUSED_FUNCTION = YES;
|
||||
GCC_WARN_UNUSED_VARIABLE = YES;
|
||||
INFOPLIST_FILE = "Pod/Info-iOS.plist";
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
|
||||
MTL_ENABLE_DEBUG_INFO = YES;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "com.mousavian.FileProvider-iOS";
|
||||
PRODUCT_NAME = FileProvider;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "com.mousavian.FilesProvider-iOS";
|
||||
SDKROOT = iphoneos;
|
||||
SKIP_INSTALL = YES;
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
|
||||
TARGETED_DEVICE_FAMILY = "1,2";
|
||||
VERSIONING_SYSTEM = "apple-generic";
|
||||
VERSION_INFO_PREFIX = "";
|
||||
@@ -736,15 +838,6 @@
|
||||
CLANG_ANALYZER_NONNULL = YES;
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
CLANG_ENABLE_OBJC_ARC = YES;
|
||||
CLANG_WARN_BOOL_CONVERSION = YES;
|
||||
CLANG_WARN_CONSTANT_CONVERSION = YES;
|
||||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
|
||||
CLANG_WARN_EMPTY_BODY = YES;
|
||||
CLANG_WARN_ENUM_CONVERSION = YES;
|
||||
CLANG_WARN_INT_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
|
||||
CLANG_WARN_UNREACHABLE_CODE = YES;
|
||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
|
||||
CURRENT_PROJECT_VERSION = 1;
|
||||
DEFINES_MODULE = YES;
|
||||
DYLIB_COMPATIBILITY_VERSION = 1;
|
||||
@@ -753,19 +846,12 @@
|
||||
ENABLE_NS_ASSERTIONS = NO;
|
||||
ENABLE_STRICT_OBJC_MSGSEND = YES;
|
||||
GCC_NO_COMMON_BLOCKS = YES;
|
||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
|
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
|
||||
GCC_WARN_UNDECLARED_SELECTOR = YES;
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
||||
GCC_WARN_UNUSED_FUNCTION = YES;
|
||||
GCC_WARN_UNUSED_VARIABLE = YES;
|
||||
INFOPLIST_FILE = "Pod/Info-iOS.plist";
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
|
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
|
||||
MTL_ENABLE_DEBUG_INFO = NO;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "com.mousavian.FileProvider-iOS";
|
||||
PRODUCT_NAME = FileProvider;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "com.mousavian.FilesProvider-iOS";
|
||||
SDKROOT = iphoneos;
|
||||
SKIP_INSTALL = YES;
|
||||
TARGETED_DEVICE_FAMILY = "1,2";
|
||||
@@ -785,15 +871,6 @@
|
||||
CLANG_CXX_LIBRARY = "libc++";
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
CLANG_ENABLE_OBJC_ARC = YES;
|
||||
CLANG_WARN_BOOL_CONVERSION = YES;
|
||||
CLANG_WARN_CONSTANT_CONVERSION = YES;
|
||||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
|
||||
CLANG_WARN_EMPTY_BODY = YES;
|
||||
CLANG_WARN_ENUM_CONVERSION = YES;
|
||||
CLANG_WARN_INT_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
|
||||
CLANG_WARN_UNREACHABLE_CODE = YES;
|
||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
|
||||
COMBINE_HIDPI_IMAGES = YES;
|
||||
CURRENT_PROJECT_VERSION = 1;
|
||||
DEFINES_MODULE = YES;
|
||||
@@ -810,22 +887,14 @@
|
||||
"DEBUG=1",
|
||||
"$(inherited)",
|
||||
);
|
||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
|
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
|
||||
GCC_WARN_UNDECLARED_SELECTOR = YES;
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
||||
GCC_WARN_UNUSED_FUNCTION = YES;
|
||||
GCC_WARN_UNUSED_VARIABLE = YES;
|
||||
INFOPLIST_FILE = "Pod/Info-MacOS.plist";
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
|
||||
MACOSX_DEPLOYMENT_TARGET = 10.10;
|
||||
MTL_ENABLE_DEBUG_INFO = YES;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "com.mousavian.FileProvider-OSX";
|
||||
PRODUCT_NAME = FileProvider;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "com.mousavian.FilesProvider-OSX";
|
||||
SDKROOT = macosx;
|
||||
SKIP_INSTALL = YES;
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
|
||||
VERSIONING_SYSTEM = "apple-generic";
|
||||
VERSION_INFO_PREFIX = "";
|
||||
};
|
||||
@@ -841,15 +910,6 @@
|
||||
CLANG_CXX_LIBRARY = "libc++";
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
CLANG_ENABLE_OBJC_ARC = YES;
|
||||
CLANG_WARN_BOOL_CONVERSION = YES;
|
||||
CLANG_WARN_CONSTANT_CONVERSION = YES;
|
||||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
|
||||
CLANG_WARN_EMPTY_BODY = YES;
|
||||
CLANG_WARN_ENUM_CONVERSION = YES;
|
||||
CLANG_WARN_INT_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
|
||||
CLANG_WARN_UNREACHABLE_CODE = YES;
|
||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
|
||||
COMBINE_HIDPI_IMAGES = YES;
|
||||
CURRENT_PROJECT_VERSION = 1;
|
||||
DEFINES_MODULE = YES;
|
||||
@@ -861,19 +921,12 @@
|
||||
ENABLE_STRICT_OBJC_MSGSEND = YES;
|
||||
FRAMEWORK_VERSION = A;
|
||||
GCC_NO_COMMON_BLOCKS = YES;
|
||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
|
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
|
||||
GCC_WARN_UNDECLARED_SELECTOR = YES;
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
||||
GCC_WARN_UNUSED_FUNCTION = YES;
|
||||
GCC_WARN_UNUSED_VARIABLE = YES;
|
||||
INFOPLIST_FILE = "Pod/Info-MacOS.plist";
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/Frameworks";
|
||||
MACOSX_DEPLOYMENT_TARGET = 10.10;
|
||||
MTL_ENABLE_DEBUG_INFO = NO;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "com.mousavian.FileProvider-OSX";
|
||||
PRODUCT_NAME = FileProvider;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "com.mousavian.FilesProvider-OSX";
|
||||
SDKROOT = macosx;
|
||||
SKIP_INSTALL = YES;
|
||||
VERSIONING_SYSTEM = "apple-generic";
|
||||
@@ -891,15 +944,6 @@
|
||||
CLANG_CXX_LIBRARY = "libc++";
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
CLANG_ENABLE_OBJC_ARC = YES;
|
||||
CLANG_WARN_BOOL_CONVERSION = YES;
|
||||
CLANG_WARN_CONSTANT_CONVERSION = YES;
|
||||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
|
||||
CLANG_WARN_EMPTY_BODY = YES;
|
||||
CLANG_WARN_ENUM_CONVERSION = YES;
|
||||
CLANG_WARN_INT_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
|
||||
CLANG_WARN_UNREACHABLE_CODE = YES;
|
||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
|
||||
"CODE_SIGN_IDENTITY[sdk=appletvos*]" = "";
|
||||
CURRENT_PROJECT_VERSION = 1;
|
||||
DEFINES_MODULE = YES;
|
||||
@@ -914,21 +958,13 @@
|
||||
"DEBUG=1",
|
||||
"$(inherited)",
|
||||
);
|
||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
|
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
|
||||
GCC_WARN_UNDECLARED_SELECTOR = YES;
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
||||
GCC_WARN_UNUSED_FUNCTION = YES;
|
||||
GCC_WARN_UNUSED_VARIABLE = YES;
|
||||
INFOPLIST_FILE = "Pod/Info-tvOS.plist";
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
|
||||
MTL_ENABLE_DEBUG_INFO = YES;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "com.mousavian.FileProvider-tvOS";
|
||||
PRODUCT_NAME = FileProvider;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "com.mousavian.FilesProvider-tvOS";
|
||||
SDKROOT = appletvos;
|
||||
SKIP_INSTALL = YES;
|
||||
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
|
||||
TARGETED_DEVICE_FAMILY = 3;
|
||||
TVOS_DEPLOYMENT_TARGET = 10.0;
|
||||
VERSIONING_SYSTEM = "apple-generic";
|
||||
@@ -946,15 +982,6 @@
|
||||
CLANG_CXX_LIBRARY = "libc++";
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
CLANG_ENABLE_OBJC_ARC = YES;
|
||||
CLANG_WARN_BOOL_CONVERSION = YES;
|
||||
CLANG_WARN_CONSTANT_CONVERSION = YES;
|
||||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
|
||||
CLANG_WARN_EMPTY_BODY = YES;
|
||||
CLANG_WARN_ENUM_CONVERSION = YES;
|
||||
CLANG_WARN_INT_CONVERSION = YES;
|
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
|
||||
CLANG_WARN_UNREACHABLE_CODE = YES;
|
||||
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
|
||||
"CODE_SIGN_IDENTITY[sdk=appletvos*]" = "";
|
||||
CURRENT_PROJECT_VERSION = 1;
|
||||
DEFINES_MODULE = YES;
|
||||
@@ -964,18 +991,11 @@
|
||||
ENABLE_NS_ASSERTIONS = NO;
|
||||
ENABLE_STRICT_OBJC_MSGSEND = YES;
|
||||
GCC_NO_COMMON_BLOCKS = YES;
|
||||
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
|
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
|
||||
GCC_WARN_UNDECLARED_SELECTOR = YES;
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
||||
GCC_WARN_UNUSED_FUNCTION = YES;
|
||||
GCC_WARN_UNUSED_VARIABLE = YES;
|
||||
INFOPLIST_FILE = "Pod/Info-tvOS.plist";
|
||||
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
|
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
|
||||
MTL_ENABLE_DEBUG_INFO = NO;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "com.mousavian.FileProvider-tvOS";
|
||||
PRODUCT_NAME = FileProvider;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = "com.mousavian.FilesProvider-tvOS";
|
||||
SDKROOT = appletvos;
|
||||
SKIP_INSTALL = YES;
|
||||
TARGETED_DEVICE_FAMILY = 3;
|
||||
@@ -986,10 +1006,80 @@
|
||||
};
|
||||
name = Release;
|
||||
};
|
||||
79D903591FAB647400D61D31 /* Debug */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
ALWAYS_SEARCH_USER_PATHS = NO;
|
||||
CLANG_ANALYZER_NONNULL = YES;
|
||||
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
|
||||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
|
||||
CLANG_CXX_LIBRARY = "libc++";
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
CLANG_ENABLE_OBJC_ARC = YES;
|
||||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
|
||||
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
|
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
|
||||
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
|
||||
CODE_SIGN_IDENTITY = "-";
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
COMBINE_HIDPI_IMAGES = YES;
|
||||
ENABLE_BITCODE = NO;
|
||||
GCC_C_LANGUAGE_STANDARD = gnu11;
|
||||
GCC_DYNAMIC_NO_PIC = NO;
|
||||
GCC_PREPROCESSOR_DEFINITIONS = (
|
||||
"DEBUG=1",
|
||||
"$(inherited)",
|
||||
);
|
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
||||
INFOPLIST_FILE = Tests/Info.plist;
|
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
|
||||
MACOSX_DEPLOYMENT_TARGET = 10.13;
|
||||
MTL_ENABLE_DEBUG_INFO = YES;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = com.mousavian.FilesProviderTests;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SDKROOT = macosx;
|
||||
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
|
||||
};
|
||||
name = Debug;
|
||||
};
|
||||
79D9035A1FAB647400D61D31 /* Release */ = {
|
||||
isa = XCBuildConfiguration;
|
||||
buildSettings = {
|
||||
ALWAYS_SEARCH_USER_PATHS = NO;
|
||||
CLANG_ANALYZER_NONNULL = YES;
|
||||
CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE;
|
||||
CLANG_CXX_LANGUAGE_STANDARD = "gnu++14";
|
||||
CLANG_CXX_LIBRARY = "libc++";
|
||||
CLANG_ENABLE_MODULES = YES;
|
||||
CLANG_ENABLE_OBJC_ARC = YES;
|
||||
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
|
||||
CLANG_WARN_DOCUMENTATION_COMMENTS = YES;
|
||||
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
|
||||
CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE;
|
||||
CODE_SIGN_IDENTITY = "-";
|
||||
CODE_SIGN_STYLE = Automatic;
|
||||
COMBINE_HIDPI_IMAGES = YES;
|
||||
COPY_PHASE_STRIP = NO;
|
||||
ENABLE_BITCODE = NO;
|
||||
ENABLE_NS_ASSERTIONS = NO;
|
||||
GCC_C_LANGUAGE_STANDARD = gnu11;
|
||||
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
|
||||
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
|
||||
INFOPLIST_FILE = Tests/Info.plist;
|
||||
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
|
||||
MACOSX_DEPLOYMENT_TARGET = 10.13;
|
||||
MTL_ENABLE_DEBUG_INFO = NO;
|
||||
PRODUCT_BUNDLE_IDENTIFIER = com.mousavian.FilesProviderTests;
|
||||
PRODUCT_NAME = "$(TARGET_NAME)";
|
||||
SDKROOT = macosx;
|
||||
};
|
||||
name = Release;
|
||||
};
|
||||
/* End XCBuildConfiguration section */
|
||||
|
||||
/* Begin XCConfigurationList section */
|
||||
7993965F1D48B7BF00086753 /* Build configuration list for PBXProject "FileProvider" */ = {
|
||||
7993965F1D48B7BF00086753 /* Build configuration list for PBXProject "FilesProvider" */ = {
|
||||
isa = XCConfigurationList;
|
||||
buildConfigurations = (
|
||||
799396601D48B7BF00086753 /* Debug */,
|
||||
@@ -998,7 +1088,7 @@
|
||||
defaultConfigurationIsVisible = 0;
|
||||
defaultConfigurationName = Release;
|
||||
};
|
||||
7993966F1D48B7F600086753 /* Build configuration list for PBXNativeTarget "FileProvider iOS" */ = {
|
||||
7993966F1D48B7F600086753 /* Build configuration list for PBXNativeTarget "FilesProvider iOS" */ = {
|
||||
isa = XCConfigurationList;
|
||||
buildConfigurations = (
|
||||
7993966D1D48B7F600086753 /* Debug */,
|
||||
@@ -1007,7 +1097,7 @@
|
||||
defaultConfigurationIsVisible = 0;
|
||||
defaultConfigurationName = Release;
|
||||
};
|
||||
7993967A1D48B80D00086753 /* Build configuration list for PBXNativeTarget "FileProvider OSX" */ = {
|
||||
7993967A1D48B80D00086753 /* Build configuration list for PBXNativeTarget "FilesProvider OSX" */ = {
|
||||
isa = XCConfigurationList;
|
||||
buildConfigurations = (
|
||||
7993967B1D48B80D00086753 /* Debug */,
|
||||
@@ -1016,7 +1106,7 @@
|
||||
defaultConfigurationIsVisible = 0;
|
||||
defaultConfigurationName = Release;
|
||||
};
|
||||
799396871D48B82700086753 /* Build configuration list for PBXNativeTarget "FileProvider tvOS" */ = {
|
||||
799396871D48B82700086753 /* Build configuration list for PBXNativeTarget "FilesProvider tvOS" */ = {
|
||||
isa = XCConfigurationList;
|
||||
buildConfigurations = (
|
||||
799396881D48B82700086753 /* Debug */,
|
||||
@@ -1025,6 +1115,15 @@
|
||||
defaultConfigurationIsVisible = 0;
|
||||
defaultConfigurationName = Release;
|
||||
};
|
||||
79D9035B1FAB647400D61D31 /* Build configuration list for PBXNativeTarget "FilesProviderTests" */ = {
|
||||
isa = XCConfigurationList;
|
||||
buildConfigurations = (
|
||||
79D903591FAB647400D61D31 /* Debug */,
|
||||
79D9035A1FAB647400D61D31 /* Release */,
|
||||
);
|
||||
defaultConfigurationIsVisible = 0;
|
||||
defaultConfigurationName = Release;
|
||||
};
|
||||
/* End XCConfigurationList section */
|
||||
};
|
||||
rootObject = 7993965C1D48B7BF00086753 /* Project object */;
|
||||
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>IDEDidComputeMac32BitWarning</key>
|
||||
<true/>
|
||||
</dict>
|
||||
</plist>
|
||||
+10
-10
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Scheme
|
||||
LastUpgradeVersion = "0820"
|
||||
LastUpgradeVersion = "0930"
|
||||
version = "1.3">
|
||||
<BuildAction
|
||||
parallelizeBuildables = "YES"
|
||||
@@ -15,9 +15,9 @@
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "799396741D48B80D00086753"
|
||||
BuildableName = "FileProvider.framework"
|
||||
BlueprintName = "FileProvider OSX"
|
||||
ReferencedContainer = "container:FileProvider.xcodeproj">
|
||||
BuildableName = "FilesProvider.framework"
|
||||
BlueprintName = "FilesProvider OSX"
|
||||
ReferencedContainer = "container:FilesProvider.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
</BuildActionEntries>
|
||||
@@ -46,9 +46,9 @@
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "799396741D48B80D00086753"
|
||||
BuildableName = "FileProvider.framework"
|
||||
BlueprintName = "FileProvider OSX"
|
||||
ReferencedContainer = "container:FileProvider.xcodeproj">
|
||||
BuildableName = "FilesProvider.framework"
|
||||
BlueprintName = "FilesProvider OSX"
|
||||
ReferencedContainer = "container:FilesProvider.xcodeproj">
|
||||
</BuildableReference>
|
||||
</MacroExpansion>
|
||||
<AdditionalOptions>
|
||||
@@ -64,9 +64,9 @@
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "799396741D48B80D00086753"
|
||||
BuildableName = "FileProvider.framework"
|
||||
BlueprintName = "FileProvider OSX"
|
||||
ReferencedContainer = "container:FileProvider.xcodeproj">
|
||||
BuildableName = "FilesProvider.framework"
|
||||
BlueprintName = "FilesProvider OSX"
|
||||
ReferencedContainer = "container:FilesProvider.xcodeproj">
|
||||
</BuildableReference>
|
||||
</MacroExpansion>
|
||||
</ProfileAction>
|
||||
+10
-10
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Scheme
|
||||
LastUpgradeVersion = "0820"
|
||||
LastUpgradeVersion = "0930"
|
||||
version = "1.3">
|
||||
<BuildAction
|
||||
parallelizeBuildables = "YES"
|
||||
@@ -15,9 +15,9 @@
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "799396661D48B7F600086753"
|
||||
BuildableName = "FileProvider.framework"
|
||||
BlueprintName = "FileProvider iOS"
|
||||
ReferencedContainer = "container:FileProvider.xcodeproj">
|
||||
BuildableName = "FilesProvider.framework"
|
||||
BlueprintName = "FilesProvider iOS"
|
||||
ReferencedContainer = "container:FilesProvider.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
</BuildActionEntries>
|
||||
@@ -46,9 +46,9 @@
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "799396661D48B7F600086753"
|
||||
BuildableName = "FileProvider.framework"
|
||||
BlueprintName = "FileProvider iOS"
|
||||
ReferencedContainer = "container:FileProvider.xcodeproj">
|
||||
BuildableName = "FilesProvider.framework"
|
||||
BlueprintName = "FilesProvider iOS"
|
||||
ReferencedContainer = "container:FilesProvider.xcodeproj">
|
||||
</BuildableReference>
|
||||
</MacroExpansion>
|
||||
<AdditionalOptions>
|
||||
@@ -64,9 +64,9 @@
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "799396661D48B7F600086753"
|
||||
BuildableName = "FileProvider.framework"
|
||||
BlueprintName = "FileProvider iOS"
|
||||
ReferencedContainer = "container:FileProvider.xcodeproj">
|
||||
BuildableName = "FilesProvider.framework"
|
||||
BlueprintName = "FilesProvider iOS"
|
||||
ReferencedContainer = "container:FilesProvider.xcodeproj">
|
||||
</BuildableReference>
|
||||
</MacroExpansion>
|
||||
</ProfileAction>
|
||||
+10
-10
@@ -1,6 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Scheme
|
||||
LastUpgradeVersion = "0810"
|
||||
LastUpgradeVersion = "0930"
|
||||
version = "1.3">
|
||||
<BuildAction
|
||||
parallelizeBuildables = "YES"
|
||||
@@ -15,9 +15,9 @@
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "799396811D48B82700086753"
|
||||
BuildableName = "FileProvider.framework"
|
||||
BlueprintName = "FileProvider tvOS"
|
||||
ReferencedContainer = "container:FileProvider.xcodeproj">
|
||||
BuildableName = "FilesProvider.framework"
|
||||
BlueprintName = "FilesProvider tvOS"
|
||||
ReferencedContainer = "container:FilesProvider.xcodeproj">
|
||||
</BuildableReference>
|
||||
</BuildActionEntry>
|
||||
</BuildActionEntries>
|
||||
@@ -46,9 +46,9 @@
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "799396811D48B82700086753"
|
||||
BuildableName = "FileProvider.framework"
|
||||
BlueprintName = "FileProvider tvOS"
|
||||
ReferencedContainer = "container:FileProvider.xcodeproj">
|
||||
BuildableName = "FilesProvider.framework"
|
||||
BlueprintName = "FilesProvider tvOS"
|
||||
ReferencedContainer = "container:FilesProvider.xcodeproj">
|
||||
</BuildableReference>
|
||||
</MacroExpansion>
|
||||
<AdditionalOptions>
|
||||
@@ -64,9 +64,9 @@
|
||||
<BuildableReference
|
||||
BuildableIdentifier = "primary"
|
||||
BlueprintIdentifier = "799396811D48B82700086753"
|
||||
BuildableName = "FileProvider.framework"
|
||||
BlueprintName = "FileProvider tvOS"
|
||||
ReferencedContainer = "container:FileProvider.xcodeproj">
|
||||
BuildableName = "FilesProvider.framework"
|
||||
BlueprintName = "FilesProvider tvOS"
|
||||
ReferencedContainer = "container:FilesProvider.xcodeproj">
|
||||
</BuildableReference>
|
||||
</MacroExpansion>
|
||||
</ProfileAction>
|
||||
+23
-2
@@ -1,5 +1,26 @@
|
||||
// swift-tools-version:4.0
|
||||
// The swift-tools-version declares the minimum version of Swift required to build this package.
|
||||
|
||||
import PackageDescription
|
||||
|
||||
let package = Package(
|
||||
name: "FileProvider"
|
||||
)
|
||||
name: "FilesProvider",
|
||||
products: [
|
||||
// Products define the executables and libraries produced by a package, and make them visible to other packages.
|
||||
.library(
|
||||
name: "FilesProvider",
|
||||
targets: ["FilesProvider"]
|
||||
)
|
||||
],
|
||||
dependencies: [],
|
||||
targets: [
|
||||
.target(name: "FilesProvider",
|
||||
dependencies: [],
|
||||
path: "Sources"
|
||||
),
|
||||
.testTarget(name: "FilesProviderTests",
|
||||
dependencies: ["FilesProvider"],
|
||||
path: "Tests"
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
@@ -1,22 +1,25 @@
|
||||

|
||||

|
||||
|
||||
>This Swift library provide a swifty way to deal with local and remote files and directories in a unified way.
|
||||
> This Swift library provide a swifty way to deal with local and remote files and directories in a unified way.
|
||||
|
||||
<center>
|
||||
|
||||
[![Swift Version][swift-image]][swift-url]
|
||||
[![Platform][platform-image]](#)
|
||||
[![License][license-image]][license-url]
|
||||
|
||||
[![Release versin][release-image]][release-url]
|
||||
[][cocoapods]
|
||||
[![Carthage compatible][carthage-image]](https://github.com/Carthage/Carthage)
|
||||
|
||||
[![Build Status][travis-image]][travis-url]
|
||||
[![Codebeat Badge][codebeat-image]][codebeat-url]
|
||||
[![Cocoapods Docs][docs-image]][docs-url]
|
||||
|
||||
[![Release version][release-image]][release-url]
|
||||
[][cocoapods]
|
||||
[![Carthage compatible][carthage-image]](https://github.com/Carthage/Carthage)
|
||||
[![Cocoapods Downloads][cocoapods-downloads]][cocoapods]
|
||||
[![Cocoapods Apps][cocoapods-apps]][cocoapods]
|
||||
|
||||
</center>
|
||||
|
||||
<!---
|
||||
[![Cocoapods Doc][docs-image]][docs-url]
|
||||
[](https://codecov.io/gh/amosavian/FileProvider)
|
||||
--->
|
||||
|
||||
@@ -28,36 +31,36 @@ All functions do async calls and it wont block your main thread.
|
||||
|
||||
- [x] **LocalFileProvider** a wrapper around `FileManager` with some additions like builtin coordinating, searching and reading a portion of file.
|
||||
- [x] **CloudFileProvider** A wrapper around app's ubiquitous container API of iCloud Drive.
|
||||
- [x] **WebDAVFileProvider** WebDAV protocol is defacto file transmission standard, supported by some cloud services like `Box.com` and `Yandex.disk`.
|
||||
- [x] **WebDAVFileProvider** WebDAV protocol is defacto file transmission standard, supported by some cloud services like `ownCloud`, `Box.com` and `Yandex.disk`.
|
||||
- [x] **FTPFileProvider** While deprecated in 1990s due to serious security concerns, it's still in use on some Web hosts.
|
||||
* Recursive directory removing & searching is not implemented yet.
|
||||
* Active mode is not implemented yet (and probably won`t).
|
||||
- [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 REST API, works with `onedrive.com` and compatible (business) servers.
|
||||
* For now it has limitation in uploading files up to 100MB.
|
||||
- [ ] **GoogleFileProvider** A wrapper around Goodle Drive REST API.
|
||||
- [ ] **AmazonS3FileProvider** Amazon storage backend. Used by many sites.
|
||||
- [ ] **GoogleFileProvider** A wrapper around Goodle Drive REST 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 deprecated and very tricky to be implemented due to strict memory allignment in Swift.
|
||||
* SMBv1/CIFS is insecure, deprecated and kinda tricky to be implemented due to strict memory allignment in Swift.
|
||||
|
||||
## Requirements
|
||||
|
||||
- **Swift 3.0 or higher**
|
||||
- **Swift 4.0 or higher**
|
||||
- iOS 8.0 , OSX 10.10
|
||||
- XCode 8.0
|
||||
- XCode 9.0
|
||||
|
||||
Legacy version is available in swift-2 branch.
|
||||
Legacy version is available in swift-3 branch.
|
||||
|
||||
## Installation
|
||||
|
||||
### Important: this library has been renamed to avoid conflict in iOS 11, macOS 10.13 and Xcode 9.0. Please read issue [#53](https://github.com/amosavian/FileProvider/issues/53) to find more.
|
||||
|
||||
|
||||
### Cocoapods / Carthage / Swift Package Manager
|
||||
|
||||
Add this line to your pods file:
|
||||
|
||||
```ruby
|
||||
pod "FileProvider"
|
||||
pod "FilesProvider"
|
||||
```
|
||||
|
||||
Or add this to Cartfile:
|
||||
@@ -69,7 +72,7 @@ github "amosavian/FileProvider"
|
||||
Or to use in Swift Package Manager add this line in `Dependencies`:
|
||||
|
||||
```swift
|
||||
.Package(url: "https://github.com/amosavian/FileProvider.git", majorVersion: 0, minorVersion: 12)
|
||||
.Package(url: "https://github.com/amosavian/FileProvider.git", majorVersion: 0)
|
||||
```
|
||||
|
||||
### Manually
|
||||
@@ -95,17 +98,25 @@ Then you can do either:
|
||||
|
||||
* Copy Source folder to your project and Voila!
|
||||
|
||||
* Drop `FileProvider.xcodeproj` to you Xcode workspace and add the framework to your Embeded Binaries in target.
|
||||
* Drop `FilesProvider.xcodeproj` to you Xcode workspace and add the framework to your Embeded Binaries in target.
|
||||
|
||||
## Design
|
||||
|
||||
To find design concepts and how to implement a custom provider, read [Concepts and Design document](Docs/DESIGN.md).
|
||||
|
||||
## Usage
|
||||
|
||||
Each provider has a specific class which conforms to FileProvider protocol and share same syntax
|
||||
Each provider has a specific class which conforms to FileProvider protocol and share same syntax.
|
||||
|
||||
Find a [sample code for iOS here](Docs/Sample-iOS.swift).
|
||||
|
||||
### Initialization
|
||||
|
||||
For LocalFileProvider if you want to deal with `Documents` folder
|
||||
For LocalFileProvider if you want to deal with `Documents` folder:
|
||||
|
||||
``` swift
|
||||
import FilesProvider
|
||||
|
||||
let documentsProvider = LocalFileProvider()
|
||||
|
||||
// Equals with:
|
||||
@@ -119,6 +130,8 @@ let documentsProvider = LocalFileProvider(baseURL: documentsURL)
|
||||
Also for using group shared container:
|
||||
|
||||
```swift
|
||||
import FilesProvider
|
||||
|
||||
let documentsProvider = LocalFileProvider(sharedContainerId: "group.yourcompany.appContainer")
|
||||
// Replace your group identifier
|
||||
```
|
||||
@@ -128,6 +141,8 @@ You can't change the base url later. and all paths are related to this base url
|
||||
To initialize an iCloud Container provider look at [here](https://medium.com/ios-os-x-development/icloud-drive-documents-1a46b5706fe1) to see how to update project settings then use below code, This will automatically manager creating Documents folder in container:
|
||||
|
||||
```swift
|
||||
import FilesProvider
|
||||
|
||||
let documentsProvider = CloudFileProvider(containerId: nil)
|
||||
```
|
||||
|
||||
@@ -135,17 +150,19 @@ let documentsProvider = CloudFileProvider(containerId: nil)
|
||||
For remote file providers authentication may be necessary:
|
||||
|
||||
``` swift
|
||||
import FilesProvider
|
||||
|
||||
let credential = URLCredential(user: "user", password: "pass", persistence: .permanent)
|
||||
let webdavProvider = WebDAVFileProvider(baseURL: URL(string: "http://www.example.com/dav")!, credential: credential)
|
||||
```
|
||||
|
||||
* In case you want to connect non-secure servers for WebDAV (http) in iOS 9+ / macOS 10.11+ you should disable App Transport Security (ATS) according to [this guide.](https://gist.github.com/mlynch/284699d676fe9ed0abfa)
|
||||
* In case you want to connect non-secure servers for WebDAV (http) or FTP in iOS 9+ / macOS 10.11+ you should disable App Transport Security (ATS) according to [this guide.](https://gist.github.com/mlynch/284699d676fe9ed0abfa)
|
||||
|
||||
* For Dropbox & OneDrive, user is clientID and password is Token which both must be retrieved via [OAuth2 API of Dropbox](https://www.dropbox.com/developers/reference/oauth-guide). There are libraries like [p2/OAuth2](https://github.com/p2/OAuth2) or [OAuthSwift](https://github.com/OAuthSwift/OAuthSwift) which can facilate the procedure to retrieve token. The latter is easier to use and prefered.
|
||||
* For Dropbox & OneDrive, user is clientID and password is Token which both must be retrieved via OAuth2 API o. There are libraries like [p2/OAuth2](https://github.com/p2/OAuth2) or [OAuthSwift](https://github.com/OAuthSwift/OAuthSwift) which can facilate the procedure to retrieve token. The latter is easier to use and prefered. Please see [OAuth example for Dropbox and OneDrive](Docs/OAuth.md) for detailed instruction.
|
||||
|
||||
For interaction with UI, set delegate variable of `FileProvider` object
|
||||
For interaction with UI, set delegate property of `FileProvider` object.
|
||||
|
||||
You can use `url(of:)` method if provider to get direct access url (local or remote files) for some file systems which allows to do so (Dropbox doesn't support and returns path simply wrapped in URL)
|
||||
You can use `url(of:)` method if provider to get direct access url (local or remote files) for some file systems which allows to do so (Dropbox doesn't support and returns path simply wrapped in URL).
|
||||
|
||||
### Delegates
|
||||
|
||||
@@ -230,10 +247,10 @@ To get list of files in a directory:
|
||||
documentsProvider.contentsOfDirectory(path: "/", completionHandler: {
|
||||
contents, error in
|
||||
for file in contents {
|
||||
print("Name: \(attributes.name)")
|
||||
print("Size: \(attributes.size)")
|
||||
print("Creation Date: \(attributes.creationDate)")
|
||||
print("Modification Date: \(attributes.modifiedDate)")
|
||||
print("Name: \(file.name)")
|
||||
print("Size: \(file.size)")
|
||||
print("Creation Date: \(file.creationDate)")
|
||||
print("Modification Date: \(file.modifiedDate)")
|
||||
}
|
||||
})
|
||||
```
|
||||
@@ -250,15 +267,6 @@ func storageProperties(completionHandler: { total, used in
|
||||
|
||||
* if this function is unavailable on provider or an error has been occurred, total space will be reported `-1` and used space `0`
|
||||
|
||||
### Change current directory
|
||||
|
||||
```swift
|
||||
documentsProvider.currentPath = "/New Folder"
|
||||
// now path is ~/Documents/New Folder
|
||||
```
|
||||
|
||||
You can then pass "" (empty string) to `contentsOfDirectory` method to list files in current directory.
|
||||
|
||||
### Creating File and Folders
|
||||
|
||||
Creating new directory:
|
||||
@@ -297,8 +305,6 @@ documentsProvider.moveItem(path: "new folder/old.txt", to: "new.txt", overwrite:
|
||||
documentsProvider.removeItem(path: "new.txt", completionHandler: nil)
|
||||
```
|
||||
|
||||
***Caution:*** This method will delete directories with all it's contents recursively except for FTP providers that don't support `SITE RMDIR` command, this will be fixed later.
|
||||
|
||||
### Fetching Contents of File
|
||||
|
||||
There is two method for this purpose, one of them loads entire file into `Data` and another can load a portion of file.
|
||||
@@ -330,7 +336,7 @@ let data = "What's up Newyork!".data(encoding: .utf8)
|
||||
documentsProvider.writeContents(path: "old.txt", content: data, atomically: true, completionHandler: nil)
|
||||
```
|
||||
|
||||
### Copying Files to and From Local URL
|
||||
### Copying Files to and From Local Storage
|
||||
|
||||
There are two methods to download and upload files between provider's and local storage. These methods use `URLSessionDownloadTask` and `URLSessionUploadTask` classes and allows to use background session and provide progress via delegate.
|
||||
|
||||
@@ -351,6 +357,12 @@ documentsProvider.copyItem(path: "/download/image.jpg", toLocalURL: fileURL, ove
|
||||
* It's safe only to assume these methods **won't** handle directories to upload/download recursively. If you need, you can list directories, create directories on target and copy files using these methods.
|
||||
* FTP provider allows developer to either use apple implemented `URLSessionDownloadTask` or custom implemented method based on stream task via `useAppleImplementation` property. FTP protocol is not supported by background session.
|
||||
|
||||
### Operation Progress
|
||||
|
||||
Creating/Copying/Deleting/Searching functions return a `(NS)Progress`. It provides operation type, progress and a `.cancel()` method which allows you to cancel operation in midst. You can check `cancellable` property to check either you can cancel operation via this object or not.
|
||||
|
||||
- **Note:** Progress reporting is not supported by native `(NS)FileManager` so `LocalFileProvider`.
|
||||
|
||||
### Undo Operations
|
||||
|
||||
Providers conform to `FileProviderUndoable` can perform undo for **some** operations like moving/renaming, copying and creating (file or folder). **Now, only `LocalFileProvider` supports this feature.** To implement:
|
||||
@@ -394,12 +406,6 @@ class ViewController: UIViewController
|
||||
}
|
||||
```
|
||||
|
||||
### Operation Handle
|
||||
|
||||
Creating/Copying/Deleting functions return a `OperationHandle` for remote operations. It provides operation type, progress and a `.cancel()` method which allows you to cancel operation in midst.
|
||||
|
||||
It's not supported by native `(NS)FileManager` so `LocalFileProvider`, but this functionality will be added to future `PosixFileProvider` class.
|
||||
|
||||
### File Coordination
|
||||
|
||||
`LocalFileProvider` and its descendents has a `isCoordinating` property. By setting this, provider will use `NSFileCoordinating` class to do all file operations. It's mandatory for iCloud, while recommended when using shared container or anywhere that simultaneous operations on a file/folder is common.
|
||||
@@ -410,12 +416,12 @@ You can monitor updates in some file system (Local and SMB2), there is three met
|
||||
|
||||
```swift
|
||||
// to register a new notification handler
|
||||
documentsProvider.registerNotifcation(path: provider.currentPath) {
|
||||
documentsProvider.registerNotifcation(path: "/") {
|
||||
// calling functions to update UI
|
||||
}
|
||||
|
||||
// To discontinue monitoring folders:
|
||||
documentsProvider.unregisterNotifcation(path: provider.currentPath)
|
||||
documentsProvider.unregisterNotifcation(path: "/")
|
||||
```
|
||||
|
||||
* **Please note** in LocalFileProvider it will also monitor changes in subfolders. This behaviour can varies according to file system specification.
|
||||
@@ -431,9 +437,10 @@ To check either file thumbnail is supported or not and fetch thumbnail, use (and
|
||||
|
||||
```swift
|
||||
let path = "/newImage.jpg"
|
||||
let thumbSize = CGSize(width: 64, height: 64)
|
||||
let thumbSize = CGSize(width: 64, height: 64) // or nil which renders to default dimension of provider
|
||||
if documentsProvider.thumbnailOfFileSupported(path: path {
|
||||
documentsProvider.thumbnailOfFile(path: file.path, dimension: thumbSize, completionHandler: { (image, error) in
|
||||
// Interacting with UI must be placed in main thread
|
||||
DispatchQueue.main.async {
|
||||
self.previewImage.image = image
|
||||
}
|
||||
@@ -464,7 +471,7 @@ We would love for you to contribute to **FileProvider**, check the `LICENSE` fil
|
||||
Things you may consider to help us:
|
||||
|
||||
- [ ] Implement request/response stack for `SMBClient`
|
||||
- [ ] Implement Test-case (`XCTest`)
|
||||
- [x] Implement Test-case (`XCTest`)
|
||||
- [ ] Add Sample project for iOS
|
||||
- [ ] Add Sample project for macOS
|
||||
|
||||
@@ -485,20 +492,27 @@ Distributed under the MIT license. See `LICENSE` for more information.
|
||||
|
||||
[https://github.com/amosavian/](https://github.com/amosavian/)
|
||||
|
||||
[cocoapods]: https://cocoapods.org/pods/FileProvider
|
||||
[swift-image]: https://img.shields.io/badge/swift-3.0,%203.1-orange.svg
|
||||
[cocoapods]: https://cocoapods.org/pods/FilesProvider
|
||||
[cocoapods-old]: https://cocoapods.org/pods/FileProvider
|
||||
[swift-image]: https://img.shields.io/badge/swift-4.0-orange.svg
|
||||
[swift-url]: https://swift.org/
|
||||
[platform-image]: https://img.shields.io/cocoapods/p/FileProvider.svg
|
||||
[platform-image]: https://img.shields.io/cocoapods/p/FilesProvider.svg
|
||||
[license-image]: https://img.shields.io/github/license/amosavian/FileProvider.svg
|
||||
[license-url]: LICENSE
|
||||
[codebeat-image]: https://codebeat.co/badges/7b359f48-78eb-4647-ab22-56262a827517
|
||||
[codebeat-url]: https://codebeat.co/projects/github-com-amosavian-fileprovider
|
||||
[travis-image]: https://img.shields.io/travis/amosavian/FileProvider/master.svg
|
||||
[travis-image]: https://travis-ci.org/amosavian/FileProvider.svg
|
||||
[travis-url]: https://travis-ci.org/amosavian/FileProvider
|
||||
[release-url]: https://github.com/amosavian/FileProvider/releases
|
||||
[release-image]: https://img.shields.io/github/release/amosavian/FileProvider.svg
|
||||
[carthage-image]: https://img.shields.io/badge/Carthage-compatible-4BC51D.svg
|
||||
[cocoapods-downloads]: https://img.shields.io/cocoapods/dt/FileProvider.svg
|
||||
[cocoapods-apps]: https://img.shields.io/cocoapods/at/FileProvider.svg
|
||||
[docs-image]: https://img.shields.io/cocoapods/metrics/doc-percent/FileProvider.svg
|
||||
[docs-url]: http://cocoadocs.org/docsets/FileProvider/
|
||||
[cocoapods-downloads-old]: https://img.shields.io/cocoapods/dt/FileProvider.svg
|
||||
[cocoapods-apps-old]: https://img.shields.io/cocoapods/at/FileProvider.svg
|
||||
[cocoapods-downloads]: https://img.shields.io/cocoapods/dt/FilesProvider.svg
|
||||
[cocoapods-apps]: https://img.shields.io/cocoapods/at/FilesProvider.svg
|
||||
[docs-image]: https://img.shields.io/cocoapods/metrics/doc-percent/FilesProvider.svg
|
||||
[docs-url]: http://cocoadocs.org/docsets/FilesProvider/
|
||||
## Support on Beerpay
|
||||
Hey dude! Help me out for a couple of :beers:!
|
||||
|
||||
[](https://beerpay.io/amosavian/FileProvider) [](https://beerpay.io/amosavian/FileProvider?focus=wish)
|
||||
+417
-428
File diff suppressed because it is too large
Load Diff
+256
-354
@@ -8,107 +8,49 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
#if os(macOS) || os(iOS) || os(tvOS)
|
||||
import CoreGraphics
|
||||
#endif
|
||||
|
||||
/**
|
||||
Allows accessing to Dropbox stored files. This provider doesn't cache or save files internally, however you can
|
||||
set `useCache` and `cache` properties to use Foundation `NSURLCache` system.
|
||||
|
||||
- Note: You can pass file id or rev instead of file path, e.g `"id:1234abcd"` or `"rev:1234abcd"`, to point to a file or folder by ID.
|
||||
|
||||
- Note: Uploading files and data are limited to 150MB, for now.
|
||||
*/
|
||||
open class DropboxFileProvider: FileProviderBasicRemote {
|
||||
open class var type: String { return "DropBox" }
|
||||
open let baseURL: URL?
|
||||
open var currentPath: String
|
||||
open class DropboxFileProvider: HTTPFileProvider, FileProviderSharing {
|
||||
override open class var type: String { return "Dropbox" }
|
||||
|
||||
/// Dropbox RPC API URL, which is equal with [https://api.dropboxapi.com/2/](https://api.dropboxapi.com/2/)
|
||||
open let apiURL: URL
|
||||
/// Dropbox contents download/upload API URL, which is equal with [https://content.dropboxapi.com/2/](https://content.dropboxapi.com/2/)
|
||||
open let contentURL: URL
|
||||
|
||||
open var dispatch_queue: DispatchQueue
|
||||
open var operation_queue: OperationQueue {
|
||||
willSet {
|
||||
assert(_session == nil, "It's not effective to change dispatch_queue property after session is initialized.")
|
||||
}
|
||||
}
|
||||
|
||||
open weak var delegate: FileProviderDelegate?
|
||||
open var credential: URLCredential?
|
||||
open private(set) var cache: URLCache?
|
||||
public var useCache: Bool
|
||||
public var validatingCache: Bool
|
||||
|
||||
fileprivate var _session: URLSession?
|
||||
fileprivate var sessionDelegate: SessionDelegate?
|
||||
public var session: URLSession {
|
||||
if _session == nil {
|
||||
self.sessionDelegate = SessionDelegate(fileProvider: self, credential: credential)
|
||||
let config = URLSessionConfiguration.default
|
||||
config.urlCache = cache
|
||||
config.requestCachePolicy = .returnCacheDataElseLoad
|
||||
_session = URLSession(configuration: config, delegate: sessionDelegate as URLSessionDelegate?, delegateQueue: self.operation_queue)
|
||||
}
|
||||
return _session!
|
||||
}
|
||||
|
||||
fileprivate var _longpollSession: URLSession?
|
||||
internal var longpollSession: URLSession {
|
||||
if _longpollSession == nil {
|
||||
let config = URLSessionConfiguration.default
|
||||
config.timeoutIntervalForRequest = 600
|
||||
_longpollSession = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
|
||||
}
|
||||
return _longpollSession!
|
||||
}
|
||||
|
||||
/**
|
||||
Initializer for Dropbox provider with given client ID and Token.
|
||||
These parameters must be retrieved via [OAuth2 API of Dropbox](https://www.dropbox.com/developers/reference/oauth-guide).
|
||||
|
||||
There are libraries like [p2/OAuth2](https://github.com/p2/OAuth2) or [OAuthSwift](https://github.com/OAuthSwift/OAuthSwift) which can facilate the procedure to retrieve token.
|
||||
The latter is easier to use and prefered. Also you can use [auth0/Lock](https://github.com/auth0/Lock.iOS-OSX) which provides graphical user interface.
|
||||
There are libraries like [p2/OAuth2](https://github.com/p2/OAuth2) or [OAuthSwift](https://github.com/OAuthSwift/OAuthSwift) which can facilate the procedure to retrieve token. The latter is easier to use and prefered.
|
||||
|
||||
- Parameter credential: a `URLCredential` object with Client ID set as `user` and Token set as `password`.
|
||||
- Parameter cache: A URLCache to cache downloaded files and contents.
|
||||
*/
|
||||
public init(credential: URLCredential?, cache: URLCache? = nil) {
|
||||
self.baseURL = nil
|
||||
self.currentPath = ""
|
||||
self.useCache = false
|
||||
self.validatingCache = true
|
||||
self.cache = cache
|
||||
self.credential = credential
|
||||
|
||||
self.apiURL = URL(string: "https://api.dropboxapi.com/2/")!
|
||||
self.contentURL = URL(string: "https://content.dropboxapi.com/2/")!
|
||||
|
||||
dispatch_queue = DispatchQueue(label: "FileProvider.\(type(of: self).type)", attributes: .concurrent)
|
||||
operation_queue = OperationQueue()
|
||||
operation_queue.name = "FileProvider.\(type(of: self).type).Operation"
|
||||
super.init(baseURL: nil, credential: credential, cache: cache)
|
||||
}
|
||||
|
||||
public required convenience init?(coder aDecoder: NSCoder) {
|
||||
self.init(credential: aDecoder.decodeObject(forKey: "credential") as? URLCredential)
|
||||
self.currentPath = aDecoder.decodeObject(forKey: "currentPath") as? String ?? ""
|
||||
self.init(credential: aDecoder.decodeObject(of: URLCredential.self, forKey: "credential"))
|
||||
self.useCache = aDecoder.decodeBool(forKey: "useCache")
|
||||
self.validatingCache = aDecoder.decodeBool(forKey: "validatingCache")
|
||||
}
|
||||
|
||||
public func encode(with aCoder: NSCoder) {
|
||||
aCoder.encode(self.credential, forKey: "credential")
|
||||
aCoder.encode(self.currentPath, forKey: "currentPath")
|
||||
aCoder.encode(self.useCache, forKey: "useCache")
|
||||
aCoder.encode(self.validatingCache, forKey: "validatingCache")
|
||||
}
|
||||
|
||||
public static var supportsSecureCoding: Bool {
|
||||
return true
|
||||
}
|
||||
|
||||
open func copy(with zone: NSZone? = nil) -> Any {
|
||||
override open func copy(with zone: NSZone? = nil) -> Any {
|
||||
let copy = DropboxFileProvider(credential: self.credential, cache: self.cache)
|
||||
copy.currentPath = self.currentPath
|
||||
copy.delegate = self.delegate
|
||||
copy.fileOperationDelegate = self.fileOperationDelegate
|
||||
copy.useCache = self.useCache
|
||||
@@ -116,138 +58,202 @@ open class DropboxFileProvider: FileProviderBasicRemote {
|
||||
return copy
|
||||
}
|
||||
|
||||
deinit {
|
||||
if fileProviderCancelTasksOnInvalidating {
|
||||
_session?.invalidateAndCancel()
|
||||
} else {
|
||||
_session?.finishTasksAndInvalidate()
|
||||
}
|
||||
/**
|
||||
Returns an Array of `FileObject`s identifying the the directory entries via asynchronous completion handler.
|
||||
|
||||
If the directory contains no entries or an error is occured, this method will return the empty array.
|
||||
|
||||
- Parameters:
|
||||
- path: path to target directory. If empty, root will be iterated.
|
||||
- completionHandler: a closure with result of directory entries or error.
|
||||
- contents: An array of `FileObject` identifying the the directory entries.
|
||||
- error: Error returned by system.
|
||||
*/
|
||||
open override func contentsOfDirectory(path: String, completionHandler: @escaping (_ contents: [FileObject], _ error: Error?) -> Void) {
|
||||
let query = NSPredicate(format: "TRUEPREDICATE")
|
||||
_ = searchFiles(path: path, recursive: false, query: query, foundItemHandler: nil, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
open func contentsOfDirectory(path: String, completionHandler: @escaping ((_ contents: [FileObject], _ error: Error?) -> Void)) {
|
||||
list(path) { (contents, cursor, error) in
|
||||
completionHandler(contents, error)
|
||||
}
|
||||
}
|
||||
|
||||
open func attributesOfItem(path: String, completionHandler: @escaping ((_ attributes: FileObject?, _ error: Error?) -> Void)) {
|
||||
/**
|
||||
Returns a `FileObject` containing the attributes of the item (file, directory, symlink, etc.) at the path in question via asynchronous completion handler.
|
||||
|
||||
If the directory contains no entries or an error is occured, this method will return the empty `FileObject`.
|
||||
|
||||
- Parameters:
|
||||
- path: path to target directory. If empty, attributes of root will be returned.
|
||||
- completionHandler: a closure with result of directory entries or error.
|
||||
- attributes: A `FileObject` containing the attributes of the item.
|
||||
- error: Error returned by system.
|
||||
*/
|
||||
open override func attributesOfItem(path: String, completionHandler: @escaping (_ attributes: FileObject?, _ error: Error?) -> Void) {
|
||||
let url = URL(string: "files/get_metadata", relativeTo: apiURL)!
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "POST"
|
||||
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
|
||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
request.setValue(authentication: credential, with: .oAuth2)
|
||||
request.setValue(contentType: .json)
|
||||
let requestDictionary: [String: AnyObject] = ["path": correctPath(path)! as NSString]
|
||||
request.httpBody = Data(jsonDictionary: requestDictionary)
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var serverError: FileProviderDropboxError?
|
||||
var serverError: FileProviderHTTPError?
|
||||
var fileObject: DropboxFileObject?
|
||||
if let response = response as? HTTPURLResponse {
|
||||
if let response = response as? HTTPURLResponse, response.statusCode >= 400 {
|
||||
let code = FileProviderHTTPErrorCode(rawValue: response.statusCode)
|
||||
serverError = code != nil ? FileProviderDropboxError(code: code!, path: path, errorDescription: String(data: data ?? Data(), encoding: .utf8)) : nil
|
||||
if let json = data?.deserializeJSON(), let file = DropboxFileObject(json: json) {
|
||||
fileObject = file
|
||||
}
|
||||
serverError = code.flatMap { self.serverError(with: $0, path: path, data: data) }
|
||||
}
|
||||
if let json = data?.deserializeJSON(), let file = DropboxFileObject(json: json) {
|
||||
fileObject = file
|
||||
}
|
||||
completionHandler(fileObject, serverError ?? error)
|
||||
})
|
||||
task.resume()
|
||||
}
|
||||
|
||||
open func storageProperties(completionHandler: @escaping ((_ total: Int64, _ used: Int64) -> Void)) {
|
||||
/// Returns volume/provider information asynchronously.
|
||||
/// - Parameter volumeInfo: Information of filesystem/Provider returned by system/server.
|
||||
open override func storageProperties(completionHandler: @escaping (_ volumeInfo: VolumeObject?) -> Void) {
|
||||
let url = URL(string: "users/get_space_usage", relativeTo: apiURL)!
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "POST"
|
||||
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
|
||||
request.setValue(authentication: credential, with: .oAuth2)
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var totalSize: Int64 = -1
|
||||
var usedSize: Int64 = 0
|
||||
if let json = data?.deserializeJSON() {
|
||||
totalSize = ((json["allocation"] as? NSDictionary)?["allocated"] as? NSNumber)?.int64Value ?? -1
|
||||
usedSize = (json["used"] as? NSNumber)?.int64Value ?? 0
|
||||
guard let json = data?.deserializeJSON() else {
|
||||
completionHandler(nil)
|
||||
return
|
||||
}
|
||||
completionHandler(totalSize, usedSize)
|
||||
|
||||
let volume = VolumeObject(allValues: [:])
|
||||
volume.totalCapacity = ((json["allocation"] as? NSDictionary)?["allocated"] as? NSNumber)?.int64Value ?? -1
|
||||
volume.usage = (json["used"] as? NSNumber)?.int64Value ?? 0
|
||||
completionHandler(volume)
|
||||
})
|
||||
task.resume()
|
||||
}
|
||||
|
||||
open func searchFiles(path: String, recursive: Bool, query: NSPredicate, foundItemHandler: ((FileObject) -> Void)?, completionHandler: @escaping ((_ files: [FileObject], _ error: Error?) -> Void)) {
|
||||
var foundFiles = [DropboxFileObject]()
|
||||
if let queryStr = query.findValue(forKey: "name", operator: .beginsWith) as? String {
|
||||
// Dropbox only support searching for file names begin with query in non-enterprise accounts.
|
||||
// We will use it if there is a `name BEGINSWITH[c] "query"` in predicate, then filter to form final result.
|
||||
search(path, query: queryStr, foundItem: { (file) in
|
||||
if query.evaluate(with: file.mapPredicate()) {
|
||||
foundFiles.append(file)
|
||||
foundItemHandler?(file)
|
||||
}
|
||||
}, completionHandler: { (error) in
|
||||
completionHandler(foundFiles, error)
|
||||
})
|
||||
/**
|
||||
Search files inside directory using query asynchronously.
|
||||
|
||||
Sample predicates:
|
||||
```
|
||||
NSPredicate(format: "(name CONTAINS[c] 'hello') && (fileSize >= 10000)")
|
||||
NSPredicate(format: "(modifiedDate >= %@)", Date())
|
||||
NSPredicate(format: "(path BEGINSWITH %@)", "folder/child folder")
|
||||
```
|
||||
|
||||
- Note: Don't pass Spotlight predicates to this method directly, use `FileProvider.convertSpotlightPredicateTo()` method to get usable predicate.
|
||||
|
||||
- Important: A file name criteria should be provided for Dropbox.
|
||||
|
||||
- Parameters:
|
||||
- path: location of directory to start search
|
||||
- recursive: Searching subdirectories of path
|
||||
- query: An `NSPredicate` object with keys like `FileObject` members, except `size` which becomes `filesize`.
|
||||
- foundItemHandler: Closure which is called when a file is found
|
||||
- completionHandler: Closure which will be called after finishing search. Returns an arry of `FileObject` or error if occured.
|
||||
- files: all files meat the `query` criteria.
|
||||
- error: `Error` returned by server if occured.
|
||||
- Returns: An `Progress` to get progress or cancel progress. Use `completedUnitCount` to iterate count of found items.
|
||||
*/
|
||||
@discardableResult
|
||||
open override func searchFiles(path: String, recursive: Bool, query: NSPredicate, foundItemHandler: ((FileObject) -> Void)?, completionHandler: @escaping (_ files: [FileObject], _ error: Error?) -> Void) -> Progress? {
|
||||
let queryStr: String?
|
||||
if query.predicateFormat == "TRUEPREDICATE" {
|
||||
queryStr = nil
|
||||
} else {
|
||||
// Dropbox doesn't support searching attributes natively. The workaround is to fallback to listing all files
|
||||
// and filter it locally. It may have a network burden in case there is many files in Dropbox, so please use it concisely.
|
||||
list(path, recursive: true, progressHandler: { (files, _, error) in
|
||||
for file in files where query.evaluate(with: file.mapPredicate()) {
|
||||
queryStr = query.findValue(forKey: "name", operator: .beginsWith) as? String
|
||||
}
|
||||
let requestHandler = self.listRequest(path: path, queryStr: queryStr, recursive: recursive)
|
||||
let queryIsTruePredicate = query.predicateFormat == "TRUEPREDICATE"
|
||||
return paginated(path, requestHandler: requestHandler,
|
||||
pageHandler: { [weak self] (data, progress) -> (files: [FileObject], error: Error?, newToken: String?) in
|
||||
guard let json = data?.deserializeJSON(), let entries = (json["entries"] ?? json["matches"]) as? [AnyObject] else {
|
||||
let err = self?.urlError(path, code: .badServerResponse)
|
||||
return ([], err, nil)
|
||||
}
|
||||
|
||||
var files = [FileObject]()
|
||||
for entry in entries {
|
||||
if let entry = entry as? [String: AnyObject], let file = DropboxFileObject(json: entry), queryIsTruePredicate || query.evaluate(with: file.mapPredicate()) {
|
||||
files.append(file)
|
||||
progress.completedUnitCount += 1
|
||||
foundItemHandler?(file)
|
||||
}
|
||||
}, completionHandler: { (files, _, error) in
|
||||
let predicatedFiles = files.filter { query.evaluate(with: $0.mapPredicate()) }
|
||||
completionHandler(predicatedFiles, error)
|
||||
})
|
||||
}
|
||||
let ncursor: String?
|
||||
if let hasmore = (json["has_more"] as? NSNumber)?.boolValue, hasmore {
|
||||
ncursor = json["cursor"] as? String
|
||||
} else if let hasmore = (json["more"] as? NSNumber)?.boolValue, hasmore {
|
||||
ncursor = (json["start"] as? Int).flatMap(String.init)
|
||||
} else {
|
||||
ncursor = nil
|
||||
}
|
||||
return (files, nil, ncursor)
|
||||
}, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
override func request(for operation: FileOperationType, overwrite: Bool = false, attributes: [URLResourceKey : Any] = [:]) -> URLRequest {
|
||||
|
||||
func uploadRequest(to path: String) -> URLRequest {
|
||||
var requestDictionary = [String: AnyObject]()
|
||||
let url: URL = URL(string: "files/upload", relativeTo: contentURL)!
|
||||
requestDictionary["path"] = correctPath(path) as NSString?
|
||||
requestDictionary["mode"] = (overwrite ? "overwrite" : "add") as NSString
|
||||
//requestDictionary["client_modified"] = (attributes[.contentModificationDateKey] as? Date)?.format(with: .rfc3339) as NSString?
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "POST"
|
||||
request.setValue(authentication: credential, with: .oAuth2)
|
||||
request.setValue(contentType: .stream)
|
||||
request.setValue(dropboxArgKey: requestDictionary)
|
||||
return request
|
||||
}
|
||||
|
||||
func downloadRequest(from path: String) -> URLRequest {
|
||||
let url = URL(string: "files/download", relativeTo: contentURL)!
|
||||
var request = URLRequest(url: url)
|
||||
request = URLRequest(url: url)
|
||||
request.setValue(authentication: credential, with: .oAuth2)
|
||||
request.setValue(dropboxArgKey: ["path": correctPath(path)! as NSString])
|
||||
return request
|
||||
}
|
||||
|
||||
// content operations
|
||||
switch operation {
|
||||
case .copy(source: let source, destination: let dest) where dest.lowercased().hasPrefix("file://"):
|
||||
return downloadRequest(from: source)
|
||||
case .fetch(let path):
|
||||
return downloadRequest(from: path)
|
||||
case .copy(source: let source, destination: let dest) where source.lowercased().hasPrefix("file://"):
|
||||
return uploadRequest(to: dest)
|
||||
case .modify(let path):
|
||||
return uploadRequest(to: path)
|
||||
default:
|
||||
return self.apiRequest(for: operation, overwrite: overwrite)
|
||||
}
|
||||
}
|
||||
|
||||
open func isReachable(completionHandler: @escaping (Bool) -> Void) {
|
||||
self.storageProperties { total, _ in
|
||||
completionHandler(total > 0)
|
||||
}
|
||||
}
|
||||
|
||||
open weak var fileOperationDelegate: FileOperationDelegate?
|
||||
}
|
||||
|
||||
extension DropboxFileProvider: FileProviderOperations {
|
||||
open func create(folder folderName: String, at atPath: String, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
let path = (atPath as NSString).appendingPathComponent(folderName) + "/"
|
||||
return doOperation(.create(path: path), completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
open func moveItem(path: String, to toPath: String, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
return doOperation(.move(source: path, destination: toPath), completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
open func copyItem(path: String, to toPath: String, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
return doOperation(.copy(source: path, destination: toPath), completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
open func removeItem(path: String, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
return doOperation(.remove(path: path), completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
fileprivate func doOperation(_ operation: FileOperationType, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
guard fileOperationDelegate?.fileProvider(self, shouldDoOperation: operation) ?? true == true else {
|
||||
return nil
|
||||
}
|
||||
func apiRequest(for operation: FileOperationType, overwrite: Bool = false) -> URLRequest {
|
||||
let url: String
|
||||
guard let sourcePath = operation.source else { return nil }
|
||||
let sourcePath = operation.source
|
||||
let destPath = operation.destination
|
||||
var requestDictionary = [String: AnyObject]()
|
||||
switch operation {
|
||||
case .create:
|
||||
url = "files/create_folder"
|
||||
url = "files/create_folder_v2"
|
||||
|
||||
case .copy:
|
||||
url = "files/copy"
|
||||
url = "files/copy_v2"
|
||||
requestDictionary["allow_shared_folder"] = NSNumber(value: true)
|
||||
case .move:
|
||||
url = "files/move"
|
||||
url = "files/move_v2"
|
||||
requestDictionary["allow_shared_folder"] = NSNumber(value: true)
|
||||
case .remove:
|
||||
url = "files/delete"
|
||||
url = "files/delete_v2"
|
||||
default: // modify, link, fetch
|
||||
return nil
|
||||
fatalError("Unimplemented operation \(operation.description) in \(#file)")
|
||||
}
|
||||
var request = URLRequest(url: URL(string: url, relativeTo: apiURL)!)
|
||||
request.httpMethod = "POST"
|
||||
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
|
||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
var requestDictionary = [String: AnyObject]()
|
||||
request.setValue(authentication: credential, with: .oAuth2)
|
||||
request.setValue(contentType: .json)
|
||||
if let dest = correctPath(destPath) as NSString? {
|
||||
requestDictionary["from_path"] = correctPath(sourcePath) as NSString?
|
||||
requestDictionary["to_path"] = dest
|
||||
@@ -255,103 +261,21 @@ extension DropboxFileProvider: FileProviderOperations {
|
||||
requestDictionary["path"] = correctPath(sourcePath) as NSString?
|
||||
}
|
||||
request.httpBody = Data(jsonDictionary: requestDictionary)
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var serverError: FileProviderDropboxError?
|
||||
if let response = response as? HTTPURLResponse, response.statusCode >= 300, let code = FileProviderHTTPErrorCode(rawValue: response.statusCode) {
|
||||
serverError = FileProviderDropboxError(code: code, path: sourcePath, errorDescription: String(data: data ?? Data(), encoding: .utf8))
|
||||
}
|
||||
completionHandler?(serverError ?? error)
|
||||
self.delegateNotify(operation, error: serverError ?? error)
|
||||
})
|
||||
task.taskDescription = operation.json
|
||||
task.resume()
|
||||
return RemoteOperationHandle(operationType: operation, tasks: [task])
|
||||
return request
|
||||
}
|
||||
|
||||
open func copyItem(localFile: URL, to toPath: String, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
let opType = FileOperationType.copy(source: localFile.absoluteString, destination: toPath)
|
||||
guard fileOperationDelegate?.fileProvider(self, shouldDoOperation: opType) ?? true == true else {
|
||||
return nil
|
||||
override func serverError(with code: FileProviderHTTPErrorCode, path: String?, data: Data?) -> FileProviderHTTPError {
|
||||
let errorDesc: String?
|
||||
if let response = data?.deserializeJSON() {
|
||||
errorDesc = (response["user_message"] as? String) ?? (response["error"]?["tag"] as? String)
|
||||
} else {
|
||||
errorDesc = data.flatMap({ String(data: $0, encoding: .utf8) })
|
||||
}
|
||||
return upload_simple(toPath, localFile: localFile, overwrite: overwrite, operation: opType, completionHandler: completionHandler)
|
||||
return FileProviderDropboxError(code: code, path: path ?? "", serverDescription: errorDesc)
|
||||
}
|
||||
|
||||
open func copyItem(path: String, toLocalURL destURL: URL, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
let opType = FileOperationType.copy(source: path, destination: destURL.absoluteString)
|
||||
guard fileOperationDelegate?.fileProvider(self, shouldDoOperation: opType) ?? true == true else {
|
||||
return nil
|
||||
}
|
||||
let url = URL(string: "files/download", relativeTo: contentURL)!
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "GET"
|
||||
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
|
||||
let requestDictionary: [String: AnyObject] = ["path": path as NSString]
|
||||
let requestJson = String(jsonDictionary: requestDictionary) ?? ""
|
||||
request.setValue(requestJson, forHTTPHeaderField: "Dropbox-API-Arg")
|
||||
let task = session.downloadTask(with: request)
|
||||
completionHandlersForTasks[task.taskIdentifier] = completionHandler
|
||||
downloadCompletionHandlersForTasks[task.taskIdentifier] = { tempURL in
|
||||
guard let httpResponse = task.response as? HTTPURLResponse , httpResponse.statusCode < 300 else {
|
||||
let code = FileProviderHTTPErrorCode(rawValue: (task.response as? HTTPURLResponse)?.statusCode ?? -1)
|
||||
let errorData : Data? = nil //Data(contentsOf:cacheURL) // TODO: Figure out how to get error response data for the error description
|
||||
let serverError : FileProviderDropboxError? = code != nil ? FileProviderDropboxError(code: code!, path: path, errorDescription: String(data: errorData ?? Data(), encoding: .utf8)) : nil
|
||||
completionHandler?(serverError)
|
||||
return
|
||||
}
|
||||
do {
|
||||
try FileManager.default.moveItem(at: tempURL, to: destURL)
|
||||
completionHandler?(nil)
|
||||
} catch let e {
|
||||
completionHandler?(e)
|
||||
}
|
||||
}
|
||||
task.taskDescription = opType.json
|
||||
task.resume()
|
||||
return RemoteOperationHandle(operationType: opType, tasks: [task])
|
||||
}
|
||||
}
|
||||
|
||||
extension DropboxFileProvider: FileProviderReadWrite {
|
||||
open func contents(path: String, offset: Int64, length: Int, completionHandler: @escaping ((_ contents: Data?, _ error: Error?) -> Void)) -> OperationHandle? {
|
||||
if length == 0 || offset < 0 {
|
||||
dispatch_queue.async {
|
||||
completionHandler(Data(), nil)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
let opType = FileOperationType.fetch(path: path)
|
||||
let url = URL(string: "files/download", relativeTo: contentURL)!
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "GET"
|
||||
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
|
||||
if length > 0 {
|
||||
request.setValue("bytes=\(offset)-\(offset + Int64(length) - 1)", forHTTPHeaderField: "Range")
|
||||
} else if offset > 0 && length < 0 {
|
||||
request.setValue("bytes=\(offset)-", forHTTPHeaderField: "Range")
|
||||
}
|
||||
let requestDictionary: [String: AnyObject] = ["path": correctPath(path)! as NSString]
|
||||
request.setValue(String(jsonDictionary: requestDictionary), forHTTPHeaderField: "Dropbox-API-Arg")
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var serverError: FileProviderDropboxError?
|
||||
if let httpResponse = response as? HTTPURLResponse , httpResponse.statusCode >= 300, let code = FileProviderHTTPErrorCode(rawValue: httpResponse.statusCode) {
|
||||
serverError = FileProviderDropboxError(code: code, path: path, errorDescription: String(data: data ?? Data(), encoding: .utf8))
|
||||
}
|
||||
let filedata = serverError ?? error == nil ? data : nil
|
||||
completionHandler(filedata, serverError ?? error)
|
||||
})
|
||||
task.taskDescription = opType.json
|
||||
task.resume()
|
||||
return RemoteOperationHandle(operationType: opType, tasks: [task])
|
||||
}
|
||||
|
||||
public func writeContents(path: String, contents data: Data?, atomically: Bool, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
let opType = FileOperationType.modify(path: path)
|
||||
guard fileOperationDelegate?.fileProvider(self, shouldDoOperation: opType) ?? true == true else {
|
||||
return nil
|
||||
}
|
||||
// FIXME: remove 150MB restriction
|
||||
return upload_simple(path, data: data ?? Data(), overwrite: overwrite, operation: opType, completionHandler: completionHandler)
|
||||
override var maxUploadSimpleSupported: Int64 {
|
||||
return 157_286_400 // 150MB
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -369,60 +293,25 @@ extension DropboxFileProvider: FileProviderReadWrite {
|
||||
}
|
||||
*/
|
||||
// TODO: Implement /get_account & /get_current_account
|
||||
}
|
||||
|
||||
extension DropboxFileProvider {
|
||||
/// *OBSOLETED:* Use `publicLink(to:, completionHandler: (URL?, DropboxFileObject?, Date?, Error?))` function instead.
|
||||
@available(*, obsoleted: 1.0, renamed: "publicLink(to:completionHandler:)", message: "Use publicLink(to:, completionHandler: (URL?, DropboxFileObject?, Date?, Error?)) function instead.")
|
||||
open func temporaryLink(to path: String, completionHandler: @escaping ((_ link: URL?, _ attribute: DropboxFileObject?, _ error: Error?) -> Void)) {
|
||||
self.publicLink(to: path) { (url, file, _, error) in
|
||||
completionHandler(url, file, error)
|
||||
}
|
||||
}
|
||||
|
||||
/// *OBSOLETED:* Use `publicLink(to:, completionHandler: (URL?, DropboxFileObject?, Date?, Error?))` function instead.
|
||||
@available(*, obsoleted: 1.0, renamed: "publicLink(to:completionHandler:)", message: "Use publicLink(to:, completionHandler: (URL?, DropboxFileObject?, Date?, Error?)) function instead.")
|
||||
open func temporaryLink(to path: String, completionHandler: @escaping ((_ link: URL?, _ attribute: DropboxFileObject?, _ expiration: Date?, _ error: Error?) -> Void)) {
|
||||
self.publicLink(to: path) { (url, file, expiration, error) in
|
||||
completionHandler(url, file, expiration, error)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Genrates a public url to a file to be shared with other users and can be downloaded without authentication.
|
||||
|
||||
- Important: URL will be available for a limitied time (4 hours according to Dropbox documentation).
|
||||
|
||||
- Parameters:
|
||||
- to: path of file, including file/directory name.
|
||||
- completionHandler: a closure with result of directory entries or error.
|
||||
- `link`: a url returned by Dropbox to share.
|
||||
- `attribute`: a `FileObject` containing the attributes of the item.
|
||||
- `expiration`: a `Date` object, determines when the public url will expires.
|
||||
- `error`: Error returned by Dropbox.
|
||||
*/
|
||||
open func publicLink(to path: String, completionHandler: @escaping ((_ link: URL?, _ attribute: DropboxFileObject?, _ expiration: Date?, _ error: Error?) -> Void)) {
|
||||
open func publicLink(to path: String, completionHandler: @escaping ((_ link: URL?, _ attribute: FileObject?, _ expiration: Date?, _ error: Error?) -> Void)) {
|
||||
let url = URL(string: "files/get_temporary_link", relativeTo: apiURL)!
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "POST"
|
||||
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
|
||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
request.setValue(authentication: credential, with: .oAuth2)
|
||||
request.setValue(contentType: .json)
|
||||
let requestDictionary: [String: AnyObject] = ["path": correctPath(path)! as NSString]
|
||||
request.httpBody = Data(jsonDictionary: requestDictionary)
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var serverError: FileProviderDropboxError?
|
||||
var serverError: FileProviderHTTPError?
|
||||
var link: URL?
|
||||
var fileObject: DropboxFileObject?
|
||||
if let response = response as? HTTPURLResponse {
|
||||
let code = FileProviderHTTPErrorCode(rawValue: response.statusCode)
|
||||
serverError = code != nil ? FileProviderDropboxError(code: code!, path: path, errorDescription: String(data: data ?? Data(), encoding: .utf8)) : nil
|
||||
serverError = code.flatMap { self.serverError(with: $0, path: path, data: data) }
|
||||
if let json = data?.deserializeJSON() {
|
||||
if let linkStr = json["link"] as? String {
|
||||
link = URL(string: linkStr)
|
||||
}
|
||||
if let attribDic = json["metadata"] as? [String: AnyObject] {
|
||||
fileObject = DropboxFileObject(json: attribDic)
|
||||
}
|
||||
link = (json["link"] as? String).flatMap(URL.init(string:))
|
||||
fileObject = (json["metadata"] as? [String: AnyObject]).flatMap(DropboxFileObject.init(json:))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -445,28 +334,26 @@ extension DropboxFileProvider {
|
||||
*/
|
||||
open func copyItem(remoteURL: URL, to toPath: String, completionHandler: @escaping ((_ jobId: String?, _ attribute: DropboxFileObject?, _ error: Error?) -> Void)) {
|
||||
if remoteURL.isFileURL {
|
||||
completionHandler(nil, nil, self.throwError(remoteURL.path, code: URLError.badURL))
|
||||
completionHandler(nil, nil, self.urlError(remoteURL.path, code: .badURL))
|
||||
return
|
||||
}
|
||||
let url = URL(string: "files/save_url", relativeTo: apiURL)!
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "POST"
|
||||
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
|
||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
request.setValue(authentication: credential, with: .oAuth2)
|
||||
request.setValue(contentType: .json)
|
||||
let requestDictionary: [String: AnyObject] = ["path": correctPath(toPath)! as NSString, "url" : remoteURL.absoluteString as NSString]
|
||||
request.httpBody = Data(jsonDictionary: requestDictionary)
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var serverError: FileProviderDropboxError?
|
||||
var serverError: FileProviderHTTPError?
|
||||
var jobId: String?
|
||||
var fileObject: DropboxFileObject?
|
||||
if let response = response as? HTTPURLResponse {
|
||||
let code = FileProviderHTTPErrorCode(rawValue: response.statusCode)
|
||||
serverError = code != nil ? FileProviderDropboxError(code: code!, path: toPath, errorDescription: String(data: data ?? Data(), encoding: .utf8)) : nil
|
||||
serverError = code.flatMap { self.serverError(with: $0, path: toPath, data: data) }
|
||||
if let json = data?.deserializeJSON() {
|
||||
jobId = json["async_job_id"] as? String
|
||||
if let attribDic = json["metadata"] as? [String: AnyObject] {
|
||||
fileObject = DropboxFileObject(json: attribDic)
|
||||
}
|
||||
fileObject = (json["metadata"] as? [String: AnyObject]).flatMap(DropboxFileObject.init(json:))
|
||||
}
|
||||
}
|
||||
completionHandler(jobId, fileObject, serverError ?? error)
|
||||
@@ -486,15 +373,15 @@ extension DropboxFileProvider {
|
||||
let url = URL(string: "files/copy_reference/save", relativeTo: apiURL)!
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "POST"
|
||||
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
|
||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
request.setValue(authentication: credential, with: .oAuth2)
|
||||
request.setValue(contentType: .json)
|
||||
let requestDictionary: [String: AnyObject] = ["path": correctPath(toPath)! as NSString, "copy_reference" : reference as NSString]
|
||||
request.httpBody = Data(jsonDictionary: requestDictionary)
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var serverError: FileProviderDropboxError?
|
||||
var serverError: FileProviderHTTPError?
|
||||
if let response = response as? HTTPURLResponse {
|
||||
let code = FileProviderHTTPErrorCode(rawValue: response.statusCode)
|
||||
serverError = code != nil ? FileProviderDropboxError(code: code!, path: toPath, errorDescription: String(data: data ?? Data(), encoding: .utf8)) : nil
|
||||
serverError = code.flatMap { self.serverError(with: $0, path: toPath, data: data) }
|
||||
}
|
||||
completionHandler?(serverError ?? error)
|
||||
})
|
||||
@@ -503,6 +390,47 @@ extension DropboxFileProvider {
|
||||
}
|
||||
|
||||
extension DropboxFileProvider: ExtendedFileProvider {
|
||||
open func propertiesOfFileSupported(path: String) -> Bool {
|
||||
let fileExt = (path as NSString).pathExtension.lowercased()
|
||||
switch fileExt {
|
||||
case "jpg", "jpeg", "bmp", "gif", "png", "tif", "tiff":
|
||||
return true
|
||||
/*case "mp3", "aac", "m4a":
|
||||
return true*/
|
||||
case "mp4", "mpg", "3gp", "mov", "avi":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func propertiesOfFile(path: String, completionHandler: @escaping ((_ propertiesDictionary: [String : Any], _ keys: [String], _ error: Error?) -> Void)) -> Progress? {
|
||||
let url = URL(string: "files/get_metadata", relativeTo: apiURL)!
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "POST"
|
||||
request.setValue(authentication: credential, with: .oAuth2)
|
||||
request.setValue(contentType: .json)
|
||||
let requestDictionary: [String: AnyObject] = ["path": correctPath(path)! as NSString, "include_media_info": NSNumber(value: true)]
|
||||
request.httpBody = Data(jsonDictionary: requestDictionary)
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var serverError: FileProviderHTTPError?
|
||||
var dic = [String: Any]()
|
||||
var keys = [String]()
|
||||
if let response = response as? HTTPURLResponse {
|
||||
let code = FileProviderHTTPErrorCode(rawValue: response.statusCode)
|
||||
serverError = code.flatMap { self.serverError(with: $0, path: path, data: data) }
|
||||
if let json = data?.deserializeJSON(), let properties = (json["media_info"] as? [String: Any])?["metadata"] as? [String: Any] {
|
||||
(dic, keys) = self.mapMediaInfo(properties)
|
||||
}
|
||||
}
|
||||
completionHandler(dic, keys, serverError ?? error)
|
||||
})
|
||||
task.resume()
|
||||
return nil
|
||||
}
|
||||
|
||||
#if os(macOS) || os(iOS) || os(tvOS)
|
||||
open func thumbnailOfFileSupported(path: String) -> Bool {
|
||||
switch (path as NSString).pathExtension.lowercased() {
|
||||
case "jpg", "jpeg", "gif", "bmp", "png", "tif", "tiff":
|
||||
@@ -518,48 +446,46 @@ extension DropboxFileProvider: ExtendedFileProvider {
|
||||
}
|
||||
}
|
||||
|
||||
open func propertiesOfFileSupported(path: String) -> Bool {
|
||||
let fileExt = (path as NSString).pathExtension.lowercased()
|
||||
switch fileExt {
|
||||
case "jpg", "jpeg", "bmp", "gif", "png", "tif", "tiff":
|
||||
return true
|
||||
/*case "mp3", "aac", "m4a":
|
||||
return true*/
|
||||
case "mp4", "mpg", "3gp", "mov", "avi":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/// Default value for dimension is 64x64, according to Dropbox documentation
|
||||
open func thumbnailOfFile(path: String, dimension: CGSize?, completionHandler: @escaping ((_ image: ImageClass?, _ error: Error?) -> Void)) {
|
||||
@discardableResult
|
||||
open func thumbnailOfFile(path: String, dimension: CGSize?, completionHandler: @escaping ((_ image: ImageClass?, _ error: Error?) -> Void)) -> Progress? {
|
||||
let url: URL
|
||||
let thumbAPI: Bool
|
||||
switch (path as NSString).pathExtension.lowercased() {
|
||||
case "jpg", "jpeg", "gif", "bmp", "png", "tif", "tiff":
|
||||
url = URL(string: "files/get_thumbnail", relativeTo: contentURL)!
|
||||
thumbAPI = true
|
||||
case "doc", "docx", "docm", "xls", "xlsx", "xlsm":
|
||||
fallthrough
|
||||
case "ppt", "pps", "ppsx", "ppsm", "pptx", "pptm":
|
||||
fallthrough
|
||||
case "rtf":
|
||||
url = URL(string: "files/get_preview", relativeTo: contentURL)!
|
||||
thumbAPI = false
|
||||
default:
|
||||
return
|
||||
return nil
|
||||
}
|
||||
var request = URLRequest(url: url)
|
||||
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
|
||||
request.setValue(authentication: credential, with: .oAuth2)
|
||||
var requestDictionary: [String: AnyObject] = ["path": path as NSString]
|
||||
requestDictionary["format"] = "jpeg" as NSString
|
||||
if let dimension = dimension {
|
||||
requestDictionary["size"] = "w\(Int(dimension.width))h\(Int(dimension.height))" as NSString
|
||||
if thumbAPI {
|
||||
requestDictionary["format"] = "jpeg" as NSString
|
||||
let size: String
|
||||
switch dimension?.height ?? 64 {
|
||||
case 0...32: size = "w32h32"
|
||||
case 33...64: size = "w64h64"
|
||||
case 65...128: size = "w128h128"
|
||||
case 129...480: size = "w640h480"
|
||||
default: size = "w1024h768"
|
||||
}
|
||||
requestDictionary["size"] = size as NSString
|
||||
}
|
||||
request.setValue(String(jsonDictionary: requestDictionary), forHTTPHeaderField: "Dropbox-API-Arg")
|
||||
request.setValue(dropboxArgKey: requestDictionary)
|
||||
let task = self.session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var image: ImageClass? = nil
|
||||
if let r = response as? HTTPURLResponse, let result = r.allHeaderFields["Dropbox-API-Result"] as? String, let jsonResult = result.deserializeJSON() {
|
||||
if jsonResult["error"] != nil {
|
||||
completionHandler(nil, self.throwError(path, code: URLError.cannotDecodeRawData as FoundationErrorEnum))
|
||||
completionHandler(nil, self.urlError(path, code: .cannotDecodeRawData))
|
||||
}
|
||||
}
|
||||
if let data = data {
|
||||
@@ -567,38 +493,14 @@ extension DropboxFileProvider: ExtendedFileProvider {
|
||||
image = pageImage
|
||||
} else if let contentType = (response as? HTTPURLResponse)?.allHeaderFields["Content-Type"] as? String, contentType.contains("text/html") {
|
||||
// TODO: Implement converting html returned type of get_preview to image
|
||||
} else {
|
||||
image = ImageClass(data: data)
|
||||
} else if let fetchedimage = ImageClass(data: data) {
|
||||
image = dimension.map({ DropboxFileProvider.scaleDown(image: fetchedimage, toSize: $0) }) ?? fetchedimage
|
||||
}
|
||||
}
|
||||
completionHandler(image, error)
|
||||
})
|
||||
task.resume()
|
||||
return nil
|
||||
}
|
||||
|
||||
open func propertiesOfFile(path: String, completionHandler: @escaping ((_ propertiesDictionary: [String : Any], _ keys: [String], _ error: Error?) -> Void)) {
|
||||
let url = URL(string: "files/get_metadata", relativeTo: apiURL)!
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "POST"
|
||||
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
|
||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
let requestDictionary: [String: AnyObject] = ["path": correctPath(path)! as NSString, "include_media_info": NSNumber(value: true)]
|
||||
request.httpBody = Data(jsonDictionary: requestDictionary)
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var serverError: FileProviderDropboxError?
|
||||
var dic = [String: Any]()
|
||||
var keys = [String]()
|
||||
if let response = response as? HTTPURLResponse {
|
||||
let code = FileProviderHTTPErrorCode(rawValue: response.statusCode)
|
||||
serverError = code != nil ? FileProviderDropboxError(code: code!, path: path, errorDescription: String(data: data ?? Data(), encoding: .utf8)) : nil
|
||||
if let json = data?.deserializeJSON(), let properties = json["media_info"] as? [String: Any] {
|
||||
(dic, keys) = self.mapMediaInfo(properties)
|
||||
}
|
||||
}
|
||||
completionHandler(dic, keys, serverError ?? error)
|
||||
})
|
||||
task.resume()
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
extension DropboxFileProvider: FileProvider { }
|
||||
|
||||
+53
-142
@@ -12,27 +12,27 @@ import Foundation
|
||||
public struct FileProviderDropboxError: FileProviderHTTPError {
|
||||
public let code: FileProviderHTTPErrorCode
|
||||
public let path: String
|
||||
public let errorDescription: String?
|
||||
public let serverDescription: String?
|
||||
}
|
||||
|
||||
/// Containts path, url and attributes of a Dropbox file or resource.
|
||||
public final class DropboxFileObject: FileObject {
|
||||
internal init(name: String, path: String) {
|
||||
super.init(url: URL(string: path) ?? URL(string: "/")!, name: name, path: path)
|
||||
}
|
||||
|
||||
internal convenience init? (jsonStr: String) {
|
||||
guard let json = jsonStr.deserializeJSON() else { return nil }
|
||||
self.init(json: json)
|
||||
}
|
||||
|
||||
internal convenience init? (json: [String: AnyObject]) {
|
||||
internal init? (json: [String: AnyObject]) {
|
||||
var json = json
|
||||
if json["name"] == nil, let metadata = json["metadata"] as? [String: AnyObject] {
|
||||
json = metadata
|
||||
}
|
||||
guard let name = json["name"] as? String else { return nil }
|
||||
guard let path = json["path_display"] as? String else { return nil }
|
||||
self.init(name: name, path: path)
|
||||
super.init(url: nil, name: name, path: path)
|
||||
self.size = (json["size"] as? NSNumber)?.int64Value ?? -1
|
||||
self.serverTime = Date(rfcString: json["server_modified"] as? String ?? "")
|
||||
self.modifiedDate = Date(rfcString: json["client_modified"] as? String ?? "")
|
||||
self.serverTime = (json["server_modified"] as? String).flatMap(Date.init(rfcString:))
|
||||
self.modifiedDate = (json["client_modified"] as? String).flatMap(Date.init(rfcString:))
|
||||
self.type = (json[".tag"] as? String) == "folder" ? .directory : .regular
|
||||
self.isReadOnly = (json["sharing_info"]?["read_only"] as? NSNumber)?.boolValue ?? false
|
||||
self.id = json["id"] as? String
|
||||
@@ -51,13 +51,12 @@ public final class DropboxFileObject: FileObject {
|
||||
|
||||
/// The document identifier is a value assigned by the Dropbox to a file.
|
||||
/// This value is used to identify the document regardless of where it is moved on a volume.
|
||||
/// The identifier persists across system restarts.
|
||||
open internal(set) var id: String? {
|
||||
get {
|
||||
return allValues[.documentIdentifierKey] as? String
|
||||
return allValues[.fileResourceIdentifierKey] as? String
|
||||
}
|
||||
set {
|
||||
allValues[.documentIdentifierKey] = newValue
|
||||
allValues[.fileResourceIdentifierKey] = newValue
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,135 +72,57 @@ public final class DropboxFileObject: FileObject {
|
||||
}
|
||||
}
|
||||
|
||||
// codebeat:disable[ARITY]
|
||||
internal extension DropboxFileProvider {
|
||||
func list(_ path: String, cursor: String? = nil, prevContents: [DropboxFileObject] = [], recursive: Bool = false, session: URLSession? = nil, progressHandler: ((_ contents: [FileObject], _ nextCursor: String?, _ error: Error?) -> Void)? = nil, completionHandler: @escaping ((_ contents: [FileObject], _ cursor: String?, _ error: Error?) -> Void)) {
|
||||
var requestDictionary = [String: AnyObject]()
|
||||
let url: URL
|
||||
if let cursor = cursor {
|
||||
url = URL(string: "files/list_folder/continue", relativeTo: apiURL)!
|
||||
requestDictionary["cursor"] = cursor as NSString?
|
||||
} else {
|
||||
url = URL(string: "files/list_folder", relativeTo: apiURL)!
|
||||
requestDictionary["path"] = correctPath(path) as NSString?
|
||||
requestDictionary["recursive"] = recursive as NSNumber?
|
||||
internal func correctPath(_ path: String?) -> String? {
|
||||
guard let path = path else { return nil }
|
||||
if path.hasPrefix("id:") || path.hasPrefix("rev:") {
|
||||
return path
|
||||
}
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "POST"
|
||||
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
|
||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
request.httpBody = Data(jsonDictionary: requestDictionary)
|
||||
let task = (session ?? self.session).dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var responseError: FileProviderDropboxError?
|
||||
var files = [DropboxFileObject]()
|
||||
if let code = (response as? HTTPURLResponse)?.statusCode , code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) {
|
||||
responseError = FileProviderDropboxError(code: rCode, path: path, errorDescription: String(data: data ?? Data(), encoding: .utf8))
|
||||
}
|
||||
if let json = data?.deserializeJSON() {
|
||||
if let entries = json["entries"] as? [AnyObject] , entries.count > 0 {
|
||||
files.reserveCapacity(entries.count)
|
||||
for entry in entries {
|
||||
if let entry = entry as? [String: AnyObject], let file = DropboxFileObject(json: entry) {
|
||||
files.append(file)
|
||||
}
|
||||
}
|
||||
let ncursor = json["cursor"] as? String
|
||||
let hasmore = (json["has_more"] as? NSNumber)?.boolValue ?? false
|
||||
if hasmore {
|
||||
progressHandler?(files, ncursor, responseError ?? error)
|
||||
self.list(path, cursor: ncursor, prevContents: prevContents + files, completionHandler: completionHandler)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
progressHandler?(files, nil, responseError ?? error)
|
||||
completionHandler(prevContents + files, nil, responseError ?? error)
|
||||
})
|
||||
task.taskDescription = FileOperationType.fetch(path: path).json
|
||||
task.resume()
|
||||
var p = path.hasPrefix("/") ? path : "/" + path
|
||||
if p.hasSuffix("/") {
|
||||
p.remove(at: p.index(before:p.endIndex))
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func upload_simple(_ targetPath: String, data: Data? = nil, localFile: URL? = nil, modifiedDate: Date = Date(), overwrite: Bool, operation: FileOperationType, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
let size = data?.count ?? Int((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: AnyObject]()
|
||||
let url: URL
|
||||
url = URL(string: "files/upload", relativeTo: contentURL)!
|
||||
requestDictionary["path"] = correctPath(targetPath) as NSString?
|
||||
requestDictionary["mode"] = (overwrite ? "overwrite" : "add") as NSString
|
||||
requestDictionary["client_modified"] = modifiedDate.rfc3339utc() as NSString
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "POST"
|
||||
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
|
||||
request.setValue("application/octet-stream", forHTTPHeaderField: "Content-Type")
|
||||
request.setValue(String(jsonDictionary: requestDictionary), forHTTPHeaderField: "Dropbox-API-Arg")
|
||||
let task: URLSessionUploadTask
|
||||
if let data = data {
|
||||
task = session.uploadTask(with: request, from: data)
|
||||
} else if let localFile = localFile {
|
||||
task = session.uploadTask(with: request, fromFile: localFile)
|
||||
internal func listRequest(path: String, queryStr: String? = nil, recursive: Bool = false) -> ((_ token: String?) -> URLRequest?) {
|
||||
if let queryStr = queryStr {
|
||||
return { [weak self] (token) -> URLRequest? in
|
||||
guard let `self` = self else { return nil }
|
||||
let url = URL(string: "files/search", relativeTo: self.apiURL)!
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "POST"
|
||||
request.setValue(authentication: self.credential, with: .oAuth2)
|
||||
request.setValue(contentType: .json)
|
||||
var requestDictionary: [String: AnyObject] = ["path": self.correctPath(path)! as NSString]
|
||||
requestDictionary["query"] = queryStr as NSString
|
||||
requestDictionary["start"] = NSNumber(value: (token.flatMap( { Int($0) } ) ?? 0))
|
||||
request.httpBody = Data(jsonDictionary: requestDictionary)
|
||||
return request
|
||||
}
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
|
||||
completionHandlersForTasks[task.taskIdentifier] = { [weak self] error in
|
||||
var responseError: FileProviderDropboxError?
|
||||
if let code = (task.response as? HTTPURLResponse)?.statusCode , code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) {
|
||||
// We can't fetch server result from delegate!
|
||||
responseError = FileProviderDropboxError(code: rCode, path: targetPath, errorDescription: nil)
|
||||
}
|
||||
completionHandler?(responseError ?? error)
|
||||
self?.delegateNotify(.create(path: targetPath), error: responseError ?? error)
|
||||
}
|
||||
task.taskDescription = operation.json
|
||||
task.resume()
|
||||
return RemoteOperationHandle(operationType: operation, tasks: [task])
|
||||
}
|
||||
|
||||
func search(_ startPath: String = "", query: String, start: Int = 0, maxResultPerPage: Int = 25, maxResults: Int = -1, foundItem:@escaping ((_ file: DropboxFileObject) -> Void), completionHandler: @escaping ((_ error: Error?) -> Void)) {
|
||||
let url = URL(string: "files/search", relativeTo: apiURL)!
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "POST"
|
||||
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
|
||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
var requestDictionary: [String: AnyObject] = ["path": startPath as NSString]
|
||||
requestDictionary["query"] = query as NSString
|
||||
requestDictionary["start"] = start as NSNumber
|
||||
requestDictionary["max_results"] = maxResultPerPage as NSNumber
|
||||
request.httpBody = Data(jsonDictionary: requestDictionary)
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var responseError: FileProviderDropboxError?
|
||||
if let code = (response as? HTTPURLResponse)?.statusCode , code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) {
|
||||
responseError = FileProviderDropboxError(code: rCode, path: startPath, errorDescription: String(data: data ?? Data(), encoding: .utf8))
|
||||
}
|
||||
if let json = data?.deserializeJSON() {
|
||||
if let entries = json["matches"] as? [AnyObject] , entries.count > 0 {
|
||||
for entry in entries {
|
||||
if let entry = entry as? [String: AnyObject], let file = DropboxFileObject(json: entry) {
|
||||
foundItem(file)
|
||||
}
|
||||
}
|
||||
let rstart = json["start"] as? Int
|
||||
let hasmore = (json["more"] as? NSNumber)?.boolValue ?? false
|
||||
if hasmore, let rstart = rstart {
|
||||
self.search(startPath, query: query, start: rstart + entries.count, maxResultPerPage: maxResultPerPage, foundItem: foundItem, completionHandler: completionHandler)
|
||||
} else {
|
||||
completionHandler(responseError ?? error)
|
||||
}
|
||||
return
|
||||
return { [weak self] (token) -> URLRequest? in
|
||||
guard let `self` = self else { return nil }
|
||||
var requestDictionary = [String: AnyObject]()
|
||||
let url: URL
|
||||
if let token = token {
|
||||
url = URL(string: "files/list_folder/continue", relativeTo: self.apiURL)!
|
||||
requestDictionary["cursor"] = token as NSString?
|
||||
} else {
|
||||
url = URL(string: "files/list_folder", relativeTo: self.apiURL)!
|
||||
requestDictionary["path"] = self.correctPath(path) as NSString?
|
||||
requestDictionary["recursive"] = NSNumber(value: recursive)
|
||||
}
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "POST"
|
||||
request.setValue(authentication: self.credential, with: .oAuth2)
|
||||
request.setValue(contentType: .json)
|
||||
request.httpBody = Data(jsonDictionary: requestDictionary)
|
||||
return request
|
||||
}
|
||||
completionHandler(responseError ?? error)
|
||||
})
|
||||
task.resume()
|
||||
}
|
||||
}
|
||||
}
|
||||
// codebeat:enable[ARITY]
|
||||
|
||||
internal extension DropboxFileProvider {
|
||||
static let dateFormatter = DateFormatter()
|
||||
@@ -234,14 +155,4 @@ internal extension DropboxFileProvider {
|
||||
}
|
||||
return (dic, keys)
|
||||
}
|
||||
|
||||
func delegateNotify(_ operation: FileOperationType, error: Error?) {
|
||||
DispatchQueue.main.async(execute: {
|
||||
if error == nil {
|
||||
self.delegate?.fileproviderSucceed(self, operation: operation)
|
||||
} else {
|
||||
self.delegate?.fileproviderFailed(self, operation: operation)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,47 +6,48 @@
|
||||
// Copyright © 2017 Mousavian. Distributed under MIT license.
|
||||
//
|
||||
|
||||
#if os(macOS) || os(iOS) || os(tvOS)
|
||||
import Foundation
|
||||
import ImageIO
|
||||
import CoreGraphics
|
||||
import AVFoundation
|
||||
|
||||
extension LocalFileProvider: ExtendedFileProvider {
|
||||
public func thumbnailOfFileSupported(path: String) -> Bool {
|
||||
open func thumbnailOfFileSupported(path: String) -> Bool {
|
||||
switch (path as NSString).pathExtension.lowercased() {
|
||||
case LocalFileInformationGenerator.imageThumbnailExtensions:
|
||||
case LocalFileInformationGenerator.imageThumbnailExtensions.contains:
|
||||
return true
|
||||
case LocalFileInformationGenerator.audioThumbnailExtensions:
|
||||
case LocalFileInformationGenerator.audioThumbnailExtensions.contains:
|
||||
return true
|
||||
case LocalFileInformationGenerator.videoThumbnailExtensions:
|
||||
case LocalFileInformationGenerator.videoThumbnailExtensions.contains:
|
||||
return true
|
||||
case LocalFileInformationGenerator.pdfThumbnailExtensions:
|
||||
case LocalFileInformationGenerator.pdfThumbnailExtensions.contains:
|
||||
return true
|
||||
case LocalFileInformationGenerator.officeThumbnailExtensions:
|
||||
case LocalFileInformationGenerator.officeThumbnailExtensions.contains:
|
||||
return true
|
||||
case LocalFileInformationGenerator.customThumbnailExtensions:
|
||||
case LocalFileInformationGenerator.customThumbnailExtensions.contains:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
public func propertiesOfFileSupported(path: String) -> Bool {
|
||||
open func propertiesOfFileSupported(path: String) -> Bool {
|
||||
let fileExt = (path as NSString).pathExtension.lowercased()
|
||||
switch fileExt {
|
||||
case LocalFileInformationGenerator.imagePropertiesExtensions:
|
||||
case LocalFileInformationGenerator.imagePropertiesExtensions.contains:
|
||||
return LocalFileInformationGenerator.imageProperties != nil
|
||||
case LocalFileInformationGenerator.audioPropertiesExtensions:
|
||||
case LocalFileInformationGenerator.audioPropertiesExtensions.contains:
|
||||
return LocalFileInformationGenerator.audioProperties != nil
|
||||
case LocalFileInformationGenerator.videoPropertiesExtensions:
|
||||
case LocalFileInformationGenerator.videoPropertiesExtensions.contains:
|
||||
return LocalFileInformationGenerator.videoProperties != nil
|
||||
case LocalFileInformationGenerator.pdfPropertiesExtensions:
|
||||
case LocalFileInformationGenerator.pdfPropertiesExtensions.contains:
|
||||
return LocalFileInformationGenerator.pdfProperties != nil
|
||||
case LocalFileInformationGenerator.archivePropertiesExtensions:
|
||||
case LocalFileInformationGenerator.archivePropertiesExtensions.contains:
|
||||
return LocalFileInformationGenerator.archiveProperties != nil
|
||||
case LocalFileInformationGenerator.officePropertiesExtensions:
|
||||
case LocalFileInformationGenerator.officePropertiesExtensions.contains:
|
||||
return LocalFileInformationGenerator.officeProperties != nil
|
||||
case LocalFileInformationGenerator.customPropertiesExtensions:
|
||||
case LocalFileInformationGenerator.customPropertiesExtensions.contains:
|
||||
return LocalFileInformationGenerator.customProperties != nil
|
||||
|
||||
default:
|
||||
@@ -54,7 +55,8 @@ extension LocalFileProvider: ExtendedFileProvider {
|
||||
}
|
||||
}
|
||||
|
||||
public func thumbnailOfFile(path: String, dimension: CGSize? = nil, completionHandler: @escaping ((_ image: ImageClass?, _ error: Error?) -> Void)) {
|
||||
@discardableResult
|
||||
open func thumbnailOfFile(path: String, dimension: CGSize? = nil, completionHandler: @escaping ((_ image: ImageClass?, _ error: Error?) -> Void)) -> Progress? {
|
||||
let dimension = dimension ?? CGSize(width: 64, height: 64)
|
||||
(dispatch_queue).async {
|
||||
var thumbnailImage: ImageClass? = nil
|
||||
@@ -62,17 +64,17 @@ extension LocalFileProvider: ExtendedFileProvider {
|
||||
let fileURL = self.url(of: path)
|
||||
// Create Thumbnail and cache
|
||||
switch fileURL.pathExtension.lowercased() {
|
||||
case LocalFileInformationGenerator.videoThumbnailExtensions:
|
||||
case LocalFileInformationGenerator.videoThumbnailExtensions.contains:
|
||||
thumbnailImage = LocalFileInformationGenerator.videoThumbnail(fileURL)
|
||||
case LocalFileInformationGenerator.audioThumbnailExtensions:
|
||||
case LocalFileInformationGenerator.audioThumbnailExtensions.contains:
|
||||
thumbnailImage = LocalFileInformationGenerator.audioThumbnail(fileURL)
|
||||
case LocalFileInformationGenerator.imageThumbnailExtensions:
|
||||
case LocalFileInformationGenerator.imageThumbnailExtensions.contains:
|
||||
thumbnailImage = LocalFileInformationGenerator.imageThumbnail(fileURL)
|
||||
case LocalFileInformationGenerator.pdfThumbnailExtensions:
|
||||
case LocalFileInformationGenerator.pdfThumbnailExtensions.contains:
|
||||
thumbnailImage = LocalFileInformationGenerator.pdfThumbnail(fileURL)
|
||||
case LocalFileInformationGenerator.officeThumbnailExtensions:
|
||||
case LocalFileInformationGenerator.officeThumbnailExtensions.contains:
|
||||
thumbnailImage = LocalFileInformationGenerator.officeThumbnail(fileURL)
|
||||
case LocalFileInformationGenerator.customThumbnailExtensions:
|
||||
case LocalFileInformationGenerator.customThumbnailExtensions.contains:
|
||||
thumbnailImage = LocalFileInformationGenerator.customThumbnail(fileURL)
|
||||
default:
|
||||
completionHandler(nil, nil)
|
||||
@@ -84,26 +86,28 @@ extension LocalFileProvider: ExtendedFileProvider {
|
||||
completionHandler(scaledImage, nil)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
public func propertiesOfFile(path: String, completionHandler: @escaping ((_ propertiesDictionary: [String: Any], _ keys: [String], _ error: Error?) -> Void)) {
|
||||
@discardableResult
|
||||
open func propertiesOfFile(path: String, completionHandler: @escaping ((_ propertiesDictionary: [String: Any], _ keys: [String], _ error: Error?) -> Void)) -> Progress? {
|
||||
(dispatch_queue).async {
|
||||
let fileExt = (path as NSString).pathExtension.lowercased()
|
||||
var getter: ((_ fileURL: URL) -> (prop: [String: Any], keys: [String]))?
|
||||
switch fileExt {
|
||||
case LocalFileInformationGenerator.imagePropertiesExtensions:
|
||||
case LocalFileInformationGenerator.imagePropertiesExtensions.contains:
|
||||
getter = LocalFileInformationGenerator.imageProperties
|
||||
case LocalFileInformationGenerator.audioPropertiesExtensions:
|
||||
case LocalFileInformationGenerator.audioPropertiesExtensions.contains:
|
||||
getter = LocalFileInformationGenerator.audioProperties
|
||||
case LocalFileInformationGenerator.videoPropertiesExtensions:
|
||||
case LocalFileInformationGenerator.videoPropertiesExtensions.contains:
|
||||
getter = LocalFileInformationGenerator.videoProperties
|
||||
case LocalFileInformationGenerator.pdfPropertiesExtensions:
|
||||
case LocalFileInformationGenerator.pdfPropertiesExtensions.contains:
|
||||
getter = LocalFileInformationGenerator.pdfProperties
|
||||
case LocalFileInformationGenerator.archivePropertiesExtensions:
|
||||
case LocalFileInformationGenerator.archivePropertiesExtensions.contains:
|
||||
getter = LocalFileInformationGenerator.archiveProperties
|
||||
case LocalFileInformationGenerator.officePropertiesExtensions:
|
||||
case LocalFileInformationGenerator.officePropertiesExtensions.contains:
|
||||
getter = LocalFileInformationGenerator.officeProperties
|
||||
case LocalFileInformationGenerator.customPropertiesExtensions:
|
||||
case LocalFileInformationGenerator.customPropertiesExtensions.contains:
|
||||
getter = LocalFileInformationGenerator.customProperties
|
||||
default:
|
||||
break
|
||||
@@ -117,6 +121,7 @@ extension LocalFileProvider: ExtendedFileProvider {
|
||||
|
||||
completionHandler(dic, keys, nil)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,18 +130,18 @@ public struct LocalFileInformationGenerator {
|
||||
/// Image extensions supportes for thumbnail.
|
||||
///
|
||||
/// Default: `["jpg", "jpeg", "gif", "bmp", "png", "tif", "tiff", "ico"]`
|
||||
static public var imageThumbnailExtensions: [String] = ["jpg", "jpeg", "gif", "bmp", "png", "tif", "tiff", "ico"]
|
||||
static public var imageThumbnailExtensions: [String] = ["heic", "jpg", "jpeg", "gif", "bmp", "png", "tif", "tiff", "ico"]
|
||||
|
||||
/// Audio and music extensions supportes for thumbnail.
|
||||
///
|
||||
/// Default: `["mp3", "aac", "m4a"]`
|
||||
static public var audioThumbnailExtensions: [String] = ["mp3", "aac", "m4a"]
|
||||
/// Default: `["mp1", "mp2", "mp3", "mpa", "mpga", "m1a", "m2a", "m4a", "m4b", "m4p", "m4r", "aac", "snd", "caf", "aa", "aax", "adts", "aif", "aifc", "aiff", "au", "flac", "amr", "wav", "wave", "bwf", "ac3", "eac3", "ec3", "cdda"]`
|
||||
static public var audioThumbnailExtensions: [String] = ["mp1", "mp2", "mp3", "mpa", "mpga", "m1a", "m2a", "m4a", "m4b", "m4p", "m4r", "aac", "snd", "caf", "aa", "aax", "adts", "aif", "aifc", "aiff", "au", "flac", "amr", "wav", "wave", "bwf", "ac3", "eac3", "ec3", "cdda"]
|
||||
|
||||
/// Video extensions supportes for thumbnail.
|
||||
///
|
||||
/// Default: `["mov", "mp4", "m4v", "mpg", "mpeg"]`
|
||||
static public var videoThumbnailExtensions: [String] = ["mov", "mp4", "m4v", "mpg", "mpeg"]
|
||||
|
||||
/// Default: `["mov", "mp4", "mpg4", "m4v", "mqv", "mpg", "mpeg", "avi", "vfw", "3g2", "3gp", "3gp2", "3gpp", "qt"]`
|
||||
static public var videoThumbnailExtensions: [String] = ["mov", "mp4", "mpg4", "m4v", "mqv", "mpg", "mpeg", "avi", "vfw", "3g2", "3gp", "3gp2", "3gpp", "qt"]
|
||||
|
||||
/// Portable document file extensions supportes for thumbnail.
|
||||
///
|
||||
/// Default: `["pdf"]`
|
||||
@@ -156,17 +161,17 @@ public struct LocalFileInformationGenerator {
|
||||
/// Image extensions supportes for properties.
|
||||
///
|
||||
/// Default: `["jpg", "jpeg", "gif", "bmp", "png", "tif", "tiff"]`
|
||||
static public var imagePropertiesExtensions: [String] = ["jpg", "jpeg", "bmp", "gif", "png", "tif", "tiff"]
|
||||
static public var imagePropertiesExtensions: [String] = ["heic", "jpg", "jpeg", "bmp", "gif", "png", "tif", "tiff"]
|
||||
|
||||
/// Audio and music extensions supportes for properties.
|
||||
///
|
||||
/// Default: `["mp3", "aac", "m4a", "caf"]`
|
||||
static public var audioPropertiesExtensions: [String] = ["mp3", "aac", "m4a", "caf"]
|
||||
/// Default: `["mp1", "mp2", "mp3", "mpa", "mpga", "m1a", "m2a", "m4a", "m4b", "m4p", "m4r", "aac", "snd", "caf", "aa", "aax", "adts", "aif", "aifc", "aiff", "au", "flac", "amr", "wav", "wave", "bwf", "ac3", "eac3", "ec3", "cdda"]`
|
||||
static public var audioPropertiesExtensions: [String] = ["mp1", "mp2", "mp3", "mpa", "mpga", "m1a", "m2a", "m4a", "m4b", "m4p", "m4r", "aac", "snd", "caf", "aa", "aax", "adts", "aif", "aifc", "aiff", "au", "flac", "amr", "wav", "wave", "bwf", "ac3", "eac3", "ec3", "cdda"]
|
||||
|
||||
/// Video extensions supportes for properties.
|
||||
///
|
||||
/// Default: `["mp4", "mpg", "3gp", "mov", "avi"]`
|
||||
static public var videoPropertiesExtensions: [String] = ["mp4", "mpg", "3gp", "mov", "avi"]
|
||||
/// Default: `["mov", "mp4", "mpg4", "m4v", "mqv", "mpg", "mpeg", "avi", "vfw", "3g2", "3gp", "3gp2", "3gpp", "qt"]`
|
||||
static public var videoPropertiesExtensions: [String] = ["mov", "mp4", "mpg4", "m4v", "mqv", "mpg", "mpeg", "avi", "vfw", "3g2", "3gp", "3gp2", "3gpp", "qt"]
|
||||
|
||||
/// Portable document file extensions supportes for properties.
|
||||
///
|
||||
@@ -197,8 +202,13 @@ public struct LocalFileInformationGenerator {
|
||||
static public var audioThumbnail: (_ fileURL: URL) -> ImageClass? = { fileURL in
|
||||
let playerItem = AVPlayerItem(url: fileURL)
|
||||
let metadataList = playerItem.asset.commonMetadata
|
||||
#if swift(>=4.0)
|
||||
let commonKeyArtwork = AVMetadataKey.commonKeyArtwork
|
||||
#else
|
||||
let commonKeyArtwork = AVMetadataCommonKeyArtwork
|
||||
#endif
|
||||
for item in metadataList {
|
||||
if item.commonKey == AVMetadataCommonKeyArtwork {
|
||||
if item.commonKey == commonKeyArtwork {
|
||||
if let data = item.dataValue {
|
||||
return ImageClass(data: data)
|
||||
}
|
||||
@@ -212,7 +222,7 @@ public struct LocalFileInformationGenerator {
|
||||
let asset = AVAsset(url: fileURL)
|
||||
let assetImgGenerate = AVAssetImageGenerator(asset: asset)
|
||||
assetImgGenerate.appliesPreferredTrackTransform = true
|
||||
let time = CMTimeMake(asset.duration.value / 3, asset.duration.timescale)
|
||||
let time = CMTime(value: asset.duration.value / 3, timescale: asset.duration.timescale)
|
||||
if let cgImage = try? assetImgGenerate.copyCGImage(at: time, actualTime: nil) {
|
||||
#if os(macOS)
|
||||
return ImageClass(cgImage: cgImage, size: .zero)
|
||||
@@ -272,26 +282,31 @@ public struct LocalFileInformationGenerator {
|
||||
let imageDict = cfImageDict as NSDictionary
|
||||
let tiffDict = imageDict[kCGImagePropertyTIFFDictionary as String] as? NSDictionary ?? [:]
|
||||
let exifDict = imageDict[kCGImagePropertyExifDictionary as String] as? NSDictionary ?? [:]
|
||||
let gpsDict = imageDict[kCGImagePropertyGPSDictionary as String] as? NSDictionary ?? [:]
|
||||
|
||||
if let pixelWidth = imageDict.object(forKey: kCGImagePropertyPixelWidth) as? NSNumber, let pixelHeight = imageDict.object(forKey: kCGImagePropertyPixelHeight) as? NSNumber {
|
||||
add(key: "Dimensions", value: "\(pixelWidth)x\(pixelHeight)")
|
||||
}
|
||||
|
||||
add(key: "DPI", value: imageDict[kCGImagePropertyDPIWidth as String])
|
||||
add(key: "Device make", value: tiffDict[kCGImagePropertyTIFFMake as String])
|
||||
add(key: "Device maker", value: tiffDict[kCGImagePropertyTIFFMake as String])
|
||||
add(key: "Device model", value: tiffDict[kCGImagePropertyTIFFModel as String])
|
||||
add(key: "Lens model", value: exifDict[kCGImagePropertyExifLensModel as String])
|
||||
add(key: "Artist", value: tiffDict[kCGImagePropertyTIFFArtist as String] as? String)
|
||||
add(key: "Copyright", value: tiffDict[kCGImagePropertyTIFFCopyright as String] as? String)
|
||||
add(key: "Date taken", value: tiffDict[kCGImagePropertyTIFFDateTime as String] as? String)
|
||||
|
||||
if let latitude = tiffDict[kCGImagePropertyGPSLatitude as String] as? NSNumber, let longitude = tiffDict[kCGImagePropertyGPSLongitude as String] as? NSNumber {
|
||||
add(key: "Location", value: "\(latitude), \(longitude)")
|
||||
if let latitude = gpsDict[kCGImagePropertyGPSLatitude as String] as? NSNumber,
|
||||
let longitude = gpsDict[kCGImagePropertyGPSLongitude as String] as? NSNumber {
|
||||
let altitudeDesc = (gpsDict[kCGImagePropertyGPSAltitude as String] as? NSNumber).map({ " at \($0.format(precision: 0))m" }) ?? ""
|
||||
add(key: "Location", value: "\(latitude.format()), \(longitude.format())\(altitudeDesc)")
|
||||
}
|
||||
add(key: "Altitude", value: tiffDict[kCGImagePropertyGPSAltitude as String] as? NSNumber)
|
||||
add(key: "Area", value: tiffDict[kCGImagePropertyGPSAreaInformation as String])
|
||||
add(key: "Area", value: gpsDict[kCGImagePropertyGPSAreaInformation as String])
|
||||
|
||||
add(key: "Color space", value: imageDict[kCGImagePropertyColorModel as String])
|
||||
add(key: "Color depth", value: (imageDict[kCGImagePropertyDepth as String] as? NSNumber).map({ "\($0) bits" }))
|
||||
add(key: "Color profile", value: imageDict[kCGImagePropertyProfileName as String])
|
||||
add(key: "Focal length", value: exifDict[kCGImagePropertyExifFocalLength as String])
|
||||
add(key: "White banance", value: exifDict[kCGImagePropertyExifWhiteBalance as String])
|
||||
add(key: "F number", value: exifDict[kCGImagePropertyExifFNumber as String])
|
||||
add(key: "Exposure program", value: exifDict[kCGImagePropertyExifExposureProgram as String])
|
||||
|
||||
@@ -315,32 +330,60 @@ public struct LocalFileInformationGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
func makeDescription(_ key: String?) -> String? {
|
||||
func makeKeyDescription(_ key: String?) -> String? {
|
||||
guard let key = key else {
|
||||
return nil
|
||||
}
|
||||
guard let regex = try? NSRegularExpression(pattern: "([a-z])([A-Z])" , options: NSRegularExpression.Options()) else {
|
||||
guard let regex = try? NSRegularExpression(pattern: "([a-z])([A-Z])" , options: []) else {
|
||||
return nil
|
||||
}
|
||||
let newKey = regex.stringByReplacingMatches(in: key, options: NSRegularExpression.MatchingOptions(), range: NSMakeRange(0, (key as NSString).length) , withTemplate: "$1 $2")
|
||||
let newKey = regex.stringByReplacingMatches(in: key, options: [], range: NSRange(location: 0, length: (key as NSString).length) , withTemplate: "$1 $2")
|
||||
return newKey.capitalized
|
||||
}
|
||||
|
||||
if FileManager.default.fileExists(atPath: fileURL.path) {
|
||||
let playerItem = AVPlayerItem(url: fileURL)
|
||||
let metadataList = playerItem.asset.commonMetadata
|
||||
for item in metadataList {
|
||||
if let description = makeDescription(item.commonKey) {
|
||||
if let value = item.stringValue {
|
||||
keys.append(description)
|
||||
dic[description] = value
|
||||
}
|
||||
func parseLocationData(_ value: String) -> (latitude: Double, longitude: Double, height: Double?)? {
|
||||
let scanner = Scanner.init(string: value)
|
||||
var latitude: Double = 0.0, longitude: Double = 0.0, height: Double = 0
|
||||
|
||||
if scanner.scanDouble(&latitude), scanner.scanDouble(&longitude) {
|
||||
scanner.scanDouble(&height)
|
||||
return (latitude, longitude, height)
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
guard fileURL.fileExists else {
|
||||
return (dic, keys)
|
||||
}
|
||||
let playerItem = AVPlayerItem(url: fileURL)
|
||||
let metadataList = playerItem.asset.commonMetadata
|
||||
for item in metadataList {
|
||||
#if swift(>=4.0)
|
||||
let commonKey = item.commonKey?.rawValue
|
||||
#else
|
||||
let commonKey = item.commonKey
|
||||
#endif
|
||||
if let key = makeKeyDescription(commonKey) {
|
||||
if commonKey == "location", let value = item.stringValue, let loc = parseLocationData(value) {
|
||||
keys.append(key)
|
||||
let heightStr: String = (loc.height as NSNumber?).map({ ", \($0.format(precision: 0))m" }) ?? ""
|
||||
dic[key] = "\((loc.latitude as NSNumber).format())°, \((loc.longitude as NSNumber).format())°\(heightStr)"
|
||||
} else if let value = item.dateValue {
|
||||
keys.append(key)
|
||||
dic[key] = value
|
||||
} else if let value = item.numberValue {
|
||||
keys.append(key)
|
||||
dic[key] = value
|
||||
} else if let value = item.stringValue {
|
||||
keys.append(key)
|
||||
dic[key] = value
|
||||
}
|
||||
}
|
||||
if let ap = try? AVAudioPlayer(contentsOf: fileURL) {
|
||||
add(key: "Duration", value: ap.duration.formatshort)
|
||||
add(key: "Bitrate", value: ap.settings[AVSampleRateKey] as? Int)
|
||||
}
|
||||
}
|
||||
if let ap = try? AVAudioPlayer(contentsOf: fileURL) {
|
||||
add(key: "Duration", value: ap.duration.formatshort)
|
||||
add(key: "Bitrate", value: ap.settings[AVSampleRateKey] as? Int)
|
||||
}
|
||||
return (dic, keys)
|
||||
}
|
||||
@@ -366,7 +409,11 @@ public struct LocalFileInformationGenerator {
|
||||
}
|
||||
}
|
||||
let asset = AVURLAsset(url: fileURL, options: nil)
|
||||
#if swift(>=4.0)
|
||||
let videoTracks = asset.tracks(withMediaType: AVMediaType.video)
|
||||
#else
|
||||
let videoTracks = asset.tracks(withMediaType: AVMediaTypeVideo)
|
||||
#endif
|
||||
if let videoTrack = videoTracks.first {
|
||||
var bitrate: Float = 0
|
||||
let width = Int(videoTrack.naturalSize.width)
|
||||
@@ -380,7 +427,11 @@ public struct LocalFileInformationGenerator {
|
||||
add(key: "Duration", value: TimeInterval(duration).formatshort)
|
||||
add(key: "Video Bitrate", value: "\(Int(ceil(bitrate / 1000))) kbps")
|
||||
}
|
||||
#if swift(>=4.0)
|
||||
let audioTracks = asset.tracks(withMediaType: AVMediaType.audio)
|
||||
#else
|
||||
let audioTracks = asset.tracks(withMediaType: AVMediaTypeAudio)
|
||||
#endif
|
||||
// dic["Audio channels"] = audioTracks.count
|
||||
var bitrate: Float = 0
|
||||
for track in audioTracks {
|
||||
@@ -403,54 +454,64 @@ public struct LocalFileInformationGenerator {
|
||||
}
|
||||
|
||||
func getKey(_ key: String, from dict: CGPDFDictionaryRef) -> String? {
|
||||
var cfValue: CGPDFStringRef? = nil
|
||||
if (CGPDFDictionaryGetString(dict, key, &cfValue)), let value = CGPDFStringCopyTextString(cfValue!) {
|
||||
var cfStrValue: CGPDFStringRef?
|
||||
if (CGPDFDictionaryGetString(dict, key, &cfStrValue)), let value = cfStrValue.flatMap({ CGPDFStringCopyTextString($0) }) {
|
||||
return value as String
|
||||
}
|
||||
var cfArrayValue: CGPDFArrayRef?
|
||||
if (CGPDFDictionaryGetArray(dict, key, &cfArrayValue)), let cfArray = cfArrayValue {
|
||||
var array = [String]()
|
||||
for i in 0..<CGPDFArrayGetCount(cfArray) {
|
||||
var cfItemValue: CGPDFStringRef?
|
||||
if CGPDFArrayGetString(cfArray, i, &cfItemValue), let item = cfItemValue.flatMap({ CGPDFStringCopyTextString($0) }) {
|
||||
array.append(item as String)
|
||||
}
|
||||
}
|
||||
return array.joined(separator: ", ")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func convertDate(_ date: String?) -> Date? {
|
||||
guard let date = date else { return nil }
|
||||
var dateStr = date
|
||||
if dateStr.hasPrefix("D:") {
|
||||
dateStr.characters.removeFirst(2)
|
||||
}
|
||||
let dateStr = date.replacingOccurrences(of: "'", with: "").replacingOccurrences(of: "D:", with: "", options: .anchored)
|
||||
let dateFormatter = DateFormatter()
|
||||
dateFormatter.dateFormat = "yyyyMMddHHmmssTZD"
|
||||
if let result = dateFormatter.date(from: dateStr) {
|
||||
return result
|
||||
}
|
||||
dateFormatter.dateFormat = "yyyyMMddHHmmss"
|
||||
if let result = dateFormatter.date(from: dateStr) {
|
||||
return result
|
||||
let formats: [String] = ["yyyyMMddHHmmssTZ", "yyyyMMddHHmmssZZZZZ", "yyyyMMddHHmmssZ", "yyyyMMddHHmmss"]
|
||||
for format in formats {
|
||||
dateFormatter.dateFormat = format
|
||||
if let result = dateFormatter.date(from: dateStr) {
|
||||
return result
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
if let provider = CGDataProvider(url: fileURL as CFURL), let reference = CGPDFDocument(provider), let dict = reference.info {
|
||||
add(key: "Title", value: getKey("Title", from: dict))
|
||||
add(key: "Author", value: getKey("Author", from: dict))
|
||||
add(key: "Subject", value: getKey("Subject", from: dict))
|
||||
var majorVersion: Int32 = 0
|
||||
var minorVersion: Int32 = 0
|
||||
reference.getVersion(majorVersion: &majorVersion, minorVersion: &minorVersion)
|
||||
if majorVersion > 0 {
|
||||
add(key: "Version", value: String(majorVersion) + "." + String(minorVersion))
|
||||
}
|
||||
add(key: "Pages", value: reference.numberOfPages)
|
||||
|
||||
if reference.numberOfPages > 0, let pageRef = reference.page(at: 1) {
|
||||
let size = pageRef.getBoxRect(CGPDFBox.mediaBox).size
|
||||
add(key: "Resolution", value: "\(Int(size.width))x\(Int(size.height))")
|
||||
}
|
||||
add(key: "Content creator", value: getKey("Creator", from: dict))
|
||||
add(key: "Creation date", value: convertDate(getKey("CreationDate", from: dict)))
|
||||
add(key: "Modified date", value: convertDate(getKey("ModDate", from: dict)))
|
||||
add(key: "Security", value: reference.isEncrypted ? "Present" : "None")
|
||||
add(key: "Allows printing", value: reference.allowsPrinting ? "Yes" : "No")
|
||||
add(key: "Allows copying", value: reference.allowsCopying ? "Yes" : "No")
|
||||
guard let provider = CGDataProvider(url: fileURL as CFURL), let reference = CGPDFDocument(provider), let dict = reference.info else {
|
||||
return (dic, keys)
|
||||
}
|
||||
add(key: "Title", value: getKey("Title", from: dict))
|
||||
add(key: "Author", value: getKey("Author", from: dict))
|
||||
add(key: "Subject", value: getKey("Subject", from: dict))
|
||||
add(key: "Producer", value: getKey("Producer", from: dict))
|
||||
add(key: "Keywords", value: getKey("Keywords", from: dict))
|
||||
var majorVersion: Int32 = 0
|
||||
var minorVersion: Int32 = 0
|
||||
reference.getVersion(majorVersion: &majorVersion, minorVersion: &minorVersion)
|
||||
if majorVersion > 0 {
|
||||
add(key: "Version", value: String(majorVersion) + "." + String(minorVersion))
|
||||
}
|
||||
add(key: "Pages", value: reference.numberOfPages)
|
||||
|
||||
if reference.numberOfPages > 0, let pageRef = reference.page(at: 1) {
|
||||
let size = pageRef.getBoxRect(CGPDFBox.mediaBox).size
|
||||
add(key: "Resolution", value: "\(Int(size.width))x\(Int(size.height))")
|
||||
}
|
||||
add(key: "Content creator", value: getKey("Creator", from: dict))
|
||||
add(key: "Creation date", value: convertDate(getKey("CreationDate", from: dict)))
|
||||
add(key: "Modified date", value: convertDate(getKey("ModDate", from: dict)))
|
||||
add(key: "Security", value: reference.isEncrypted)
|
||||
add(key: "Allows printing", value: reference.allowsPrinting)
|
||||
add(key: "Allows copying", value: reference.allowsCopying)
|
||||
return (dic, keys)
|
||||
}
|
||||
|
||||
@@ -466,7 +527,4 @@ public struct LocalFileInformationGenerator {
|
||||
/// - Note: No default implementation is avaiable
|
||||
static public var customProperties: ((_ fileURL: URL) -> (prop: [String: Any], keys: [String]))? = nil
|
||||
}
|
||||
|
||||
fileprivate func ~=<T : Equatable>(array: [T], value: T) -> Bool {
|
||||
return array.contains(value)
|
||||
}
|
||||
#endif
|
||||
|
||||
+616
@@ -0,0 +1,616 @@
|
||||
//
|
||||
// FileProviderExtensions.swift
|
||||
// FileProvider
|
||||
//
|
||||
// Created by Amir Abbas on 12/27/1395 AP.
|
||||
//
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
public extension Array where Element: FileObject {
|
||||
/// Returns a sorted array of `FileObject`s by criterias set in attributes.
|
||||
public func sort(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]
|
||||
}
|
||||
|
||||
/// Sorts array of `FileObject`s by criterias set in attributes.
|
||||
public mutating func sorted(by type: FileObjectSorting.SortType, ascending: Bool = true, isDirectoriesFirst: Bool = false) {
|
||||
self = self.sort(by: type, ascending: ascending, isDirectoriesFirst: isDirectoriesFirst)
|
||||
}
|
||||
}
|
||||
|
||||
public extension Sequence where Iterator.Element == UInt8 {
|
||||
/// Converts a byte array into hexadecimal string representation
|
||||
func hexString() -> String {
|
||||
return self.map{String(format: "%02X", $0)}.joined()
|
||||
}
|
||||
}
|
||||
|
||||
public extension URLFileResourceType {
|
||||
/// **FileProvider** returns corresponding `URLFileResourceType` of a `FileAttributeType` value
|
||||
public init(fileTypeValue: FileAttributeType) {
|
||||
switch fileTypeValue {
|
||||
case FileAttributeType.typeCharacterSpecial: self = .characterSpecial
|
||||
case FileAttributeType.typeDirectory: self = .directory
|
||||
case FileAttributeType.typeBlockSpecial: self = .blockSpecial
|
||||
case FileAttributeType.typeRegular: self = .regular
|
||||
case FileAttributeType.typeSymbolicLink: self = .symbolicLink
|
||||
case FileAttributeType.typeSocket: self = .socket
|
||||
case FileAttributeType.typeUnknown: self = .unknown
|
||||
default: self = .unknown
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public extension URLResourceKey {
|
||||
/// **FileProvider** returns url of file object.
|
||||
public static let fileURLKey = URLResourceKey(rawValue: "NSURLFileURLKey")
|
||||
/// **FileProvider** returns modification date of file in server
|
||||
public static let serverDateKey = URLResourceKey(rawValue: "NSURLServerDateKey")
|
||||
/// **FileProvider** returns HTTP ETag string of remote resource
|
||||
public static let entryTagKey = URLResourceKey(rawValue: "NSURLEntryTagKey")
|
||||
/// **FileProvider** returns MIME type of file, if returned by server
|
||||
public static let mimeTypeKey = URLResourceKey(rawValue: "NSURLMIMETypeIdentifierKey")
|
||||
/// **FileProvider** returns either file is encrypted or not
|
||||
public static let isEncryptedKey = URLResourceKey(rawValue: "NSURLIsEncryptedKey")
|
||||
/// **FileProvider** count of items in directory
|
||||
public static let childrensCount = URLResourceKey(rawValue: "MFPURLChildrensCount")
|
||||
}
|
||||
|
||||
public extension ProgressUserInfoKey {
|
||||
/// **FileProvider** returns associated `FileProviderOperationType`
|
||||
public static let fileProvderOperationTypeKey = ProgressUserInfoKey("MFPOperationTypeKey")
|
||||
/// **FileProvider** returns start date/time of operation
|
||||
public static let startingTimeKey = ProgressUserInfoKey("NSProgressStartingTimeKey")
|
||||
}
|
||||
|
||||
internal extension URL {
|
||||
var uw_scheme: String {
|
||||
return self.scheme ?? ""
|
||||
}
|
||||
|
||||
var fileIsDirectory: Bool {
|
||||
return (try? self.resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory ?? false
|
||||
}
|
||||
|
||||
var fileSize: Int64 {
|
||||
return Int64((try? self.resourceValues(forKeys: [.fileSizeKey]))?.fileSize ?? -1)
|
||||
}
|
||||
|
||||
var fileExists: Bool {
|
||||
return (try? self.checkResourceIsReachable()) ?? false
|
||||
}
|
||||
|
||||
#if os(macOS) || os(iOS) || os(tvOS)
|
||||
#else
|
||||
func checkPromisedItemIsReachable() throws -> Bool {
|
||||
return false
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
public extension URLRequest {
|
||||
/// Defines HTTP Authentication method required to access
|
||||
public enum AuthenticationType {
|
||||
/// Basic method for authentication
|
||||
case basic
|
||||
/// Digest method for authentication
|
||||
case digest
|
||||
/// OAuth 1.0 method for authentication (OAuth)
|
||||
case oAuth1
|
||||
/// OAuth 2.0 method for authentication (Bearer)
|
||||
case oAuth2
|
||||
}
|
||||
}
|
||||
|
||||
/// Holds file MIME, and introduces selected type MIME as constants
|
||||
public struct ContentMIMEType: RawRepresentable, Hashable, Equatable {
|
||||
public var rawValue: String
|
||||
public typealias RawValue = String
|
||||
|
||||
public init(rawValue: String) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
public var hashValue: Int { return rawValue.hashValue }
|
||||
public static func == (lhs: ContentMIMEType, rhs: ContentMIMEType) -> Bool {
|
||||
return lhs.rawValue == rhs.rawValue
|
||||
}
|
||||
|
||||
/// Directory
|
||||
static public let directory = ContentMIMEType(rawValue: "httpd/unix-directory")
|
||||
|
||||
// Archive and Binary
|
||||
|
||||
/// Binary stream and unknown types
|
||||
static public let stream = ContentMIMEType(rawValue: "application/octet-stream")
|
||||
/// Protable document format
|
||||
static public let pdf = ContentMIMEType(rawValue: "application/pdf")
|
||||
/// Zip archive
|
||||
static public let zip = ContentMIMEType(rawValue: "application/zip")
|
||||
/// Rar archive
|
||||
static public let rarArchive = ContentMIMEType(rawValue: "application/x-rar-compressed")
|
||||
/// 7-zip archive
|
||||
static public let lzma = ContentMIMEType(rawValue: "application/x-7z-compressed")
|
||||
/// Adobe Flash
|
||||
static public let flash = ContentMIMEType(rawValue: "application/x-shockwave-flash")
|
||||
/// ePub book
|
||||
static public let epub = ContentMIMEType(rawValue: "application/epub+zip")
|
||||
/// Java archive (jar)
|
||||
static public let javaArchive = ContentMIMEType(rawValue: "application/java-archive")
|
||||
|
||||
// Texts
|
||||
|
||||
/// Text file
|
||||
static public let plainText = ContentMIMEType(rawValue: "text/plain")
|
||||
/// Coma-separated values
|
||||
static public let csv = ContentMIMEType(rawValue: "text/csv")
|
||||
/// Hyper-text markup language
|
||||
static public let html = ContentMIMEType(rawValue: "text/html")
|
||||
/// Common style sheet
|
||||
static public let css = ContentMIMEType(rawValue: "text/css")
|
||||
/// eXtended Markup language
|
||||
static public let xml = ContentMIMEType(rawValue: "text/xml")
|
||||
/// Javascript code file
|
||||
static public let javascript = ContentMIMEType(rawValue: "application/javascript")
|
||||
/// Javascript notation
|
||||
static public let json = ContentMIMEType(rawValue: "application/json")
|
||||
|
||||
// Documents
|
||||
|
||||
/// Rich text file (RTF)
|
||||
static public let richText = ContentMIMEType(rawValue: "application/rtf")
|
||||
/// Excel 2013 (OOXML) document
|
||||
static public let excel = ContentMIMEType(rawValue: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
||||
/// Powerpoint 2013 (OOXML) document
|
||||
static public let powerpoint = ContentMIMEType(rawValue: "application/vnd.openxmlformats-officedocument.presentationml.slideshow")
|
||||
/// Word 2013 (OOXML) document
|
||||
static public let word = ContentMIMEType(rawValue: "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
|
||||
|
||||
// Images
|
||||
|
||||
/// Bitmap
|
||||
static public let bmp = ContentMIMEType(rawValue: "image/bmp")
|
||||
/// Graphics Interchange Format photo
|
||||
static public let gif = ContentMIMEType(rawValue: "image/gif")
|
||||
/// JPEG photo
|
||||
static public let jpeg = ContentMIMEType(rawValue: "image/jpeg")
|
||||
/// Portable network graphics
|
||||
static public let png = ContentMIMEType(rawValue: "image/png")
|
||||
|
||||
// Audio & Video
|
||||
|
||||
/// MPEG Audio
|
||||
static public let mpegAudio = ContentMIMEType(rawValue: "audio/mpeg")
|
||||
/// MPEG Video
|
||||
static public let mpeg = ContentMIMEType(rawValue: "video/mpeg")
|
||||
/// MPEG4 Audio
|
||||
static public let mpeg4Audio = ContentMIMEType(rawValue: "audio/mp4")
|
||||
/// MPEG4 Video
|
||||
static public let mpeg4 = ContentMIMEType(rawValue: "video/mp4")
|
||||
/// OGG Audio
|
||||
static public let ogg = ContentMIMEType(rawValue: "audio/ogg")
|
||||
/// Advanced Audio Coding
|
||||
static public let aac = ContentMIMEType(rawValue: "audio/x-aac")
|
||||
/// Microsoft Audio Video Interleaved
|
||||
static public let avi = ContentMIMEType(rawValue: "video/x-msvideo")
|
||||
/// Microsoft Wave audio
|
||||
static public let wav = ContentMIMEType(rawValue: "audio/x-wav")
|
||||
/// Apple QuickTime format
|
||||
static public let quicktime = ContentMIMEType(rawValue: "video/quicktime")
|
||||
/// 3GPP
|
||||
static public let threegp = ContentMIMEType(rawValue: "video/3gpp")
|
||||
/// Adobe Flash video
|
||||
static public let flashVideo = ContentMIMEType(rawValue: "video/x-flv")
|
||||
/// Adobe Flash video
|
||||
static public let flv = ContentMIMEType.flashVideo
|
||||
|
||||
// Google Drive
|
||||
|
||||
/// Google Drive: Folder
|
||||
static public let googleFolder = ContentMIMEType(rawValue: "application/vnd.google-apps.folder")
|
||||
/// Google Drive: Document (word processor)
|
||||
static public let googleDocument = ContentMIMEType(rawValue: "application/vnd.google-apps.document")
|
||||
/// Google Drive: Sheets (spreadsheet)
|
||||
static public let googleSheets = ContentMIMEType(rawValue: "application/vnd.google-apps.spreadsheet")
|
||||
/// Google Drive: Slides (presentation)
|
||||
static public let googleSlides = ContentMIMEType(rawValue: "application/vnd.google-apps.presentation")
|
||||
/// Google Drive: Drawing (vector draw)
|
||||
static public let googleDrawing = ContentMIMEType(rawValue: "application/vnd.google-apps.drawing")
|
||||
/// Google Drive: Audio
|
||||
static public let googleAudio = ContentMIMEType(rawValue: "application/vnd.google-apps.audio")
|
||||
/// Google Drive: Video
|
||||
static public let googleVideo = ContentMIMEType(rawValue: "application/vnd.google-apps.video")
|
||||
}
|
||||
|
||||
internal extension URLRequest {
|
||||
mutating func setValue(authentication credential: URLCredential?, with type: AuthenticationType) {
|
||||
func base64(_ str: String) -> String {
|
||||
let plainData = str.data(using: .utf8)
|
||||
let base64String = plainData!.base64EncodedString(options: [])
|
||||
return base64String
|
||||
}
|
||||
|
||||
guard let credential = credential else { return }
|
||||
switch type {
|
||||
case .basic:
|
||||
let user = credential.user?.replacingOccurrences(of: ":", with: "") ?? ""
|
||||
let pass = credential.password ?? ""
|
||||
let authStr = "\(user):\(pass)"
|
||||
if let base64Auth = authStr.data(using: .utf8)?.base64EncodedString() {
|
||||
self.setValue("Basic \(base64Auth)", forHTTPHeaderField: "Authorization")
|
||||
}
|
||||
case .digest:
|
||||
// handled by RemoteSessionDelegate
|
||||
break
|
||||
case .oAuth1:
|
||||
if let oauth = credential.password {
|
||||
self.setValue("OAuth \(oauth)", forHTTPHeaderField: "Authorization")
|
||||
}
|
||||
case .oAuth2:
|
||||
if let bearer = credential.password {
|
||||
self.setValue("Bearer \(bearer)", forHTTPHeaderField: "Authorization")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mutating func setValue(acceptCharset: String.Encoding, quality: Double? = nil) {
|
||||
let cfEncoding = CFStringConvertNSStringEncodingToEncoding(acceptCharset.rawValue)
|
||||
if let charsetString = CFStringConvertEncodingToIANACharSetName(cfEncoding) as String? {
|
||||
if let qualityDesc = quality.flatMap({ String(format: "%.1f", min(0, max ($0, 1))) }) {
|
||||
self.setValue("\(charsetString);q=\(qualityDesc)", forHTTPHeaderField: "Accept-Charset")
|
||||
} else {
|
||||
self.setValue(charsetString, forHTTPHeaderField: "Accept-Charset")
|
||||
}
|
||||
}
|
||||
}
|
||||
mutating func addValue(acceptCharset: String.Encoding, quality: Double? = nil) {
|
||||
let cfEncoding = CFStringConvertNSStringEncodingToEncoding(acceptCharset.rawValue)
|
||||
if let charsetString = CFStringConvertEncodingToIANACharSetName(cfEncoding) as String? {
|
||||
if let qualityDesc = quality.flatMap({ String(format: "%.1f", min(0, max ($0, 1))) }) {
|
||||
self.addValue("\(charsetString);q=\(qualityDesc)", forHTTPHeaderField: "Accept-Charset")
|
||||
} else {
|
||||
self.addValue(charsetString, forHTTPHeaderField: "Accept-Charset")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum Encoding: String {
|
||||
case all = "*"
|
||||
case identity
|
||||
case gzip
|
||||
case deflate
|
||||
}
|
||||
|
||||
mutating func setValue(acceptEncoding: Encoding, quality: Double? = nil) {
|
||||
if let qualityDesc = quality.flatMap({ String(format: "%.1f", min(0, max ($0, 1))) }) {
|
||||
self.setValue("\(acceptEncoding.rawValue);q=\(qualityDesc)", forHTTPHeaderField: "Accept-Encoding")
|
||||
} else {
|
||||
self.setValue(acceptEncoding.rawValue, forHTTPHeaderField: "Accept-Encoding")
|
||||
}
|
||||
}
|
||||
|
||||
mutating func addValue(acceptEncoding: Encoding, quality: Double? = nil) {
|
||||
if let qualityDesc = quality.flatMap({ String(format: "%.1f", min(0, max ($0, 1))) }) {
|
||||
self.addValue("\(acceptEncoding.rawValue);q=\(qualityDesc)", forHTTPHeaderField: "Accept-Encoding")
|
||||
} else {
|
||||
self.addValue(acceptEncoding.rawValue, forHTTPHeaderField: "Accept-Encoding")
|
||||
}
|
||||
}
|
||||
|
||||
mutating func setValue(acceptLanguage: Locale, quality: Double? = nil) {
|
||||
let langCode = acceptLanguage.identifier.replacingOccurrences(of: "_", with: "-")
|
||||
if let qualityDesc = quality.flatMap({ String(format: "%.1f", min(0, max ($0, 1))) }) {
|
||||
self.setValue("\(langCode);q=\(qualityDesc)", forHTTPHeaderField: "Accept-Language")
|
||||
} else {
|
||||
self.setValue(langCode, forHTTPHeaderField: "Accept-Language")
|
||||
}
|
||||
}
|
||||
|
||||
mutating func addValue(acceptLanguage: Locale, quality: Double? = nil) {
|
||||
let langCode = acceptLanguage.identifier.replacingOccurrences(of: "_", with: "-")
|
||||
if let qualityDesc = quality.flatMap({ String(format: "%.1f", min(0, max ($0, 1))) }) {
|
||||
self.addValue("\(langCode);q=\(qualityDesc)", forHTTPHeaderField: "Accept-Language")
|
||||
} else {
|
||||
self.addValue(langCode, forHTTPHeaderField: "Accept-Language")
|
||||
}
|
||||
}
|
||||
|
||||
mutating func setValue(rangeWithOffset offset: Int64, length: Int) {
|
||||
if length > 0 {
|
||||
self.setValue("bytes=\(offset)-\(offset + Int64(length) - 1)", forHTTPHeaderField: "Range")
|
||||
} else if offset > 0 && length < 0 {
|
||||
self.setValue("bytes=\(offset)-", forHTTPHeaderField: "Range")
|
||||
}
|
||||
}
|
||||
|
||||
mutating func setValue(range: Range<Int>) {
|
||||
let range = max(0, range.lowerBound)..<range.upperBound
|
||||
if range.upperBound < Int.max && range.count > 0 {
|
||||
self.setValue("bytes=\(range.lowerBound)-\(range.upperBound - 1)", forHTTPHeaderField: "Range")
|
||||
} else if range.lowerBound > 0 {
|
||||
self.setValue("bytes=\(range.lowerBound)-", forHTTPHeaderField: "Range")
|
||||
}
|
||||
}
|
||||
|
||||
mutating func setValue(contentRange range: Range<Int64>, totalBytes: Int64) {
|
||||
let range = max(0, range.lowerBound)..<range.upperBound
|
||||
if range.upperBound < Int.max && range.count > 0 {
|
||||
self.setValue("bytes \(range.lowerBound)-\(range.upperBound - 1)/\(totalBytes)", forHTTPHeaderField: "Content-Range")
|
||||
} else if range.lowerBound > 0 {
|
||||
self.setValue("bytes \(range.lowerBound)-/\(totalBytes)", forHTTPHeaderField: "Content-Range")
|
||||
} else {
|
||||
self.setValue("bytes 0-/\(totalBytes)", forHTTPHeaderField: "Content-Range")
|
||||
}
|
||||
}
|
||||
|
||||
mutating func setValue(contentType: ContentMIMEType, charset: String.Encoding? = nil) {
|
||||
var parameter = ""
|
||||
if let charset = charset {
|
||||
let cfEncoding = CFStringConvertNSStringEncodingToEncoding(charset.rawValue)
|
||||
if let charsetString = CFStringConvertEncodingToIANACharSetName(cfEncoding) as String? {
|
||||
parameter = ";charset=" + charsetString
|
||||
}
|
||||
}
|
||||
|
||||
self.setValue(contentType.rawValue + parameter, forHTTPHeaderField: "Content-Type")
|
||||
}
|
||||
|
||||
mutating func setValue(dropboxArgKey requestDictionary: [String: AnyObject]) {
|
||||
guard let jsonData = try? JSONSerialization.data(withJSONObject: requestDictionary, options: []) else {
|
||||
return
|
||||
}
|
||||
guard var jsonString = String(data: jsonData, encoding: .utf8) else { return }
|
||||
jsonString = jsonString.asciiEscaped().replacingOccurrences(of: "\\/", with: "/")
|
||||
|
||||
self.setValue(jsonString, forHTTPHeaderField: "Dropbox-API-Arg")
|
||||
}
|
||||
}
|
||||
|
||||
internal extension CharacterSet {
|
||||
static let filePathAllowed = CharacterSet.urlPathAllowed.subtracting(CharacterSet(charactersIn: ":"))
|
||||
}
|
||||
|
||||
internal extension Data {
|
||||
internal var isPDF: Bool {
|
||||
return self.count > 4 && self.scanString(length: 4, using: .ascii) == "%PDF"
|
||||
}
|
||||
|
||||
init? (jsonDictionary dictionary: [String: AnyObject]) {
|
||||
guard JSONSerialization.isValidJSONObject(dictionary) else { return nil }
|
||||
guard let data = try? JSONSerialization.data(withJSONObject: dictionary, options: []) else {
|
||||
return nil
|
||||
}
|
||||
self = data
|
||||
}
|
||||
|
||||
func deserializeJSON() -> [String: AnyObject]? {
|
||||
return (try? JSONSerialization.jsonObject(with: self, options: [])) as? [String: AnyObject]
|
||||
}
|
||||
|
||||
init<T>(value: T) {
|
||||
var value = value
|
||||
self = Data(buffer: UnsafeBufferPointer(start: &value, count: 1))
|
||||
}
|
||||
|
||||
func scanValue<T>() -> T? {
|
||||
guard MemoryLayout<T>.size <= self.count else { return nil }
|
||||
return self.withUnsafeBytes { $0.pointee }
|
||||
}
|
||||
|
||||
func scanValue<T>(start: Int) -> T? {
|
||||
let length = MemoryLayout<T>.size
|
||||
guard self.count >= start + length else { return nil }
|
||||
return self.subdata(in: start..<start+length).withUnsafeBytes { $0.pointee }
|
||||
}
|
||||
|
||||
func scanString(start: Int = 0, length: Int, using encoding: String.Encoding = .utf8) -> String? {
|
||||
guard self.count >= start + length else { return nil }
|
||||
return String(data: self.subdata(in: start..<start+length), encoding: encoding)
|
||||
}
|
||||
}
|
||||
|
||||
internal extension String {
|
||||
init? (jsonDictionary: [String: AnyObject]) {
|
||||
guard let data = Data(jsonDictionary: jsonDictionary) else {
|
||||
return nil
|
||||
}
|
||||
self.init(data: data, encoding: .utf8)
|
||||
}
|
||||
|
||||
func deserializeJSON(using encoding: String.Encoding = .utf8) -> [String: AnyObject]? {
|
||||
guard let data = self.data(using: encoding) else {
|
||||
return nil
|
||||
}
|
||||
return data.deserializeJSON()
|
||||
}
|
||||
|
||||
func asciiEscaped() -> String {
|
||||
var res = ""
|
||||
for char in self.unicodeScalars {
|
||||
let substring = String(char)
|
||||
if substring.canBeConverted(to: .ascii) {
|
||||
res.append(substring)
|
||||
} else {
|
||||
res = res.appendingFormat("\\u%04x", char.value)
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
}
|
||||
|
||||
internal extension NSNumber {
|
||||
internal func format(precision: Int = 2, style: NumberFormatter.Style = .decimal) -> String {
|
||||
let formatter = NumberFormatter()
|
||||
formatter.maximumFractionDigits = precision
|
||||
formatter.numberStyle = style
|
||||
return formatter.string(from: self)!
|
||||
}
|
||||
}
|
||||
|
||||
internal extension TimeInterval {
|
||||
internal var formatshort: String {
|
||||
var result = "0:00"
|
||||
if self < TimeInterval(Int32.max) {
|
||||
result = ""
|
||||
var time = DateComponents()
|
||||
time.hour = Int(self / 3600)
|
||||
time.minute = Int((self.truncatingRemainder(dividingBy: 3600)) / 60)
|
||||
time.second = Int(self.truncatingRemainder(dividingBy: 60))
|
||||
let formatter = NumberFormatter()
|
||||
formatter.paddingCharacter = "0"
|
||||
formatter.minimumIntegerDigits = 2
|
||||
formatter.maximumFractionDigits = 0
|
||||
let formatterFirst = NumberFormatter()
|
||||
formatterFirst.maximumFractionDigits = 0
|
||||
if time.hour! > 0 {
|
||||
result = "\(formatterFirst.string(from: NSNumber(value: time.hour!))!):\(formatter.string(from: NSNumber(value: time.minute!))!):\(formatter.string(from: NSNumber(value: time.second!))!)"
|
||||
} else {
|
||||
result = "\(formatterFirst.string(from: NSNumber(value: time.minute!))!):\(formatter.string(from: NSNumber(value: time.second!))!)"
|
||||
}
|
||||
}
|
||||
result = result.trimmingCharacters(in: CharacterSet(charactersIn: ": "))
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
public extension Date {
|
||||
/// Date formats used commonly in internet messaging defined by various RFCs.
|
||||
public enum RFCStandards: String {
|
||||
/// Obsolete (2-digit year) date format defined by RFC 822 for http.
|
||||
case rfc822 = "EEE',' dd' 'MMM' 'yy HH':'mm':'ss z"
|
||||
/// Obsolete (2-digit year) date format defined by RFC 850 for usenet.
|
||||
case rfc850 = "EEEE',' dd'-'MMM'-'yy HH':'mm':'ss z"
|
||||
/// Date format defined by RFC 1123 for http.
|
||||
case rfc1123 = "EEE',' dd' 'MMM' 'yyyy HH':'mm':'ss z"
|
||||
/// Date format defined by RFC 3339, as a profile of ISO 8601.
|
||||
case rfc3339 = "yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZZZ"
|
||||
/// Date format defined RFC 3339 as rare case with milliseconds.
|
||||
case rfc3339Extended = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSSZZZZZ"
|
||||
/// Date string returned by asctime() function.
|
||||
case asctime = "EEE MMM d HH':'mm':'ss yyyy"
|
||||
|
||||
// Defining a http alias allows changing default time format if a new RFC becomes standard.
|
||||
/// Equivalent to and defined by RFC 1123.
|
||||
public static let http = RFCStandards.rfc1123
|
||||
/// Equivalent to and defined by RFC 850.
|
||||
public static let usenet = RFCStandards.rfc850
|
||||
|
||||
/* re. [RFC7231 section-7.1.1.1](https://tools.ietf.org/html/rfc7231#section-7.1.1.1)
|
||||
"HTTP servers and client MUST accept all three HTTP-date formats" which are IMF-fixdate,
|
||||
obsolete RFC 850 format and ANSI C's asctime() format.
|
||||
|
||||
ISO 8601 format is common in JSON and XML fields, defined by RFC 3339 as a timestamp format.
|
||||
Though not mandated, we check string against them to allow using Date(rfcString:) in
|
||||
wider and more general sitations.
|
||||
|
||||
We use RFC 822 instead of RFC 1123 to convert from string because NSDateFormatter can parse
|
||||
both 2-digit and 4-digit year correctly when `dateFormat` year is 2-digit.
|
||||
|
||||
These values are sorted by frequency.
|
||||
*/
|
||||
fileprivate static let parsingCases: [RFCStandards] = [.rfc822, .rfc850, .asctime, .rfc3339, .rfc3339Extended]
|
||||
}
|
||||
|
||||
private static let posixLocale = Locale(identifier: "en_US_POSIX")
|
||||
private static let utcTimezone = TimeZone(identifier: "UTC")
|
||||
|
||||
/// Checks date string against various RFC standards and returns `Date`.
|
||||
public init?(rfcString: String) {
|
||||
let dateFor: DateFormatter = DateFormatter()
|
||||
dateFor.locale = Date.posixLocale
|
||||
|
||||
for standard in RFCStandards.parsingCases {
|
||||
dateFor.dateFormat = standard.rawValue
|
||||
if let date = dateFor.date(from: rfcString) {
|
||||
self = date
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
/// Formats date according to RFCs standard.
|
||||
/// - Note: local and timezone paramters should be nil for `.http` standard
|
||||
internal func format(with standard: RFCStandards, locale: Locale? = nil, timeZone: TimeZone? = nil) -> String {
|
||||
let fm = DateFormatter()
|
||||
fm.dateFormat = standard.rawValue
|
||||
fm.timeZone = timeZone ?? Date.utcTimezone
|
||||
fm.locale = locale ?? Date.posixLocale
|
||||
return fm.string(from: self)
|
||||
}
|
||||
}
|
||||
|
||||
internal extension NSPredicate {
|
||||
func findValue(forKey key: String?, operator op: NSComparisonPredicate.Operator? = nil) -> Any? {
|
||||
let val = findAllValues(forKey: key).lazy.filter { (op == nil || $0.operator == op!) && !$0.not }
|
||||
return val.first?.value
|
||||
}
|
||||
|
||||
func findAllValues(forKey key: String?) -> [(value: Any, operator: NSComparisonPredicate.Operator, not: Bool)] {
|
||||
if let cQuery = self as? NSCompoundPredicate {
|
||||
let find = cQuery.subpredicates.flatMap { ($0 as! NSPredicate).findAllValues(forKey: key) }
|
||||
if cQuery.compoundPredicateType == .not {
|
||||
return find.map { return ($0.value, $0.operator, !$0.not) }
|
||||
}
|
||||
return find
|
||||
} else if let cQuery = self as? NSComparisonPredicate {
|
||||
if cQuery.leftExpression.expressionType == .keyPath, key == nil || cQuery.leftExpression.keyPath == key!, let const = cQuery.rightExpression.constantValue {
|
||||
return [(value: const, operator: cQuery.predicateOperatorType, false)]
|
||||
}
|
||||
if cQuery.rightExpression.expressionType == .keyPath, key == nil || cQuery.rightExpression.keyPath == key!, let const = cQuery.leftExpression.constantValue {
|
||||
return [(value: const, operator: cQuery.predicateOperatorType, false)]
|
||||
}
|
||||
return []
|
||||
} else {
|
||||
return []
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func ~=<T>(pattern: (T) -> Bool, value: T) -> Bool {
|
||||
return pattern(value)
|
||||
}
|
||||
|
||||
func hasPrefix(_ prefix: String) -> (_ value: String) -> Bool {
|
||||
return { (value: String) -> Bool in
|
||||
value.hasPrefix(prefix)
|
||||
}
|
||||
}
|
||||
|
||||
func hasSuffix(_ suffix: String) -> (_ value: String) -> Bool {
|
||||
return { (value: String) -> Bool in
|
||||
value.hasSuffix(suffix)
|
||||
}
|
||||
}
|
||||
|
||||
// Legacy Swift versions support
|
||||
|
||||
#if swift(>=4.0)
|
||||
#else
|
||||
extension String {
|
||||
var count: Int {
|
||||
return self.characters.count
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if swift(>=4.1)
|
||||
#else
|
||||
extension Array {
|
||||
func compactMap<ElementOfResult>(_ transform: (Element) throws -> ElementOfResult?) rethrows -> [ElementOfResult] {
|
||||
return try self.flatMap(transform)
|
||||
}
|
||||
}
|
||||
|
||||
extension ArraySlice {
|
||||
func compactMap<ElementOfResult>(_ transform: (Element) throws -> ElementOfResult?) rethrows -> [ElementOfResult] {
|
||||
return try self.flatMap(transform)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -0,0 +1,448 @@
|
||||
// Originally based on CryptoSwift by Marcin Krzyżanowski <marcin.krzyzanowski@gmail.com>
|
||||
// Copyright (C) 2014 Marcin Krzyżanowski <marcin.krzyzanowski@gmail.com>
|
||||
// This software is provided 'as-is', without any express or implied warranty.
|
||||
//
|
||||
// In no event will the authors be held liable for any damages arising from the use of this software.
|
||||
//
|
||||
// Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
||||
//
|
||||
// - The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation is required.
|
||||
// - Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
|
||||
// - This notice may not be removed or altered from any source or binary distribution.
|
||||
|
||||
import Foundation
|
||||
|
||||
protocol SHA2Variant {
|
||||
static var size: Int { get }
|
||||
static var h: [UInt64] { get }
|
||||
static var k: [UInt64] { get }
|
||||
|
||||
static func resultingArray<T>(_ hh:[T]) -> ArraySlice<T>
|
||||
static func calculate(_ message: [UInt8]) -> [UInt8]
|
||||
}
|
||||
|
||||
protocol SHA2Variant32: SHA2Variant { }
|
||||
protocol SHA2Variant64: SHA2Variant { }
|
||||
|
||||
extension SHA2Variant32 {
|
||||
static var size: Int {
|
||||
return 64
|
||||
}
|
||||
|
||||
static var k: [UInt64] {
|
||||
return [0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
||||
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
||||
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
||||
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
||||
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
||||
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
||||
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
||||
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2]
|
||||
}
|
||||
|
||||
// codebeat:disable[ABC]
|
||||
static func calculate(_ message: [UInt8]) -> [UInt8] {
|
||||
var tmpMessage = message
|
||||
|
||||
let len = Self.size
|
||||
|
||||
// Step 1. Append Padding Bits
|
||||
tmpMessage.append(0x80) // append one bit (UInt8 with one bit) to message
|
||||
|
||||
// append "0" bit until message length in bits ≡ 448 (mod 512)
|
||||
var msgLength = tmpMessage.count
|
||||
var counter = 0
|
||||
|
||||
while msgLength % len != (len - 8) {
|
||||
counter += 1
|
||||
msgLength += 1
|
||||
}
|
||||
|
||||
tmpMessage.append(contentsOf: [UInt8](repeating: 0, count: counter))
|
||||
|
||||
// hash values
|
||||
var hh: [UInt32] = Self.h.map { UInt32($0) }
|
||||
let k = Self.k
|
||||
|
||||
// append message length, in a 64-bit big-endian integer. So now the message length is a multiple of 512 bits.
|
||||
tmpMessage.append(contentsOf: arrayOfBytes(message.count * 8, length: 64 / 8))
|
||||
|
||||
// Process the message in successive 512-bit chunks:
|
||||
let chunkSizeBytes = 512 / 8 // 64
|
||||
for chunk in BytesSequence(chunkSize: chunkSizeBytes, data: tmpMessage) {
|
||||
// break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15, big-endian
|
||||
// Extend the sixteen 32-bit words into sixty-four 32-bit words:
|
||||
var M:[UInt32] = [UInt32](repeating: 0, count: k.count)
|
||||
for x in 0..<M.count {
|
||||
switch (x) {
|
||||
case 0...15:
|
||||
let start: Int = chunk.startIndex + (x * MemoryLayout.size(ofValue: M[x]))
|
||||
let end: Int = start + MemoryLayout.size(ofValue: M[x])
|
||||
let le = toUInt32Array(chunk[start..<end])[0]
|
||||
M[x] = le.bigEndian
|
||||
break
|
||||
default:
|
||||
let s0 = rotateRight(M[x-15], n: 7) ^ rotateRight(M[x-15], n: 18) ^ (M[x-15] >> 3) //FIXME: n
|
||||
let s1 = rotateRight(M[x-2], n: 17) ^ rotateRight(M[x-2], n: 19) ^ (M[x-2] >> 10)
|
||||
let s2 = M[x-16]
|
||||
let s3 = M[x-7]
|
||||
M[x] = s2 &+ s0 &+ s3 &+ s1
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
var A = hh[0], B = hh[1], C = hh[2], D = hh[3], E = hh[4], F = hh[5], G = hh[6], H = hh[7]
|
||||
|
||||
// Main loop
|
||||
for j in 0..<k.count {
|
||||
let s0 = rotateRight(A,n: 2) ^ rotateRight(A,n: 13) ^ rotateRight(A,n: 22)
|
||||
let maj = (A & B) ^ (A & C) ^ (B & C)
|
||||
let t2 = s0 &+ maj
|
||||
let s1 = rotateRight(E,n: 6) ^ rotateRight(E,n: 11) ^ rotateRight(E,n: 25)
|
||||
let ch = (E & F) ^ ((~E) & G)
|
||||
let t1 = H &+ s1 &+ ch &+ UInt32(k[j]) &+ M[j]
|
||||
|
||||
H = G; G = F; F = E; E = D &+ t1
|
||||
D = C; C = B; B = A; A = t1 &+ t2
|
||||
}
|
||||
|
||||
hh[0] = (hh[0] &+ A)
|
||||
hh[1] = (hh[1] &+ B)
|
||||
hh[2] = (hh[2] &+ C)
|
||||
hh[3] = (hh[3] &+ D)
|
||||
hh[4] = (hh[4] &+ E)
|
||||
hh[5] = (hh[5] &+ F)
|
||||
hh[6] = (hh[6] &+ G)
|
||||
hh[7] = (hh[7] &+ H)
|
||||
}
|
||||
|
||||
// Produce the final hash value (big-endian) as a 160 bit number:
|
||||
var result = [UInt8]()
|
||||
result.reserveCapacity(hh.count / 4)
|
||||
Self.resultingArray(hh).forEach {
|
||||
let item = $0.bigEndian
|
||||
#if swift(>=4.0)
|
||||
result.append(UInt8(truncatingIfNeeded: item))
|
||||
result.append(UInt8(truncatingIfNeeded: item >> 8))
|
||||
result.append(UInt8(truncatingIfNeeded: item >> 16))
|
||||
result.append(UInt8(truncatingIfNeeded: item >> 24))
|
||||
#else
|
||||
result.append(UInt8(item))
|
||||
result.append(UInt8((item >> 8) & 0xFF))
|
||||
result.append(UInt8((item >> 16) & 0xFF))
|
||||
result.append(UInt8((item >> 24) & 0xFF))
|
||||
#endif
|
||||
|
||||
}
|
||||
return result
|
||||
}
|
||||
// codebeat:enable[ABC]
|
||||
}
|
||||
|
||||
extension SHA2Variant64 {
|
||||
static var size: Int {
|
||||
return 128
|
||||
}
|
||||
|
||||
static var k: [UInt64] {
|
||||
return [0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc, 0x3956c25bf348b538,
|
||||
0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, 0xd807aa98a3030242, 0x12835b0145706fbe,
|
||||
0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2, 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235,
|
||||
0xc19bf174cf692694, 0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,
|
||||
0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5, 0x983e5152ee66dfab,
|
||||
0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4, 0xc6e00bf33da88fc2, 0xd5a79147930aa725,
|
||||
0x06ca6351e003826f, 0x142929670a0e6e70, 0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed,
|
||||
0x53380d139d95b3df, 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b,
|
||||
0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30, 0xd192e819d6ef5218,
|
||||
0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8, 0x19a4c116b8d2d0c8, 0x1e376c085141ab53,
|
||||
0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8, 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373,
|
||||
0x682e6ff3d6b2b8a3, 0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
|
||||
0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b, 0xca273eceea26619c,
|
||||
0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, 0x06f067aa72176fba, 0x0a637dc5a2c898a6,
|
||||
0x113f9804bef90dae, 0x1b710b35131c471b, 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc,
|
||||
0x431d67c49c100d4c, 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817]
|
||||
}
|
||||
|
||||
// codebeat:disable[ABC]
|
||||
static func calculate(_ message: [UInt8]) -> [UInt8] {
|
||||
var tmpMessage = message
|
||||
|
||||
let len = Self.size
|
||||
|
||||
// Step 1. Append Padding Bits
|
||||
tmpMessage.append(0x80) // append one bit (UInt8 with one bit) to message
|
||||
|
||||
// append "0" bit until message length in bits ≡ 448 (mod 512)
|
||||
var msgLength = tmpMessage.count
|
||||
var counter = 0
|
||||
|
||||
while msgLength % len != (len - 8) {
|
||||
counter += 1
|
||||
msgLength += 1
|
||||
}
|
||||
|
||||
tmpMessage += [UInt8](repeating: 0, count: counter)
|
||||
|
||||
// hash values
|
||||
var hh: [UInt64] = Self.h
|
||||
let k = Self.k
|
||||
|
||||
// append message length, in a 64-bit big-endian integer. So now the message length is a multiple of 512 bits.
|
||||
tmpMessage += arrayOfBytes(message.count * 8, length: 64 / 8)
|
||||
|
||||
// Process the message in successive 1024-bit chunks:
|
||||
let chunkSizeBytes = 1024 / 8 // 128
|
||||
for chunk in BytesSequence(chunkSize: chunkSizeBytes, data: tmpMessage) {
|
||||
// break chunk into sixteen 64-bit words M[j], 0 ≤ j ≤ 15, big-endian
|
||||
// Extend the sixteen 64-bit words into eighty 64-bit words:
|
||||
var M = [UInt64](repeating: 0, count: k.count)
|
||||
for x in 0..<M.count {
|
||||
switch (x) {
|
||||
case 0...15:
|
||||
let start = chunk.startIndex + (x * MemoryLayout.size(ofValue: M[x]))
|
||||
let end = start + MemoryLayout.size(ofValue: M[x])
|
||||
let le = toUInt64Array(chunk[start..<end])[0]
|
||||
M[x] = le.bigEndian
|
||||
break
|
||||
default:
|
||||
let s0 = rotateRight(M[x-15], n: 1) ^ rotateRight(M[x-15], n: 8) ^ (M[x-15] >> 7)
|
||||
let s1 = rotateRight(M[x-2], n: 19) ^ rotateRight(M[x-2], n: 61) ^ (M[x-2] >> 6)
|
||||
let s2 = M[x-16]
|
||||
let s3 = M[x-7]
|
||||
M[x] = s2 &+ s0 &+ s3 &+ s1
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
var A = hh[0], B = hh[1], C = hh[2], D = hh[3], E = hh[4], F = hh[5], G = hh[6], H = hh[7]
|
||||
|
||||
// Main loop
|
||||
for j in 0..<k.count {
|
||||
let s0 = rotateRight(A,n: 28) ^ rotateRight(A,n: 34) ^ rotateRight(A,n: 39) //FIXME: n:
|
||||
let maj = (A & B) ^ (A & C) ^ (B & C)
|
||||
let t2 = s0 &+ maj
|
||||
let s1 = rotateRight(E,n: 14) ^ rotateRight(E,n: 18) ^ rotateRight(E,n: 41)
|
||||
let ch = (E & F) ^ ((~E) & G)
|
||||
let t1 = H &+ s1 &+ ch &+ k[j] &+ UInt64(M[j])
|
||||
|
||||
H = G; G = F; F = E; E = D &+ t1
|
||||
D = C; C = B; B = A; A = t1 &+ t2
|
||||
}
|
||||
|
||||
hh[0] = (hh[0] &+ A)
|
||||
hh[1] = (hh[1] &+ B)
|
||||
hh[2] = (hh[2] &+ C)
|
||||
hh[3] = (hh[3] &+ D)
|
||||
hh[4] = (hh[4] &+ E)
|
||||
hh[5] = (hh[5] &+ F)
|
||||
hh[6] = (hh[6] &+ G)
|
||||
hh[7] = (hh[7] &+ H)
|
||||
}
|
||||
|
||||
// Produce the final hash value (big-endian)
|
||||
var result = [UInt8]()
|
||||
result.reserveCapacity(hh.count / 4)
|
||||
Self.resultingArray(hh).forEach {
|
||||
let item = $0.bigEndian
|
||||
var partialResult = [UInt8]()
|
||||
partialResult.reserveCapacity(8)
|
||||
for i in 0..<8 {
|
||||
let shift = UInt64(8 * i)
|
||||
partialResult.append(UInt8((item >> shift) & 0xff))
|
||||
}
|
||||
result += partialResult
|
||||
}
|
||||
return result
|
||||
}
|
||||
// codebeat:enable[ABC]
|
||||
}
|
||||
|
||||
final class SHA256 : SHA2Variant32 {
|
||||
static let h: [UInt64] = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]
|
||||
|
||||
static func resultingArray<T>(_ hh: [T]) -> ArraySlice<T> {
|
||||
return ArraySlice(hh)
|
||||
}
|
||||
}
|
||||
|
||||
final class SHA384 : SHA2Variant64 {
|
||||
static let h: [UInt64] = [0xcbbb9d5dc1059ed8, 0x629a292a367cd507, 0x9159015a3070dd17, 0x152fecd8f70e5939, 0x67332667ffc00b31, 0x8eb44a8768581511, 0xdb0c2e0d64f98fa7, 0x47b5481dbefa4fa4]
|
||||
|
||||
public static func resultingArray<T>(_ hh: [T]) -> ArraySlice<T> {
|
||||
return hh[0..<6]
|
||||
}
|
||||
}
|
||||
|
||||
final class SHA512 : SHA2Variant64 {
|
||||
static let h: [UInt64] = [0x6a09e667f3bcc908, 0xbb67ae8584caa73b, 0x3c6ef372fe94f82b, 0xa54ff53a5f1d36f1, 0x510e527fade682d1, 0x9b05688c2b3e6c1f, 0x1f83d9abfb41bd6b, 0x5be0cd19137e2179]
|
||||
|
||||
static func resultingArray<T>(_ hh: [T]) -> ArraySlice<T> {
|
||||
return ArraySlice(hh)
|
||||
}
|
||||
}
|
||||
|
||||
final class SHA2<Variant: SHA2Variant> {
|
||||
static var size: Int {
|
||||
return Variant.size
|
||||
}
|
||||
|
||||
static func calculate(_ message: [UInt8]) -> [UInt8] {
|
||||
return Variant.calculate(message)
|
||||
}
|
||||
}
|
||||
|
||||
final class HMAC<Variant: SHA2Variant> {
|
||||
public static func authenticate(message:[UInt8], withKey key: [UInt8]) -> [UInt8] {
|
||||
var key = key
|
||||
|
||||
if (key.count > Variant.size) {
|
||||
key = Variant.calculate(key)
|
||||
}
|
||||
|
||||
if (key.count < Variant.size) { // keys shorter than blocksize are zero-padded
|
||||
key = key + [UInt8](repeating: 0, count: Variant.size - key.count)
|
||||
}
|
||||
|
||||
var opad = [UInt8](repeating: 0x5c, count: Variant.size)
|
||||
for (idx, _) in key.enumerated() {
|
||||
opad[idx] = key[idx] ^ opad[idx]
|
||||
}
|
||||
var ipad = [UInt8](repeating: 0x36, count: Variant.size)
|
||||
for (idx, _) in key.enumerated() {
|
||||
ipad[idx] = key[idx] ^ ipad[idx]
|
||||
}
|
||||
|
||||
let ipadAndMessageHash = Variant.calculate(ipad + message)
|
||||
let finalHash = Variant.calculate(opad + ipadAndMessageHash);
|
||||
|
||||
return finalHash
|
||||
}
|
||||
|
||||
static func authenticate(message: String, withKey key: [UInt8]) -> [UInt8] {
|
||||
return authenticate(message: [UInt8](message.utf8), withKey: key)
|
||||
}
|
||||
|
||||
static func authenticate(message: Data, withKey key: Data) -> Data {
|
||||
return Data(bytes: authenticate(message: Array(message), withKey: Array(key)))
|
||||
}
|
||||
|
||||
static func authenticate(message: String, withKey key: Data) -> Data {
|
||||
return Data(bytes: authenticate(message: [UInt8](message.utf8), withKey: Array(key)))
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate struct BytesSequence: Sequence {
|
||||
let chunkSize: Int
|
||||
let data: [UInt8]
|
||||
|
||||
init(chunkSize: Int, data: [UInt8]) {
|
||||
self.chunkSize = chunkSize
|
||||
self.data = data
|
||||
}
|
||||
|
||||
func makeIterator() -> AnyIterator<ArraySlice<UInt8>> {
|
||||
var offset:Int = 0
|
||||
|
||||
return AnyIterator {
|
||||
let end = Swift.min(self.chunkSize, self.data.count - offset)
|
||||
let result = self.data[offset..<offset + end]
|
||||
offset += result.count
|
||||
return result.count > 0 ? result : nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate func rotateRight(_ x:UInt16, n:UInt16) -> UInt16 {
|
||||
return (x >> n) | (x << (16 - n))
|
||||
}
|
||||
|
||||
fileprivate func rotateRight(_ x:UInt32, n:UInt32) -> UInt32 {
|
||||
return (x >> n) | (x << (32 - n))
|
||||
}
|
||||
|
||||
fileprivate func rotateRight(_ x:UInt64, n:UInt64) -> UInt64 {
|
||||
return ((x >> n) | (x << (64 - n)))
|
||||
}
|
||||
|
||||
fileprivate func toUInt32Array(_ slice: ArraySlice<UInt8>) -> Array<UInt32> {
|
||||
var result = Array<UInt32>()
|
||||
result.reserveCapacity(16)
|
||||
|
||||
for idx in stride(from: slice.startIndex, to: slice.endIndex, by: MemoryLayout<UInt32>.size) {
|
||||
let val1:UInt32 = (UInt32(slice[idx.advanced(by: 3)]) << 24)
|
||||
let val2:UInt32 = (UInt32(slice[idx.advanced(by: 2)]) << 16)
|
||||
let val3:UInt32 = (UInt32(slice[idx.advanced(by: 1)]) << 8)
|
||||
let val4:UInt32 = UInt32(slice[idx])
|
||||
let val:UInt32 = val1 | val2 | val3 | val4
|
||||
result.append(val)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fileprivate func toUInt64Array(_ slice: ArraySlice<UInt8>) -> Array<UInt64> {
|
||||
var result = Array<UInt64>()
|
||||
result.reserveCapacity(32)
|
||||
for idx in stride(from: slice.startIndex, to: slice.endIndex, by: MemoryLayout<UInt64>.size) {
|
||||
var val:UInt64 = 0
|
||||
val |= UInt64(slice[idx.advanced(by: 7)]) << 56
|
||||
val |= UInt64(slice[idx.advanced(by: 6)]) << 48
|
||||
val |= UInt64(slice[idx.advanced(by: 5)]) << 40
|
||||
val |= UInt64(slice[idx.advanced(by: 4)]) << 32
|
||||
val |= UInt64(slice[idx.advanced(by: 3)]) << 24
|
||||
val |= UInt64(slice[idx.advanced(by: 2)]) << 16
|
||||
val |= UInt64(slice[idx.advanced(by: 1)]) << 8
|
||||
val |= UInt64(slice[idx.advanced(by: 0)]) << 0
|
||||
result.append(val)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fileprivate func arrayOfBytes<T>(_ value:T, length:Int? = nil) -> [UInt8] {
|
||||
let totalBytes = length ?? MemoryLayout<T>.size
|
||||
|
||||
let valuePointer = UnsafeMutablePointer<T>.allocate(capacity: 1)
|
||||
|
||||
valuePointer.pointee = value
|
||||
|
||||
let bytesPointer = UnsafeMutableRawPointer(valuePointer).assumingMemoryBound(to: UInt8.self)
|
||||
var bytes = [UInt8](repeating: 0, count: totalBytes)
|
||||
for j in 0..<min(MemoryLayout<T>.size,totalBytes) {
|
||||
bytes[totalBytes - 1 - j] = (bytesPointer + j).pointee
|
||||
}
|
||||
|
||||
valuePointer.deinitialize(count: 1)
|
||||
#if swift(>=4.1)
|
||||
valuePointer.deallocate()
|
||||
#else
|
||||
valuePointer.deallocate(capacity: 1)
|
||||
#endif
|
||||
return bytes
|
||||
}
|
||||
|
||||
public extension String {
|
||||
public func fp_sha256() -> [UInt8] {
|
||||
return SHA2<SHA256>.calculate([UInt8](self.utf8))
|
||||
}
|
||||
|
||||
public func fp_sha384() -> [UInt8] {
|
||||
return SHA2<SHA384>.calculate([UInt8](self.utf8))
|
||||
}
|
||||
|
||||
public func fp_sha512() -> [UInt8] {
|
||||
return SHA2<SHA512>.calculate([UInt8](self.utf8))
|
||||
}
|
||||
}
|
||||
|
||||
public extension Data {
|
||||
public func fp_sha256() -> [UInt8] {
|
||||
return SHA2<SHA256>.calculate(Array(self))
|
||||
}
|
||||
|
||||
public func fp_sha384() -> [UInt8] {
|
||||
return SHA2<SHA384>.calculate(Array(self))
|
||||
}
|
||||
|
||||
public func fp_sha512() -> [UInt8] {
|
||||
return SHA2<SHA512>.calculate(Array(self))
|
||||
}
|
||||
}
|
||||
+317
-73
@@ -8,14 +8,67 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
private var lasttaskIdAssociated = 1_000_000_000
|
||||
|
||||
private var _lasttaskIdAssociated = 1_000_000_000
|
||||
let _lasttaskIdAssociated_lock: NSLock = NSLock()
|
||||
var lasttaskIdAssociated: Int {
|
||||
get {
|
||||
_lasttaskIdAssociated_lock.try()
|
||||
defer {
|
||||
_lasttaskIdAssociated_lock.unlock()
|
||||
}
|
||||
return _lasttaskIdAssociated
|
||||
}
|
||||
set {
|
||||
_lasttaskIdAssociated_lock.try()
|
||||
defer {
|
||||
_lasttaskIdAssociated_lock.unlock()
|
||||
}
|
||||
_lasttaskIdAssociated = newValue
|
||||
}
|
||||
}
|
||||
|
||||
// codebeat:disable[TOTAL_LOC,TOO_MANY_IVARS]
|
||||
/// This class is a replica of NSURLSessionStreamTask with same api for iOS 7/8
|
||||
/// while it can actually fallback to NSURLSessionStreamTask in iOS 9.
|
||||
public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
fileprivate var inputStream: InputStream?
|
||||
fileprivate var outputStream: OutputStream?
|
||||
fileprivate var _inputStream: InputStream?
|
||||
fileprivate var _outputStream: OutputStream?
|
||||
|
||||
let _inputStream_lock: NSLock = NSLock()
|
||||
var inputStream: InputStream? {
|
||||
get {
|
||||
_inputStream_lock.try()
|
||||
defer {
|
||||
_inputStream_lock.unlock()
|
||||
}
|
||||
return _inputStream
|
||||
}
|
||||
set {
|
||||
_inputStream_lock.try()
|
||||
defer {
|
||||
_inputStream_lock.unlock()
|
||||
}
|
||||
_inputStream = newValue
|
||||
}
|
||||
}
|
||||
|
||||
let _outputStream_lock: NSLock = NSLock()
|
||||
var outputStream: OutputStream? {
|
||||
get {
|
||||
_outputStream_lock.try()
|
||||
defer {
|
||||
_outputStream_lock.unlock()
|
||||
}
|
||||
return _outputStream
|
||||
}
|
||||
set {
|
||||
_outputStream_lock.try()
|
||||
defer {
|
||||
_outputStream_lock.unlock()
|
||||
}
|
||||
_outputStream = newValue
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate var operation_queue: OperationQueue!
|
||||
internal var _underlyingSession: URLSession
|
||||
@@ -23,13 +76,14 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
return (_underlyingSession.delegate as? FPSStreamDelegate)
|
||||
}
|
||||
fileprivate var _taskIdentifier: Int
|
||||
fileprivate var _taskDescription: String?
|
||||
|
||||
/// Force using `URLSessionStreamTask` for iOS 9 and later
|
||||
public var useURLSession = true
|
||||
@available(iOS 9.0, OSX 10.11, *)
|
||||
public let useURLSession: Bool
|
||||
@available(iOS 9.0, macOS 10.11, *)
|
||||
fileprivate static var streamTasks = [Int: URLSessionStreamTask]()
|
||||
|
||||
@available(iOS 9.0, OSX 10.11, *)
|
||||
@available(iOS 9.0, macOS 10.11, *)
|
||||
internal var _underlyingTask: URLSessionStreamTask? {
|
||||
return FileProviderStreamTask.streamTasks[_taskIdentifier]
|
||||
}
|
||||
@@ -41,7 +95,7 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
* tasks in other sessions may have the same `taskIdentifier` value.
|
||||
*/
|
||||
open override var taskIdentifier: Int {
|
||||
if #available(iOS 9.0, OSX 10.11, *) {
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
if self.useURLSession {
|
||||
return _underlyingTask!.taskIdentifier
|
||||
}
|
||||
@@ -50,13 +104,40 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
return _taskIdentifier
|
||||
}
|
||||
|
||||
/// An app-provided description of the current task.
|
||||
///
|
||||
/// This value may be nil. It is intended to contain human-readable strings that you can
|
||||
/// then display to the user as part of your app’s user interface.
|
||||
open override var taskDescription: String? {
|
||||
get {
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
if self.useURLSession {
|
||||
return _underlyingTask!.taskDescription
|
||||
}
|
||||
}
|
||||
|
||||
return _taskDescription
|
||||
}
|
||||
@objc(setTaskDescription:)
|
||||
set {
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
if self.useURLSession {
|
||||
_underlyingTask!.taskDescription = newValue
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
_taskDescription = newValue
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate var _state: URLSessionTask.State = .suspended
|
||||
/**
|
||||
* The current state of the task—active, suspended, in the process
|
||||
* of being canceled, or completed.
|
||||
*/
|
||||
override open var state: URLSessionTask.State {
|
||||
if #available(iOS 9.0, OSX 10.11, *) {
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
if self.useURLSession {
|
||||
return _underlyingTask!.state
|
||||
}
|
||||
@@ -71,7 +152,7 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
* except when the server has responded to the initial request with a redirect to a different URL.
|
||||
*/
|
||||
override open var originalRequest: URLRequest? {
|
||||
if #available(iOS 9.0, OSX 10.11, *) {
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
if self.useURLSession {
|
||||
return _underlyingTask!.originalRequest
|
||||
}
|
||||
@@ -86,15 +167,38 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
* except when the server has responded to the initial request with a redirect to a different URL.
|
||||
*/
|
||||
override open var currentRequest: URLRequest? {
|
||||
if #available(iOS 9.0, OSX 10.11, *) {
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
return _underlyingTask!.currentRequest
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate var _countOfBytesSent: Int64 = 0
|
||||
fileprivate var _countOfBytesRecieved: Int64 = 0
|
||||
fileprivate var _countOfBytesSent: Int64 = 0 {
|
||||
willSet {
|
||||
for observer in observers where observer.keyPath == "countOfBytesSent" {
|
||||
observer.observer.observeValue(forKeyPath: observer.keyPath, of: self, change: [.oldKey: _countOfBytesSent, .oldKey: newValue], context: observer.context)
|
||||
}
|
||||
}
|
||||
didSet {
|
||||
for observer in observers where observer.keyPath == "countOfBytesSent" {
|
||||
observer.observer.observeValue(forKeyPath: observer.keyPath, of: self, change: [.oldKey: oldValue, .oldKey: _countOfBytesSent], context: observer.context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate var _countOfBytesRecieved: Int64 = 0 {
|
||||
willSet {
|
||||
for observer in observers where observer.keyPath == "countOfBytesRecieved" {
|
||||
observer.observer.observeValue(forKeyPath: observer.keyPath, of: self, change: [.oldKey: _countOfBytesRecieved, .oldKey: newValue], context: observer.context)
|
||||
}
|
||||
}
|
||||
didSet {
|
||||
for observer in observers where observer.keyPath == "countOfBytesRecieved" {
|
||||
observer.observer.observeValue(forKeyPath: observer.keyPath, of: self, change: [.oldKey: oldValue, .oldKey: _countOfBytesRecieved], context: observer.context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The number of bytes that the task has sent to the server in the request body.
|
||||
@@ -105,7 +209,7 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
* `urlSession(_:task:didSendBodyData:totalBytesSent:totalBytesExpectedToSend:)` delegate method.
|
||||
*/
|
||||
override open var countOfBytesSent: Int64 {
|
||||
if #available(iOS 9.0, OSX 10.11, *) {
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
if self.useURLSession {
|
||||
return _underlyingTask!.countOfBytesSent
|
||||
}
|
||||
@@ -121,7 +225,7 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
* or the `urlSession(_:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:)` method (for download tasks).
|
||||
*/
|
||||
override open var countOfBytesReceived: Int64 {
|
||||
if #available(iOS 9.0, OSX 10.11, *) {
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
if self.useURLSession {
|
||||
return _underlyingTask!.countOfBytesReceived
|
||||
}
|
||||
@@ -141,7 +245,7 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
* Otherwise, the value is `NSURLSessionTransferSizeUnknown` (`-1`) if you provided a stream or body data object, or zero (`0`) if you did not.
|
||||
*/
|
||||
override open var countOfBytesExpectedToSend: Int64 {
|
||||
if #available(iOS 9.0, OSX 10.11, *) {
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
return _underlyingTask!.countOfBytesExpectedToSend
|
||||
} else {
|
||||
return Int64(dataToBeSent.count)
|
||||
@@ -155,7 +259,7 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
* If that header is absent, the value is `NSURLSessionTransferSizeUnknown`.
|
||||
*/
|
||||
override open var countOfBytesExpectedToReceive: Int64 {
|
||||
if #available(iOS 9.0, OSX 10.11, *) {
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
if self.useURLSession {
|
||||
return _underlyingTask!.countOfBytesExpectedToReceive
|
||||
}
|
||||
@@ -164,6 +268,49 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
return Int64(dataReceived.count)
|
||||
}
|
||||
|
||||
var observers: [(keyPath: String, observer: NSObject, context: UnsafeMutableRawPointer?)] = []
|
||||
|
||||
public override func addObserver(_ observer: NSObject, forKeyPath keyPath: String, options: NSKeyValueObservingOptions = [], context: UnsafeMutableRawPointer?) {
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
if self.useURLSession {
|
||||
self._underlyingTask?.addObserver(observer, forKeyPath: keyPath, options: options, context: context)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
switch keyPath {
|
||||
case #keyPath(countOfBytesSent):
|
||||
fallthrough
|
||||
case #keyPath(countOfBytesReceived):
|
||||
fallthrough
|
||||
case #keyPath(countOfBytesExpectedToSend):
|
||||
fallthrough
|
||||
case #keyPath(countOfBytesExpectedToReceive):
|
||||
observers.append((keyPath: keyPath, observer: observer, context: context))
|
||||
default:
|
||||
break
|
||||
}
|
||||
super.addObserver(observer, forKeyPath: keyPath, options: options, context: context)
|
||||
}
|
||||
|
||||
public override func removeObserver(_ observer: NSObject, forKeyPath keyPath: String) {
|
||||
var newObservers: [(keyPath: String, observer: NSObject, context: UnsafeMutableRawPointer?)] = []
|
||||
for observer in observers where observer.keyPath != keyPath {
|
||||
newObservers.append(observer)
|
||||
}
|
||||
self.observers = newObservers
|
||||
super.removeObserver(observer, forKeyPath: keyPath)
|
||||
}
|
||||
|
||||
public override func removeObserver(_ observer: NSObject, forKeyPath keyPath: String, context: UnsafeMutableRawPointer?) {
|
||||
var newObservers: [(keyPath: String, observer: NSObject, context: UnsafeMutableRawPointer?)] = []
|
||||
for observer in observers where observer.keyPath != keyPath || observer.context != context {
|
||||
newObservers.append(observer)
|
||||
}
|
||||
self.observers = newObservers
|
||||
super.removeObserver(observer, forKeyPath: keyPath, context: context)
|
||||
}
|
||||
|
||||
override public init() {
|
||||
fatalError("Use NSURLSession.fpstreamTask() method")
|
||||
}
|
||||
@@ -171,10 +318,13 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
fileprivate var host: (hostname: String, port: Int)?
|
||||
fileprivate var service: NetService?
|
||||
|
||||
internal init(session: URLSession, host: String, port: Int) {
|
||||
internal static let defaultUseURLSession = false
|
||||
|
||||
internal init(session: URLSession, host: String, port: Int, useURLSession: Bool = defaultUseURLSession) {
|
||||
self._underlyingSession = session
|
||||
if #available(iOS 9.0, OSX 10.11, *) {
|
||||
if self.useURLSession {
|
||||
self.useURLSession = useURLSession
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
if useURLSession {
|
||||
let task = session.streamTask(withHostName: host, port: port)
|
||||
self._taskIdentifier = task.taskIdentifier
|
||||
FileProviderStreamTask.streamTasks[_taskIdentifier] = task
|
||||
@@ -190,10 +340,11 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
self.operation_queue.maxConcurrentOperationCount = 1
|
||||
}
|
||||
|
||||
internal init(session: URLSession, netService: NetService) {
|
||||
internal init(session: URLSession, netService: NetService, useURLSession: Bool = defaultUseURLSession) {
|
||||
self._underlyingSession = session
|
||||
if #available(iOS 9.0, OSX 10.11, *) {
|
||||
if self.useURLSession {
|
||||
self.useURLSession = useURLSession
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
if useURLSession {
|
||||
let task = session.streamTask(with: netService)
|
||||
self._taskIdentifier = task.taskIdentifier
|
||||
FileProviderStreamTask.streamTasks[_taskIdentifier] = task
|
||||
@@ -209,6 +360,12 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
self.operation_queue.maxConcurrentOperationCount = 1
|
||||
}
|
||||
|
||||
deinit {
|
||||
if !self.useURLSession {
|
||||
self.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancels the task.
|
||||
*
|
||||
@@ -220,16 +377,18 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
* This method may be called on a task that is suspended.
|
||||
*/
|
||||
override open func cancel() {
|
||||
if #available(iOS 9.0, OSX 10.11, *) {
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
if self.useURLSession {
|
||||
_underlyingTask!.cancel()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
self.operation_queue.isSuspended = true
|
||||
self._state = .canceling
|
||||
inputStream?.setValue(kCFBooleanTrue, forKey: kCFStreamPropertyShouldCloseNativeSocket as String)
|
||||
outputStream?.setValue(kCFBooleanTrue, forKey: kCFStreamPropertyShouldCloseNativeSocket as String)
|
||||
|
||||
self.inputStream?.delegate = nil
|
||||
self.outputStream?.delegate = nil
|
||||
|
||||
self.inputStream?.close()
|
||||
self.outputStream?.close()
|
||||
@@ -237,9 +396,6 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
self.inputStream?.remove(from: RunLoop.main, forMode: .defaultRunLoopMode)
|
||||
self.outputStream?.remove(from: RunLoop.main, forMode: .defaultRunLoopMode)
|
||||
|
||||
self.inputStream?.delegate = nil
|
||||
self.outputStream?.delegate = nil
|
||||
|
||||
self.inputStream = nil
|
||||
self.outputStream = nil
|
||||
|
||||
@@ -248,7 +404,25 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
self._countOfBytesRecieved = 0
|
||||
}
|
||||
|
||||
var _error: Error? = nil
|
||||
var __error: Error? = nil
|
||||
|
||||
let _error_lock: NSLock = NSLock()
|
||||
var _error: Error? {
|
||||
get {
|
||||
_error_lock.try()
|
||||
defer {
|
||||
_error_lock.unlock()
|
||||
}
|
||||
return __error
|
||||
}
|
||||
set {
|
||||
_error_lock.try()
|
||||
defer {
|
||||
_error_lock.unlock()
|
||||
}
|
||||
__error = newValue
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An error object that indicates why the task failed.
|
||||
@@ -256,7 +430,7 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
* This value is `NULL` if the task is still active or if the transfer completed successfully.
|
||||
*/
|
||||
override open var error: Error? {
|
||||
if #available(iOS 9.0, OSX 10.11, *) {
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
if useURLSession {
|
||||
return _underlyingTask!.error
|
||||
}
|
||||
@@ -273,7 +447,7 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
* All other tasks must start over when resumed.
|
||||
*/
|
||||
override open func suspend() {
|
||||
if #available(iOS 9.0, OSX 10.11, *) {
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
if self.useURLSession {
|
||||
_underlyingTask!.suspend()
|
||||
return
|
||||
@@ -286,7 +460,7 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
|
||||
// Resumes the task, if it is suspended.
|
||||
override open func resume() {
|
||||
if #available(iOS 9.0, OSX 10.11, *) {
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
if self.useURLSession {
|
||||
_underlyingTask!.resume()
|
||||
return
|
||||
@@ -296,43 +470,74 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
var readStream : Unmanaged<CFReadStream>?
|
||||
var writeStream : Unmanaged<CFWriteStream>?
|
||||
|
||||
if inputStream == nil || outputStream == nil {
|
||||
if let host = host {
|
||||
if self.inputStream == nil || self.outputStream == nil {
|
||||
if let host = self.host {
|
||||
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, host.hostname as CFString, UInt32(host.port), &readStream, &writeStream)
|
||||
} else if let service = service {
|
||||
} else if let service = self.service {
|
||||
let cfnetService = CFNetServiceCreate(kCFAllocatorDefault, service.domain as CFString, service.type as CFString, service.name as CFString, Int32(service.port))
|
||||
CFStreamCreatePairWithSocketToNetService(kCFAllocatorDefault, cfnetService.takeRetainedValue(), &readStream, &writeStream)
|
||||
}
|
||||
|
||||
inputStream = readStream?.takeRetainedValue()
|
||||
outputStream = writeStream?.takeRetainedValue()
|
||||
guard let inputStream = inputStream, let outputStream = outputStream else {
|
||||
self.inputStream = readStream?.takeRetainedValue()
|
||||
self.outputStream = writeStream?.takeRetainedValue()
|
||||
guard let inputStream = self.inputStream, let outputStream = self.outputStream else {
|
||||
return
|
||||
}
|
||||
streamDelegate?.urlSession?(self._underlyingSession, streamTask: self, didBecome: inputStream, outputStream: outputStream)
|
||||
self.streamDelegate?.urlSession?(self._underlyingSession, streamTask: self, didBecome: inputStream, outputStream: outputStream)
|
||||
}
|
||||
|
||||
guard let inputStream = inputStream, let outputStream = outputStream else {
|
||||
guard let inputStream = self.inputStream, let outputStream = self.outputStream else {
|
||||
return
|
||||
}
|
||||
|
||||
if self.isSecure {
|
||||
inputStream.setProperty(self.securityLevel.rawValue, forKey: .socketSecurityLevelKey)
|
||||
outputStream.setProperty(self.securityLevel.rawValue, forKey: .socketSecurityLevelKey)
|
||||
} else {
|
||||
inputStream.setProperty(StreamSocketSecurityLevel.none.rawValue, forKey: .socketSecurityLevelKey)
|
||||
outputStream.setProperty(StreamSocketSecurityLevel.none.rawValue, forKey: .socketSecurityLevelKey)
|
||||
}
|
||||
|
||||
inputStream.delegate = self
|
||||
outputStream.delegate = self
|
||||
|
||||
operation_queue.addOperation {
|
||||
inputStream.schedule(in: RunLoop.main, forMode: .defaultRunLoopMode)
|
||||
outputStream.schedule(in: RunLoop.main, forMode: .defaultRunLoopMode)
|
||||
}
|
||||
inputStream.schedule(in: RunLoop.main, forMode: .defaultRunLoopMode)
|
||||
outputStream.schedule(in: RunLoop.main, forMode: .defaultRunLoopMode)
|
||||
|
||||
inputStream.open()
|
||||
outputStream.open()
|
||||
|
||||
operation_queue.isSuspended = false
|
||||
_state = .running
|
||||
self.operation_queue.isSuspended = false
|
||||
self._state = .running
|
||||
}
|
||||
|
||||
fileprivate var dataToBeSent: Data = Data()
|
||||
fileprivate var dataReceived: Data = Data()
|
||||
fileprivate var _dataReceived: Data = Data()
|
||||
|
||||
// We are updating `dataReceived` from main thread while reading it from operation_queue.
|
||||
let _dataReceived_lock: NSLock = NSLock()
|
||||
|
||||
var dataReceived: Data {
|
||||
get {
|
||||
if !_dataReceived_lock.try() {
|
||||
print("not locked")
|
||||
}
|
||||
|
||||
defer {
|
||||
_dataReceived_lock.unlock()
|
||||
}
|
||||
return _dataReceived
|
||||
}
|
||||
set {
|
||||
if !_dataReceived_lock.try() {
|
||||
print("not locked")
|
||||
}
|
||||
defer {
|
||||
_dataReceived_lock.unlock()
|
||||
}
|
||||
_dataReceived = newValue
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Asynchronously reads a number of bytes from the stream, and calls a handler upon completion.
|
||||
@@ -343,12 +548,12 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
* the read is canceled and the completionHandler is called with an error. Pass `0` to prevent a read from timing out.
|
||||
* - Parameter completionHandler: The completion handler to call when all bytes are read, or an error occurs.
|
||||
* This handler is executed on the delegate queue. This completion handler takes the following parameters:
|
||||
* - data: The data read from the stream.
|
||||
* - atEOF: Whether or not the stream reached end-of-file (`EOF`), such that no more data can be read.
|
||||
* - error: An error object that indicates why the read failed, or `nil` if the read was successful.
|
||||
* - Parameter data: The data read from the stream.
|
||||
* - Parameter atEOF: Whether or not the stream reached end-of-file (`EOF`), such that no more data can be read.
|
||||
* - Parameter error: An error object that indicates why the read failed, or `nil` if the read was successful.
|
||||
*/
|
||||
open func readData(ofMinLength minBytes: Int, maxLength maxBytes: Int, timeout: TimeInterval, completionHandler: @escaping (_ data: Data?, _ atEOF: Bool, _ error :Error?) -> Void) {
|
||||
if #available(iOS 9.0, OSX 10.11, *) {
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
if self.useURLSession {
|
||||
_underlyingTask!.readData(ofMinLength: minBytes, maxLength: maxBytes, timeout: timeout, completionHandler: completionHandler)
|
||||
return
|
||||
@@ -363,6 +568,11 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
operation_queue.addOperation {
|
||||
var timedOut: Bool = false
|
||||
while (self.dataReceived.count == 0 || self.dataReceived.count < minBytes) && !timedOut {
|
||||
if let error = inputStream.streamError {
|
||||
completionHandler(nil, inputStream.streamStatus == .atEnd, error)
|
||||
return
|
||||
}
|
||||
|
||||
Thread.sleep(forTimeInterval: 0.1)
|
||||
timedOut = expireDate < Date()
|
||||
}
|
||||
@@ -370,11 +580,11 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
if self.dataReceived.count > maxBytes {
|
||||
let range: Range = 0..<maxBytes
|
||||
dR = self.dataReceived.subdata(in: range)
|
||||
self.dataReceived.replaceSubrange(range, with: Data())
|
||||
self.dataReceived.removeFirst(maxBytes)
|
||||
} else {
|
||||
if self.dataReceived.count > 0 {
|
||||
dR = self.dataReceived
|
||||
self.dataReceived.count = 0
|
||||
self.dataReceived.removeAll(keepingCapacity: false)
|
||||
}
|
||||
}
|
||||
let isEOF = inputStream.streamStatus == .atEnd && self.dataReceived.count == 0
|
||||
@@ -395,10 +605,10 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
* - Parameter completionHandler: The completion handler to call when all bytes are written, or an error occurs.
|
||||
* This handler is executed on the delegate queue.
|
||||
* This completion handler takes the following parameter:
|
||||
* - error: An error object that indicates why the write failed, or nil if the write was successful.
|
||||
* - Parameter error: An error object that indicates why the write failed, or `nil` if the write was successful.
|
||||
*/
|
||||
open func write(_ data: Data, timeout: TimeInterval, completionHandler: @escaping (_ error: Error?) -> Void) {
|
||||
if #available(iOS 9.0, OSX 10.11, *) {
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
if self.useURLSession {
|
||||
_underlyingTask!.write(data, timeout: timeout, completionHandler: completionHandler)
|
||||
return
|
||||
@@ -409,12 +619,11 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
operation_queue.addOperation {
|
||||
self.dataToBeSent.append(data)
|
||||
let result = self.write(timeout: timeout, close: false)
|
||||
if result < 0 {
|
||||
let error = self.outputStream?.streamError ?? NSError(domain: URLError.errorDomain, code: URLError.cannotWriteToFile.rawValue, userInfo: nil)
|
||||
let error = self.outputStream?.streamError ?? URLError(.cannotWriteToFile)
|
||||
completionHandler(error)
|
||||
} else {
|
||||
completionHandler(nil)
|
||||
@@ -427,7 +636,7 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
* `urlSession(_:streamTask:didBecome:outputStream:)` delegate message.
|
||||
*/
|
||||
open func captureStreams() {
|
||||
if #available(iOS 9.0, OSX 10.11, *) {
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
if self.useURLSession {
|
||||
_underlyingTask!.captureStreams()
|
||||
return
|
||||
@@ -457,7 +666,7 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
* you continue reading until the stream reaches end-of-file (EOF).
|
||||
*/
|
||||
open func closeWrite() {
|
||||
if #available(iOS 9.0, OSX 10.11, *) {
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
if self.useURLSession {
|
||||
_underlyingTask!.closeWrite()
|
||||
return
|
||||
@@ -477,6 +686,10 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
var byteSent: Int = 0
|
||||
let expireDate = Date(timeIntervalSinceNow: timeout)
|
||||
while self.dataToBeSent.count > 0 && (timeout == 0 || expireDate > Date()) {
|
||||
if let _ = outputStream.streamError {
|
||||
return byteSent == 0 ? -1 : byteSent
|
||||
}
|
||||
|
||||
let bytesWritten = self.dataToBeSent.withUnsafeBytes {
|
||||
outputStream.write($0, maxLength: self.dataToBeSent.count)
|
||||
}
|
||||
@@ -509,7 +722,7 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
* after calling this method will result in an error.
|
||||
*/
|
||||
open func closeRead() {
|
||||
if #available(iOS 9.0, OSX 10.11, *) {
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
if self.useURLSession {
|
||||
_underlyingTask!.closeRead()
|
||||
return
|
||||
@@ -528,6 +741,9 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate var isSecure = false
|
||||
|
||||
public var securityLevel: StreamSocketSecurityLevel = .negotiatedSSL
|
||||
/**
|
||||
* Completes any enqueued reads and writes, and establishes a secure connection.
|
||||
*
|
||||
@@ -535,16 +751,20 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
* `urlSession(_:task:didReceive:completionHandler:)` method.
|
||||
*/
|
||||
open func startSecureConnection() {
|
||||
if #available(iOS 9.0, OSX 10.11, *) {
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
if self.useURLSession {
|
||||
_underlyingTask!.startSecureConnection()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
isSecure = true
|
||||
operation_queue.addOperation {
|
||||
self.inputStream!.setProperty(StreamSocketSecurityLevel.negotiatedSSL.rawValue, forKey: .socketSecurityLevelKey)
|
||||
self.outputStream!.setProperty(StreamSocketSecurityLevel.negotiatedSSL.rawValue, forKey: .socketSecurityLevelKey)
|
||||
if let inputStream = self.inputStream, let outputStream = self.outputStream,
|
||||
inputStream.property(forKey: .socketSecurityLevelKey) as? String == StreamSocketSecurityLevel.none.rawValue {
|
||||
inputStream.setProperty(self.securityLevel.rawValue, forKey: .socketSecurityLevelKey)
|
||||
outputStream.setProperty(self.securityLevel.rawValue, forKey: .socketSecurityLevelKey)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -552,30 +772,53 @@ public class FileProviderStreamTask: URLSessionTask, StreamDelegate {
|
||||
* Completes any enqueued reads and writes, and closes the secure connection.
|
||||
*/
|
||||
open func stopSecureConnection() {
|
||||
if #available(iOS 9.0, OSX 10.11, *) {
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
if self.useURLSession {
|
||||
_underlyingTask!.stopSecureConnection()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
isSecure = false
|
||||
operation_queue.addOperation {
|
||||
self.inputStream!.setProperty(StreamSocketSecurityLevel.none.rawValue, forKey: .socketSecurityLevelKey)
|
||||
self.outputStream!.setProperty(StreamSocketSecurityLevel.none.rawValue, forKey: .socketSecurityLevelKey)
|
||||
if let inputStream = self.inputStream, let outputStream = self.outputStream,
|
||||
inputStream.property(forKey: .socketSecurityLevelKey) as? String != StreamSocketSecurityLevel.none.rawValue {
|
||||
|
||||
inputStream.setProperty(StreamSocketSecurityLevel.none.rawValue, forKey: .socketSecurityLevelKey)
|
||||
outputStream.setProperty(StreamSocketSecurityLevel.none.rawValue, forKey: .socketSecurityLevelKey)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private var _retriesForInputStream: Int = 0
|
||||
private var _retriesForOutputStream: Int = 0
|
||||
open func stream(_ aStream: Stream, handle eventCode: Stream.Event) {
|
||||
if eventCode.contains(.errorOccurred) {
|
||||
self._error = aStream.streamError
|
||||
streamDelegate?.urlSession?(_underlyingSession, task: self, didCompleteWithError: error)
|
||||
let retries: Int
|
||||
if aStream == self.inputStream {
|
||||
retries = _retriesForInputStream
|
||||
_retriesForInputStream += 1
|
||||
} else if aStream == self.outputStream {
|
||||
retries = _retriesForOutputStream
|
||||
_retriesForOutputStream += 1
|
||||
} else {
|
||||
return
|
||||
}
|
||||
|
||||
if retries < 3 {
|
||||
aStream.open()
|
||||
} else {
|
||||
self._error = aStream.streamError
|
||||
streamDelegate?.urlSession?(_underlyingSession, task: self, didCompleteWithError: error)
|
||||
}
|
||||
}
|
||||
|
||||
if aStream == inputStream && eventCode.contains(.hasBytesAvailable) {
|
||||
while (inputStream!.hasBytesAvailable) {
|
||||
if aStream == self.inputStream && eventCode.contains(.hasBytesAvailable) {
|
||||
while (self.inputStream!.hasBytesAvailable) {
|
||||
var buffer = [UInt8](repeating: 0, count: 2048)
|
||||
let len = inputStream!.read(&buffer, maxLength: buffer.count)
|
||||
let len = self.inputStream!.read(&buffer, maxLength: buffer.count)
|
||||
if len > 0 {
|
||||
dataReceived.append(&buffer, count: len)
|
||||
self.dataReceived.append(&buffer, count: len)
|
||||
self._countOfBytesRecieved += Int64(len)
|
||||
}
|
||||
}
|
||||
@@ -640,6 +883,7 @@ internal protocol FPSStreamDelegate : URLSessionTaskDelegate {
|
||||
*/
|
||||
@objc optional func urlSession(_ session: URLSession, streamTask: FileProviderStreamTask, didBecome inputStream: InputStream, outputStream: OutputStream)
|
||||
}
|
||||
// codebeat:enable[TOTAL_LOC,TOO_MANY_IVARS]
|
||||
|
||||
private let ports: [String: Int] = ["http": 80, "https": 443, "smb": 445,"ftp": 21,
|
||||
"telnet": 23, "pop": 110, "smtp": 25, "imap": 143]
|
||||
|
||||
+581
-388
File diff suppressed because it is too large
Load Diff
+762
-486
File diff suppressed because it is too large
Load Diff
+170
-25
@@ -9,32 +9,34 @@
|
||||
import Foundation
|
||||
|
||||
/// Containts path, url and attributes of a file or resource.
|
||||
open class FileObject: Equatable {
|
||||
open class FileObject: NSObject {
|
||||
/// A `Dictionary` contains file information, using `URLResourceKey` keys.
|
||||
open internal(set) var allValues: [URLResourceKey: Any]
|
||||
|
||||
internal init(allValues: [URLResourceKey: Any]) {
|
||||
public init(allValues: [URLResourceKey: Any]) {
|
||||
self.allValues = allValues
|
||||
}
|
||||
|
||||
internal init(url: URL, name: String, path: String) {
|
||||
internal init(url: URL?, name: String, path: String) {
|
||||
self.allValues = [URLResourceKey: Any]()
|
||||
self.url = url
|
||||
super.init()
|
||||
if let url = url {
|
||||
self.url = url
|
||||
}
|
||||
self.name = name
|
||||
self.path = path
|
||||
}
|
||||
|
||||
/// url to access the resource, not supported by Dropbox provider
|
||||
@available(*, obsoleted: 1.0, renamed: "url", message: "Use url.absoluteURL instead.")
|
||||
open var absoluteURL: URL? {
|
||||
return url?.absoluteURL
|
||||
}
|
||||
|
||||
/// URL to access the resource, can be a relative URL against base URL.
|
||||
/// not supported by Dropbox provider.
|
||||
open internal(set) var url: URL? {
|
||||
open internal(set) var url: URL {
|
||||
get {
|
||||
return allValues[.fileURLKey] as? URL
|
||||
if let url = allValues[.fileURLKey] as? URL {
|
||||
return url
|
||||
} else {
|
||||
let path = self.path.addingPercentEncoding(withAllowedCharacters: .filePathAllowed) ?? self.path
|
||||
return URL(string: path) ?? URL(string: "/")!
|
||||
}
|
||||
}
|
||||
set {
|
||||
allValues[.fileURLKey] = newValue
|
||||
@@ -71,6 +73,16 @@ open class FileObject: Equatable {
|
||||
}
|
||||
}
|
||||
|
||||
/// Count of children items of a driectory.
|
||||
open internal(set) var childrensCount: Int? {
|
||||
get {
|
||||
return allValues[.childrensCount] as? Int
|
||||
}
|
||||
set {
|
||||
allValues[.childrensCount] = newValue
|
||||
}
|
||||
}
|
||||
|
||||
/// The time contents of file has been created, returns nil if not set
|
||||
open internal(set) var creationDate: Date? {
|
||||
get {
|
||||
@@ -92,9 +104,9 @@ open class FileObject: Equatable {
|
||||
}
|
||||
|
||||
/// return resource type of file, usually directory, regular or symLink
|
||||
open internal(set) var type: URLFileResourceType? {
|
||||
open internal(set) var type: URLFileResourceType {
|
||||
get {
|
||||
return allValues[.fileResourceTypeKey] as? URLFileResourceType
|
||||
return allValues[.fileResourceTypeKey] as? URLFileResourceType ?? .unknown
|
||||
}
|
||||
set {
|
||||
allValues[.fileResourceTypeKey] = newValue
|
||||
@@ -136,25 +148,55 @@ open class FileObject: Equatable {
|
||||
open var isSymLink: Bool {
|
||||
return self.type == .symbolicLink
|
||||
}
|
||||
}
|
||||
|
||||
extension FileObject {
|
||||
open override var hashValue: Int {
|
||||
let hashURL = self.url.hashValue
|
||||
let hashSize = self.size.hashValue
|
||||
return (hashURL << 7) &+ hashURL &+ hashSize
|
||||
}
|
||||
|
||||
open override var hash: Int {
|
||||
return self.hashValue
|
||||
}
|
||||
|
||||
/// Check `FileObject` equality
|
||||
public static func ==(lhs: FileObject, rhs: FileObject) -> Bool {
|
||||
if rhs === lhs {
|
||||
if rhs === lhs {
|
||||
return true
|
||||
}
|
||||
if type(of: lhs) != type(of: rhs) {
|
||||
#if swift(>=3.1)
|
||||
if Swift.type(of: lhs) != Swift.type(of: rhs) {
|
||||
return false
|
||||
}
|
||||
if let rurl = rhs.url, let lurl = lhs.url {
|
||||
return rurl == lurl
|
||||
#else
|
||||
if type(of: lhs) != type(of: rhs) {
|
||||
return false
|
||||
}
|
||||
#endif
|
||||
|
||||
if let rurl = rhs.allValues[.fileURLKey] as? URL, let lurl = lhs.allValues[.fileURLKey] as? URL {
|
||||
return rurl == lurl && rhs.size == lhs.size
|
||||
}
|
||||
return rhs.path == lhs.path && rhs.size == lhs.size && rhs.modifiedDate == lhs.modifiedDate
|
||||
}
|
||||
|
||||
open override func isEqual(_ object: Any?) -> Bool {
|
||||
guard let object = object as? FileObject else { return false }
|
||||
return self == object
|
||||
}
|
||||
}
|
||||
|
||||
extension FileObject {
|
||||
internal func mapPredicate() -> [String: Any] {
|
||||
let mapDict: [URLResourceKey: String] = [.fileURLKey: "url", .nameKey: "name", .pathKey: "path", .fileSizeKey: "filesize", .creationDateKey: "creationDate",
|
||||
.contentModificationDateKey: "modifiedDate", .isHiddenKey: "isHidden", .isWritableKey: "isWritable", .serverDateKey: "serverDate", .entryTagKey: "entryTag", .mimeTypeKey: "mimeType"]
|
||||
let typeDict: [URLFileResourceType: String] = [.directory: "directory", .regular: "regular", .symbolicLink: "symbolicLink", .unknown: "unknown"]
|
||||
let mapDict: [URLResourceKey: String] = [.fileURLKey: "url", .nameKey: "name", .pathKey: "path",
|
||||
.fileSizeKey: "fileSize", .creationDateKey: "creationDate",
|
||||
.contentModificationDateKey: "modifiedDate", .isHiddenKey: "isHidden",
|
||||
.isWritableKey: "isWritable", .serverDateKey: "serverDate",
|
||||
.entryTagKey: "entryTag", .mimeTypeKey: "mimeType"]
|
||||
let typeDict: [URLFileResourceType: String] = [.directory: "directory", .regular: "regular",
|
||||
.symbolicLink: "symbolicLink", .unknown: "unknown"]
|
||||
var result = [String: Any]()
|
||||
for (key, value) in allValues {
|
||||
if let convertkey = mapDict[key] {
|
||||
@@ -162,19 +204,22 @@ open class FileObject: Equatable {
|
||||
}
|
||||
}
|
||||
result["eTag"] = result["entryTag"]
|
||||
result["filesize"] = result["fileSize"]
|
||||
result["isReadOnly"] = self.isReadOnly
|
||||
result["isDirectory"] = self.isDirectory
|
||||
result["isRegularFile"] = self.isRegularFile
|
||||
result["isSymLink"] = self.isSymLink
|
||||
result["type"] = typeDict[self.type ?? .unknown] ?? "unknown"
|
||||
result["type"] = typeDict[self.type] ?? "unknown"
|
||||
return result
|
||||
}
|
||||
|
||||
/// Converts macOS spotlight query for searching files to a query that can be used for `searchFiles()` method
|
||||
static public func convertPredicate(fromSpotlight query: NSPredicate) -> NSPredicate {
|
||||
let mapDict: [String: URLResourceKey] = [NSMetadataItemURLKey: .fileURLKey, NSMetadataItemFSNameKey: .nameKey, NSMetadataItemPathKey: .pathKey,
|
||||
NSMetadataItemFSSizeKey: .fileSizeKey, NSMetadataItemFSCreationDateKey: .creationDateKey,
|
||||
NSMetadataItemFSContentChangeDateKey: .contentModificationDateKey, "kMDItemFSInvisible": .isHiddenKey, "kMDItemFSIsWriteable": .isWritableKey, "kMDItemKind": .mimeTypeKey]
|
||||
let mapDict: [String: URLResourceKey] = [NSMetadataItemURLKey: .fileURLKey, NSMetadataItemFSNameKey: .nameKey,
|
||||
NSMetadataItemPathKey: .pathKey, NSMetadataItemFSSizeKey: .fileSizeKey,
|
||||
NSMetadataItemFSCreationDateKey: .creationDateKey, NSMetadataItemFSContentChangeDateKey: .contentModificationDateKey,
|
||||
"kMDItemFSInvisible": .isHiddenKey, "kMDItemFSIsWriteable": .isWritableKey,
|
||||
"kMDItemKind": .mimeTypeKey]
|
||||
|
||||
if let cQuery = query as? NSCompoundPredicate {
|
||||
let newSub = cQuery.subpredicates.map { convertPredicate(fromSpotlight: $0 as! NSPredicate) }
|
||||
@@ -199,6 +244,106 @@ open class FileObject: Equatable {
|
||||
}
|
||||
}
|
||||
|
||||
/// Containts attributes of a provider.
|
||||
open class VolumeObject: NSObject {
|
||||
/// A `Dictionary` contains volume information, using `URLResourceKey` keys.
|
||||
open internal(set) var allValues: [URLResourceKey: Any]
|
||||
|
||||
public init(allValues: [URLResourceKey: Any]) {
|
||||
self.allValues = allValues
|
||||
}
|
||||
|
||||
/// The root directory of the resource’s volume, returned as an `URL` object.
|
||||
open internal(set) var url: URL? {
|
||||
get {
|
||||
return allValues[.volumeURLKey] as? URL
|
||||
}
|
||||
set {
|
||||
allValues[.volumeURLKey] = newValue
|
||||
}
|
||||
}
|
||||
|
||||
/// The name of the volume.
|
||||
open internal(set) var name: String? {
|
||||
get {
|
||||
return allValues[.volumeNameKey] as? String
|
||||
}
|
||||
set {
|
||||
allValues[.volumeNameKey] = newValue
|
||||
}
|
||||
}
|
||||
|
||||
/// The root directory of the resource’s volume, returned as an `URL` object.
|
||||
open internal(set) var uuid: String? {
|
||||
get {
|
||||
return allValues[.volumeUUIDStringKey] as? String
|
||||
}
|
||||
set {
|
||||
allValues[.volumeUUIDStringKey] = newValue
|
||||
}
|
||||
}
|
||||
|
||||
/// the volume’s capacity in bytes, return -1 if is undetermined.
|
||||
open internal(set) var totalCapacity: Int64 {
|
||||
get {
|
||||
return allValues[.volumeTotalCapacityKey] as? Int64 ?? -1
|
||||
}
|
||||
set {
|
||||
allValues[.volumeTotalCapacityKey] = newValue
|
||||
}
|
||||
}
|
||||
|
||||
/// The volume’s available capacity in bytes.
|
||||
open internal(set) var availableCapacity: Int64 {
|
||||
get {
|
||||
return allValues[.volumeAvailableCapacityKey] as? Int64 ?? 0
|
||||
}
|
||||
set {
|
||||
allValues[.volumeAvailableCapacityKey] = newValue
|
||||
}
|
||||
}
|
||||
|
||||
open internal(set) var usage: Int64 {
|
||||
get {
|
||||
return totalCapacity >= 0 ? totalCapacity - availableCapacity : -availableCapacity
|
||||
}
|
||||
set {
|
||||
availableCapacity = totalCapacity >= 0 ? totalCapacity - newValue : -newValue
|
||||
}
|
||||
}
|
||||
|
||||
/// the volume’s creation date, returned as an `Date` object, or NULL if it cannot be determined
|
||||
open internal(set) var creationDate: Date? {
|
||||
get {
|
||||
return allValues[.volumeCreationDateKey] as? Date
|
||||
}
|
||||
set {
|
||||
allValues[.volumeCreationDateKey] = newValue
|
||||
}
|
||||
}
|
||||
|
||||
/// Determining whether the volume is read-only
|
||||
open internal(set) var isReadOnly: Bool {
|
||||
get {
|
||||
return allValues[.volumeIsReadOnlyKey] as? Bool ?? false
|
||||
}
|
||||
set {
|
||||
allValues[.volumeIsReadOnlyKey] = newValue
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 10.0, macOS 10.12, tvOS 10.0, *)
|
||||
open internal(set) var isEncrypted: Bool {
|
||||
get {
|
||||
return allValues[.volumeIsEncryptedKey] as? Bool ?? false
|
||||
}
|
||||
set {
|
||||
allValues[.volumeIsEncryptedKey] = !newValue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Sorting FileObject array by given criteria, **not thread-safe**
|
||||
public struct FileObjectSorting {
|
||||
|
||||
|
||||
+359
-220
@@ -19,7 +19,7 @@ public typealias ImageClass = NSImage
|
||||
public typealias SimpleCompletionHandler = ((_ error: Error?) -> Void)?
|
||||
|
||||
/// This protocol defines FileProvider neccesary functions and properties to connect and get contents list
|
||||
public protocol FileProviderBasic: class, NSCoding, NSSecureCoding {
|
||||
public protocol FileProviderBasic: class, NSSecureCoding {
|
||||
/// An string to identify type of provider.
|
||||
static var type: String { get }
|
||||
|
||||
@@ -29,9 +29,6 @@ public protocol FileProviderBasic: class, NSCoding, NSSecureCoding {
|
||||
/// The url of which paths should resolve against.
|
||||
var baseURL: URL? { get }
|
||||
|
||||
/// Current active path used in `contentsOfDirectory(path:completionHandler:)` method.
|
||||
var currentPath: String { get set }
|
||||
|
||||
/**
|
||||
Dispatch queue usually used in query methods.
|
||||
Set it to a new object to switch between cuncurrent and serial queues.
|
||||
@@ -64,28 +61,31 @@ public protocol FileProviderBasic: class, NSCoding, NSSecureCoding {
|
||||
|
||||
If the directory contains no entries or an error is occured, this method will return the empty array.
|
||||
|
||||
- Parameter path: path to target directory. If empty, `currentPath` value will be used.
|
||||
- Parameter completionHandler: a closure with result of directory entries or error.
|
||||
- `contents`: An array of `FileObject` identifying the the directory entries.
|
||||
- `error`: Error returned by system.
|
||||
- Parameters:
|
||||
- path: path to target directory. If empty, root will be iterated.
|
||||
- completionHandler: a closure with result of directory entries or error.
|
||||
- contents: An array of `FileObject` identifying the the directory entries.
|
||||
- error: Error returned by system.
|
||||
*/
|
||||
func contentsOfDirectory(path: String, completionHandler: @escaping ((_ contents: [FileObject], _ error: Error?) -> Void))
|
||||
func contentsOfDirectory(path: String, completionHandler: @escaping (_ contents: [FileObject], _ error: Error?) -> Void)
|
||||
|
||||
/**
|
||||
Returns a `FileObject` containing the attributes of the item (file, directory, symlink, etc.) at the path in question via asynchronous completion handler.
|
||||
|
||||
If the directory contains no entries or an error is occured, this method will return the empty `FileObject`.
|
||||
|
||||
- Parameter path: path to target directory. If empty, `currentPath` value will be used.
|
||||
- Parameter completionHandler: a closure with result of directory entries or error.
|
||||
- `attributes`: A `FileObject` containing the attributes of the item.
|
||||
- `error`: Error returned by system.
|
||||
- Parameters:
|
||||
- path: path to target directory. If empty, attributes of root will be returned.
|
||||
- completionHandler: a closure with result of directory entries or error.
|
||||
- attributes: A `FileObject` containing the attributes of the item.
|
||||
- error: Error returned by system.
|
||||
*/
|
||||
func attributesOfItem(path: String, completionHandler: @escaping ((_ attributes: FileObject?, _ error: Error?) -> Void))
|
||||
func attributesOfItem(path: String, completionHandler: @escaping (_ attributes: FileObject?, _ error: Error?) -> Void)
|
||||
|
||||
|
||||
/// Returns total and used capacity in provider container asynchronously.
|
||||
func storageProperties(completionHandler: @escaping ((_ total: Int64, _ used: Int64) -> Void))
|
||||
/// Returns volume/provider information asynchronously.
|
||||
/// - Parameter volumeInfo: Information of filesystem/Provider returned by system/server.
|
||||
func storageProperties(completionHandler: @escaping (_ volumeInfo: VolumeObject?) -> Void)
|
||||
|
||||
/**
|
||||
Search files inside directory using query asynchronously.
|
||||
@@ -98,8 +98,70 @@ public protocol FileProviderBasic: class, NSCoding, NSSecureCoding {
|
||||
- query: Simple string that file name begins with to be search, case-insensitive.
|
||||
- foundItemHandler: Closure which is called when a file is found
|
||||
- completionHandler: Closure which will be called after finishing search. Returns an arry of `FileObject` or error if occured.
|
||||
- files: all files meat the `query` criteria.
|
||||
- error: `Error` returned by server if occured.
|
||||
*/
|
||||
func searchFiles(path: String, recursive: Bool, query: String, foundItemHandler: ((FileObject) -> Void)?, completionHandler: @escaping ((_ files: [FileObject], _ error: Error?) -> Void))
|
||||
@discardableResult
|
||||
func searchFiles(path: String, recursive: Bool, query: String, foundItemHandler: ((FileObject) -> Void)?, completionHandler: @escaping (_ files: [FileObject], _ error: Error?) -> Void) -> Progress?
|
||||
|
||||
/**
|
||||
Search files inside directory using query asynchronously.
|
||||
|
||||
Sample predicates:
|
||||
```
|
||||
NSPredicate(format: "(name CONTAINS[c] 'hello') && (fileSize >= 10000)")
|
||||
NSPredicate(format: "(modifiedDate >= %@)", Date())
|
||||
NSPredicate(format: "(path BEGINSWITH %@)", "folder/child folder")
|
||||
```
|
||||
|
||||
- Note: Don't pass Spotlight predicates to this method directly, use `FileProvider.convertSpotlightPredicateTo()` method to get usable predicate.
|
||||
|
||||
- Important: A file name criteria should be provided for Dropbox.
|
||||
|
||||
- Parameters:
|
||||
- path: location of directory to start search
|
||||
- recursive: Searching subdirectories of path
|
||||
- query: An `NSPredicate` object with keys like `FileObject` members, except `size` which becomes `filesize`.
|
||||
- foundItemHandler: Closure which is called when a file is found
|
||||
- completionHandler: Closure which will be called after finishing search. Returns an arry of `FileObject` or error if occured.
|
||||
- files: all files meat the `query` criteria.
|
||||
- error: `Error` returned by server if occured.
|
||||
- Returns: An `Progress` to get progress or cancel progress. Use `completedUnitCount` to iterate count of found items.
|
||||
*/
|
||||
@discardableResult
|
||||
func searchFiles(path: String, recursive: Bool, query: NSPredicate, foundItemHandler: ((FileObject) -> Void)?, completionHandler: @escaping (_ files: [FileObject], _ error: Error?) -> Void) -> Progress?
|
||||
|
||||
/**
|
||||
Returns an independent url to access the file. Some providers like `Dropbox` due to their nature.
|
||||
don't return an absolute url to be used to access file directly.
|
||||
- Parameter path: Relative path of file or directory.
|
||||
- Returns: An url, can be used to access to file directly.
|
||||
*/
|
||||
func url(of path: String) -> URL
|
||||
|
||||
|
||||
/// Returns the relative path of url, without percent encoding. Even if url is absolute or
|
||||
/// retrieved from another provider, it will try to resolve the url against `baseURL` of
|
||||
/// current provider. It's highly recomended to use this method for displaying purposes.
|
||||
///
|
||||
/// - Parameter url: Absolute url to file or directory.
|
||||
/// - Returns: A `String` contains relative path of url against base url.
|
||||
func relativePathOf(url: URL) -> String
|
||||
|
||||
/// Checks the connection to server or permission on local
|
||||
///
|
||||
/// - Note: To prevent race condition, use this method wisely and avoid it as far possible.
|
||||
///
|
||||
/// - Parameter success: indicated server is reachable or not.
|
||||
/// - Parameter error: `Error` returned by server if occured.
|
||||
func isReachable(completionHandler: @escaping(_ success: Bool, _ error: Error?) -> Void)
|
||||
}
|
||||
|
||||
extension FileProviderBasic {
|
||||
public func searchFiles(path: String, recursive: Bool, query: String, foundItemHandler: ((FileObject) -> Void)?, completionHandler: @escaping (_ files: [FileObject], _ error: Error?) -> Void) -> Progress? {
|
||||
let predicate = NSPredicate(format: "name BEGINSWITH[c] %@", query)
|
||||
return self.searchFiles(path: path, recursive: recursive, query: predicate, foundItemHandler: foundItemHandler, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
/**
|
||||
Search files inside directory using query asynchronously.
|
||||
@@ -116,30 +178,16 @@ public protocol FileProviderBasic: class, NSCoding, NSSecureCoding {
|
||||
- Important: A file name criteria should be provided for Dropbox.
|
||||
|
||||
- Parameters:
|
||||
- path: location of directory to start search
|
||||
- recursive: Searching subdirectories of path
|
||||
- query: An `NSPredicate` object with keys like `FileObject` members, except `size` which becomes `filesize`.
|
||||
- foundItemHandler: Closure which is called when a file is found
|
||||
- completionHandler: Closure which will be called after finishing search. Returns an arry of `FileObject` or error if occured.
|
||||
- path: location of directory to start search
|
||||
- recursive: Searching subdirectories of path
|
||||
- query: An `NSPredicate` object with keys like `FileObject` members, except `size` which becomes `filesize`.
|
||||
- completionHandler: Closure which will be called after finishing search. Returns an arry of `FileObject` or error if occured.
|
||||
- files: all files meat the `query` criteria.
|
||||
- error: `Error` returned by server if occured.
|
||||
- Returns: An `Progress` to get progress or cancel progress. Use `completedUnitCount` to iterate count of found items.
|
||||
*/
|
||||
func searchFiles(path: String, recursive: Bool, query: NSPredicate, foundItemHandler: ((FileObject) -> Void)?, completionHandler: @escaping ((_ files: [FileObject], _ error: Error?) -> Void))
|
||||
|
||||
/**
|
||||
Returns an independent url to access the file. Some providers like `Dropbox` due to their nature.
|
||||
don't return an absolute url to be used to access file directly.
|
||||
- Parameter path: Relative path of file or directory.
|
||||
- Returns: An url, can be used to access to file directly.
|
||||
*/
|
||||
func url(of path: String?) -> URL
|
||||
|
||||
/// Checks the connection to server or permission on local
|
||||
func isReachable(completionHandler: @escaping(_ success: Bool) -> Void)
|
||||
}
|
||||
|
||||
extension FileProviderBasic {
|
||||
public func searchFiles(path: String, recursive: Bool, query: String, foundItemHandler: ((FileObject) -> Void)?, completionHandler: @escaping ((_ files: [FileObject], _ error: Error?) -> Void)) {
|
||||
let predicate = NSPredicate(format: "name BEGINSWITH[c] %@", query)
|
||||
self.searchFiles(path: path, recursive: recursive, query: predicate, foundItemHandler: foundItemHandler, completionHandler: completionHandler)
|
||||
func searchFiles(path: String, recursive: Bool, query: NSPredicate, completionHandler: @escaping (_ files: [FileObject], _ error: Error?) -> Void) -> Progress? {
|
||||
return searchFiles(path: path, recursive: recursive, query: query, foundItemHandler: nil, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
/// The maximum number of queued operations that can execute at the same time.
|
||||
@@ -153,8 +201,6 @@ extension FileProviderBasic {
|
||||
operation_queue.maxConcurrentOperationCount = newValue
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/// Checking equality of two file provider, regardless of current path queues and delegates.
|
||||
@@ -226,7 +272,7 @@ internal extension FileProviderBasicRemote {
|
||||
return false
|
||||
}
|
||||
|
||||
func runDataTask(with request: URLRequest, operationHandle: RemoteOperationHandle? = nil, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Swift.Void) {
|
||||
func runDataTask(with request: URLRequest, operation: FileOperationType? = nil, completionHandler: @escaping (Data?, URLResponse?, Error?) -> Swift.Void) {
|
||||
let useCache = self.useCache
|
||||
let validatingCache = self.validatingCache
|
||||
dispatch_queue.async {
|
||||
@@ -236,8 +282,7 @@ internal extension FileProviderBasicRemote {
|
||||
}
|
||||
}
|
||||
let task = self.session.dataTask(with: request, completionHandler: completionHandler)
|
||||
task.taskDescription = operationHandle?.operationType.json
|
||||
operationHandle?.add(task: task)
|
||||
task.taskDescription = operation?.json
|
||||
task.resume()
|
||||
}
|
||||
}
|
||||
@@ -256,10 +301,10 @@ public protocol FileProviderOperations: FileProviderBasic {
|
||||
- folder: Directory name.
|
||||
- at: Parent path of new directory.
|
||||
- completionHandler: If an error parameter was provided, a presentable `Error` will be returned.
|
||||
- Returns: An `OperationHandle` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
- Returns: An `Progress` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
*/
|
||||
@discardableResult
|
||||
func create(folder: String, at: String, completionHandler: SimpleCompletionHandler) -> OperationHandle?
|
||||
func create(folder: String, at: String, completionHandler: SimpleCompletionHandler) -> Progress?
|
||||
|
||||
/**
|
||||
Moves a file or directory from `path` to designated path asynchronously.
|
||||
@@ -270,10 +315,10 @@ public protocol FileProviderOperations: FileProviderBasic {
|
||||
- path: original file or directory path.
|
||||
- to: destination path of file or directory, including file/directory name.
|
||||
- completionHandler: If an error parameter was provided, a presentable `Error` will be returned.
|
||||
- Returns: An `OperationHandle` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
- Returns: An `Progress` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
*/
|
||||
@discardableResult
|
||||
func moveItem(path: String, to: String, completionHandler: SimpleCompletionHandler) -> OperationHandle?
|
||||
func moveItem(path: String, to: String, completionHandler: SimpleCompletionHandler) -> Progress?
|
||||
|
||||
/**
|
||||
Moves a file or directory from `path` to designated path asynchronously.
|
||||
@@ -285,10 +330,10 @@ public protocol FileProviderOperations: FileProviderBasic {
|
||||
- to: destination path of file or directory, including file/directory name.
|
||||
- overwrite: Destination file should be overwritten if file is already exists. **Default** is `false`.
|
||||
- completionHandler: If an error parameter was provided, a presentable `Error` will be returned.
|
||||
- Returns: An `OperationHandle` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
- Returns: An `Progress` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
*/
|
||||
@discardableResult
|
||||
func moveItem(path: String, to: String, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> OperationHandle?
|
||||
func moveItem(path: String, to: String, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> Progress?
|
||||
|
||||
/**
|
||||
Copies a file or directory from `path` to designated path asynchronously.
|
||||
@@ -299,10 +344,10 @@ public protocol FileProviderOperations: FileProviderBasic {
|
||||
- path: original file or directory path.
|
||||
- to: destination path of file or directory, including file/directory name.
|
||||
- completionHandler: If an error parameter was provided, a presentable `Error` will be returned.
|
||||
- Returns: An `OperationHandle` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
- Returns: An `Progress` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
*/
|
||||
@discardableResult
|
||||
func copyItem(path: String, to: String, completionHandler: SimpleCompletionHandler) -> OperationHandle?
|
||||
func copyItem(path: String, to: String, completionHandler: SimpleCompletionHandler) -> Progress?
|
||||
|
||||
/**
|
||||
Copies a file or directory from `path` to designated path asynchronously.
|
||||
@@ -314,10 +359,10 @@ public protocol FileProviderOperations: FileProviderBasic {
|
||||
- to: destination path of file or directory, including file/directory name.
|
||||
- overwrite: Destination file should be overwritten if file is already exists. **Default** is `false`.
|
||||
- completionHandler: If an error parameter was provided, a presentable `Error` will be returned.
|
||||
- Returns: An `OperationHandle` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
- Returns: An `Progress` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
*/
|
||||
@discardableResult
|
||||
func copyItem(path: String, to: String, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> OperationHandle?
|
||||
func copyItem(path: String, to: String, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> Progress?
|
||||
|
||||
/**
|
||||
Removes the file or directory at the specified path.
|
||||
@@ -325,11 +370,11 @@ public protocol FileProviderOperations: FileProviderBasic {
|
||||
- Parameters:
|
||||
- path: file or directory path.
|
||||
- completionHandler: If an error parameter was provided, a presentable `Error` will be returned.
|
||||
- Returns: An `OperationHandle` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
- Returns: An `Progress` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
|
||||
*/
|
||||
@discardableResult
|
||||
func removeItem(path: String, completionHandler: SimpleCompletionHandler) -> OperationHandle?
|
||||
func removeItem(path: String, completionHandler: SimpleCompletionHandler) -> Progress?
|
||||
|
||||
/**
|
||||
Uploads a file from local file url to designated path asynchronously.
|
||||
@@ -341,10 +386,10 @@ public protocol FileProviderOperations: FileProviderBasic {
|
||||
- localFile: a file url to file.
|
||||
- to: destination path of file, including file/directory name.
|
||||
- completionHandler: If an error parameter was provided, a presentable `Error` will be returned.
|
||||
- Returns: An `OperationHandle` to get progress or cancel progress.
|
||||
- Returns: An `Progress` to get progress or cancel progress.
|
||||
*/
|
||||
@discardableResult
|
||||
func copyItem(localFile: URL, to: String, completionHandler: SimpleCompletionHandler) -> OperationHandle?
|
||||
func copyItem(localFile: URL, to: String, completionHandler: SimpleCompletionHandler) -> Progress?
|
||||
|
||||
/**
|
||||
Uploads a file from local file url to designated path asynchronously.
|
||||
@@ -357,10 +402,10 @@ public protocol FileProviderOperations: FileProviderBasic {
|
||||
- to: destination path of file, including file/directory name.
|
||||
- overwrite: Destination file should be overwritten if file is already exists. **Default** is `false`.
|
||||
- completionHandler: If an error parameter was provided, a presentable `Error` will be returned.
|
||||
- Returns: An `OperationHandle` to get progress or cancel progress.
|
||||
- Returns: An `Progress` to get progress or cancel progress.
|
||||
*/
|
||||
@discardableResult
|
||||
func copyItem(localFile: URL, to: String, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> OperationHandle?
|
||||
func copyItem(localFile: URL, to: String, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> Progress?
|
||||
|
||||
/**
|
||||
Download a file from `path` to designated local file url asynchronously.
|
||||
@@ -372,37 +417,47 @@ public protocol FileProviderOperations: FileProviderBasic {
|
||||
- path: original file or directory path.
|
||||
- toLocalURL: destination local url of file, including file/directory name.
|
||||
- completionHandler: If an error parameter was provided, a presentable `Error` will be returned.
|
||||
- Returns: An `OperationHandle` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
- Returns: An `Progress` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
*/
|
||||
@discardableResult
|
||||
func copyItem(path: String, toLocalURL: URL, completionHandler: SimpleCompletionHandler) -> OperationHandle?
|
||||
func copyItem(path: String, toLocalURL: URL, completionHandler: SimpleCompletionHandler) -> Progress?
|
||||
}
|
||||
|
||||
public extension FileProviderOperations {
|
||||
/// *DEPRECATED:* Use Use FileProviderReadWrite.writeContents(path:, data:, completionHandler:) method instead.
|
||||
@available(*, deprecated, message: "Use FileProviderReadWrite.writeContents(path:, data:, completionHandler:) method instead.")
|
||||
@discardableResult
|
||||
public func create(file: String, at: String, contents data: Data?, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
let path = (at as NSString).appendingPathComponent(file)
|
||||
return (self as? FileProviderReadWrite)?.writeContents(path: path, contents: data, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
public func moveItem(path: String, to: String, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
public func moveItem(path: String, to: String, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
return self.moveItem(path: path, to: to, overwrite: false, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
public func copyItem(localFile: URL, to: String, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
public func copyItem(localFile: URL, to: String, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
return self.copyItem(localFile: localFile, to: to, overwrite: false, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
public func copyItem(path: String, to: String, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
public func copyItem(path: String, to: String, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
return self.copyItem(path: path, to: to, overwrite: false, completionHandler: completionHandler)
|
||||
}
|
||||
}
|
||||
|
||||
internal extension FileProviderOperations {
|
||||
internal func delegateNotify(_ operation: FileOperationType, error: Error? = nil) {
|
||||
DispatchQueue.main.async(execute: {
|
||||
if let error = error {
|
||||
self.delegate?.fileproviderFailed(self, operation: operation, error: error)
|
||||
} else {
|
||||
self.delegate?.fileproviderSucceed(self, operation: operation)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
internal func delegateNotify(_ operation: FileOperationType, progress: Double) {
|
||||
DispatchQueue.main.async(execute: {
|
||||
self.delegate?.fileproviderProgress(self, operation: operation, progress: Float(progress))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Defines method for fetching and modifying file contents
|
||||
public protocol FileProviderReadWrite: FileProviderBasic {
|
||||
/**
|
||||
@@ -412,12 +467,12 @@ public protocol FileProviderReadWrite: FileProviderBasic {
|
||||
- Parameters:
|
||||
- path: Path of file.
|
||||
- completionHandler: a closure with result of file contents or error.
|
||||
- `contents`: contents of file in a `Data` object.
|
||||
- `error`: Error returned by system.
|
||||
- Returns: An `OperationHandle` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
- contents: contents of file in a `Data` object.
|
||||
- error: `Error` returned by system if occured.
|
||||
- Returns: An `Progress` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
*/
|
||||
@discardableResult
|
||||
func contents(path: String, completionHandler: @escaping ((_ contents: Data?, _ error: Error?) -> Void)) -> OperationHandle?
|
||||
func contents(path: String, completionHandler: @escaping (_ contents: Data?, _ error: Error?) -> Void) -> Progress?
|
||||
|
||||
/**
|
||||
Retreives a `Data` object with a portion contents of the file asynchronously vis contents argument of completion handler.
|
||||
@@ -428,12 +483,12 @@ public protocol FileProviderReadWrite: FileProviderBasic {
|
||||
- offset: First byte index which should be read. **Starts from 0.**
|
||||
- length: Bytes count of data. Pass `-1` to read until the end of file.
|
||||
- completionHandler: a closure with result of file contents or error.
|
||||
- `contents`: contents of file in a `Data` object.
|
||||
- `error`: Error returned by system.
|
||||
- Returns: An `OperationHandle` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
- contents: contents of file in a `Data` object.
|
||||
- error: Error returned by system if occured.
|
||||
- Returns: An `Progress` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
*/
|
||||
@discardableResult
|
||||
func contents(path: String, offset: Int64, length: Int, completionHandler: @escaping ((_ contents: Data?, _ error: Error?) -> Void)) -> OperationHandle?
|
||||
func contents(path: String, offset: Int64, length: Int, completionHandler: @escaping (_ contents: Data?, _ error: Error?) -> Void) -> Progress?
|
||||
|
||||
/**
|
||||
Write the contents of the `Data` to a location asynchronously.
|
||||
@@ -444,10 +499,10 @@ public protocol FileProviderReadWrite: FileProviderBasic {
|
||||
- path: Path of target file.
|
||||
- contents: Data to be written into file, pass nil to create empty file.
|
||||
- completionHandler: If an error parameter was provided, a presentable `Error` will be returned.
|
||||
- Returns: An `OperationHandle` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
- Returns: An `Progress` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
*/
|
||||
@discardableResult
|
||||
func writeContents(path: String, contents: Data?, completionHandler: SimpleCompletionHandler) -> OperationHandle?
|
||||
func writeContents(path: String, contents: Data?, completionHandler: SimpleCompletionHandler) -> Progress?
|
||||
|
||||
/**
|
||||
Write the contents of the `Data` to a location asynchronously.
|
||||
@@ -458,10 +513,10 @@ public protocol FileProviderReadWrite: FileProviderBasic {
|
||||
- contents: Data to be written into file, pass nil to create empty file.
|
||||
- atomically: data will be written to a temporary file before writing to final location. Default is `false`.
|
||||
- completionHandler: If an error parameter was provided, a presentable `Error` will be returned.
|
||||
- Returns: An `OperationHandle` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
- Returns: An `Progress` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
*/
|
||||
@discardableResult
|
||||
func writeContents(path: String, contents: Data?, atomically: Bool, completionHandler: SimpleCompletionHandler) -> OperationHandle?
|
||||
func writeContents(path: String, contents: Data?, atomically: Bool, completionHandler: SimpleCompletionHandler) -> Progress?
|
||||
|
||||
/**
|
||||
Write the contents of the `Data` to a location asynchronously.
|
||||
@@ -472,10 +527,10 @@ public protocol FileProviderReadWrite: FileProviderBasic {
|
||||
- contents: Data to be written into file, pass nil to create empty file.
|
||||
- overwrite: Destination file should be overwritten if file is already exists. Default is `false`.
|
||||
- completionHandler: If an error parameter was provided, a presentable `Error` will be returned.
|
||||
- Returns: An `OperationHandle` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
- Returns: An `Progress` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
*/
|
||||
@discardableResult
|
||||
func writeContents(path: String, contents: Data?, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> OperationHandle?
|
||||
func writeContents(path: String, contents: Data?, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> Progress?
|
||||
|
||||
/**
|
||||
Write the contents of the `Data` to a location asynchronously.
|
||||
@@ -486,34 +541,100 @@ public protocol FileProviderReadWrite: FileProviderBasic {
|
||||
- overwrite: Destination file should be overwritten if file is already exists. Default is `false`.
|
||||
- atomically: data will be written to a temporary file before writing to final location. Default is `false`.
|
||||
- completionHandler: If an error parameter was provided, a presentable `Error` will be returned.
|
||||
- Returns: An `OperationHandle` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
- Returns: An `Progress` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
*/
|
||||
@discardableResult
|
||||
func writeContents(path: String, contents: Data?, atomically: Bool, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> OperationHandle?
|
||||
func writeContents(path: String, contents: Data?, atomically: Bool, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> Progress?
|
||||
}
|
||||
|
||||
extension FileProviderReadWrite {
|
||||
@discardableResult
|
||||
public func contents(path: String, completionHandler: @escaping ((_ contents: Data?, _ error: Error?) -> Void)) -> OperationHandle?{
|
||||
public func contents(path: String, completionHandler: @escaping ((_ contents: Data?, _ error: Error?) -> Void)) -> Progress? {
|
||||
return self.contents(path: path, offset: 0, length: -1, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
public func writeContents(path: String, contents: Data?, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
public func writeContents(path: String, contents: Data?, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
return self.writeContents(path: path, contents: contents, atomically: false, overwrite: false, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
public func writeContents(path: String, contents: Data?, atomically: Bool, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
public func writeContents(path: String, contents: Data?, atomically: Bool, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
return self.writeContents(path: path, contents: contents, atomically: atomically, overwrite: false, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
public func writeContents(path: String, contents: Data?, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
public func writeContents(path: String, contents: Data?, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
return self.writeContents(path: path, contents: contents, atomically: false, overwrite: overwrite, completionHandler: completionHandler)
|
||||
}
|
||||
}
|
||||
|
||||
/// Defines method for fetching file contents progressivly
|
||||
public protocol FileProviderReadWriteProgressive {
|
||||
/**
|
||||
Retreives a `Data` object with a portion contents of the file asynchronously vis contents argument of completion handler.
|
||||
If path specifies a directory, or if some other error occurs, data will be nil.
|
||||
|
||||
- Parameters:
|
||||
- path: Path of file.
|
||||
- progressHandler: a closure which will be called every time a new data received from server.
|
||||
- position: Start position of returned data, indexed from zero.
|
||||
- data: returned `Data` from server.
|
||||
- completionHandler: a closure which will be called after receiving is completed or an error occureed.
|
||||
- Returns: An `Progress` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
*/
|
||||
@discardableResult
|
||||
func contents(path: String, progressHandler: @escaping (_ position: Int64, _ data: Data) -> Void, completionHandler: SimpleCompletionHandler) -> Progress?
|
||||
|
||||
/**
|
||||
Retreives a `Data` object with a portion contents of the file asynchronously vis contents argument of completion handler.
|
||||
If path specifies a directory, or if some other error occurs, data will be nil.
|
||||
|
||||
- Parameters:
|
||||
- path: Path of file.
|
||||
- responseHandler: a closure which will be called after fetching server response.
|
||||
- response: `URLResponse` returned from server.
|
||||
- progressHandler: a closure which will be called every time a new data received from server.
|
||||
- position: Start position of returned data, indexed from zero.
|
||||
- data: returned `Data` from server.
|
||||
- completionHandler: a closure which will be called after receiving is completed or an error occureed.
|
||||
- Returns: An `Progress` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
*/
|
||||
@discardableResult
|
||||
func contents(path: String, responseHandler: ((_ response: URLResponse) -> Void)?, progressHandler: @escaping (_ position: Int64, _ data: Data) -> Void, completionHandler: SimpleCompletionHandler) -> Progress?
|
||||
|
||||
/**
|
||||
Retreives a `Data` object with a portion contents of the file asynchronously vis contents argument of completion handler.
|
||||
If path specifies a directory, or if some other error occurs, data will be nil.
|
||||
|
||||
- Parameters:
|
||||
- path: Path of file.
|
||||
- offset: First byte index which should be read. **Starts from 0.**
|
||||
- length: Bytes count of data. Pass `-1` to read until the end of file.
|
||||
- responseHandler: a closure which will be called after fetching server response.
|
||||
- response: `URLResponse` returned from server.
|
||||
- progressHandler: a closure which will be called every time a new data received from server.
|
||||
- position: Start position of returned data, indexed from offset.
|
||||
- data: returned `Data` from server.
|
||||
- completionHandler: a closure which will be called after receiving is completed or an error occureed.
|
||||
- Returns: An `Progress` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
*/
|
||||
@discardableResult
|
||||
func contents(path: String, offset: Int64, length: Int, responseHandler: ((_ response: URLResponse) -> Void)?, progressHandler: @escaping (_ position: Int64, _ data: Data) -> Void, completionHandler: SimpleCompletionHandler) -> Progress?
|
||||
}
|
||||
|
||||
public extension FileProviderReadWriteProgressive {
|
||||
@discardableResult
|
||||
public func contents(path: String, progressHandler: @escaping (_ position: Int64, _ data: Data) -> Void, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
return contents(path: path, offset: 0, length: -1, responseHandler: nil, progressHandler: progressHandler, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
public func contents(path: String, responseHandler: ((_ response: URLResponse) -> Void)?, progressHandler: @escaping (_ position: Int64, _ data: Data) -> Void, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
return contents(path: path, offset: 0, length: -1, responseHandler: responseHandler, progressHandler: progressHandler, completionHandler: completionHandler)
|
||||
}
|
||||
}
|
||||
|
||||
/// Allows a file provider to notify changes occured
|
||||
public protocol FileProviderMonitor: FileProviderBasic {
|
||||
|
||||
@@ -532,7 +653,7 @@ public protocol FileProviderMonitor: FileProviderBasic {
|
||||
- path: path of directory.
|
||||
- eventHandler: Closure executed after change, on a secondary thread.
|
||||
*/
|
||||
func registerNotifcation(path: String, eventHandler: @escaping (() -> Void))
|
||||
func registerNotifcation(path: String, eventHandler: @escaping () -> Void)
|
||||
|
||||
/// Stops monitoring the path.
|
||||
///
|
||||
@@ -546,6 +667,7 @@ public protocol FileProviderMonitor: FileProviderBasic {
|
||||
func isRegisteredForNotification(path: String) -> Bool
|
||||
}
|
||||
|
||||
#if os(macOS) || os(iOS) || os(tvOS)
|
||||
/// Allows undo file operations done by provider
|
||||
public protocol FileProvideUndoable: FileProviderOperations {
|
||||
/// To initialize undo manager either call `setupUndoManager()` or set it manually.
|
||||
@@ -555,8 +677,10 @@ public protocol FileProvideUndoable: FileProviderOperations {
|
||||
var undoManager: UndoManager? { get set }
|
||||
|
||||
/// UndoManager supports undoing this file operation
|
||||
func canUndo(handle: OperationHandle) -> Bool
|
||||
/// - Parameter handle: determines wheither this progress can be rolled back or not.
|
||||
func canUndo(handle: Progress) -> Bool
|
||||
/// UndoManager supports undoing this operation
|
||||
/// - Parameter operation: determines wheither this operation can be rolled back or not.
|
||||
func canUndo(operation: FileOperationType) -> Bool
|
||||
}
|
||||
|
||||
@@ -565,10 +689,14 @@ public extension FileProvideUndoable {
|
||||
return undoOperation(for: operation) != nil
|
||||
}
|
||||
|
||||
public func canUndo(handle: OperationHandle) -> Bool {
|
||||
return canUndo(operation: handle.operationType)
|
||||
public func canUndo(handle: Progress) -> Bool {
|
||||
if let operationType = handle.userInfo[.fileProvderOperationTypeKey] as? FileOperationType {
|
||||
return canUndo(operation: operationType)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/// Reuturns roll back operation for provided `operation`.
|
||||
internal func undoOperation(for operation: FileOperationType) -> FileOperationType? {
|
||||
switch operation {
|
||||
case .create(path: let path):
|
||||
@@ -595,69 +723,76 @@ public extension FileProvideUndoable {
|
||||
self.undoManager?.levelsOfUndo = 10
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/// This protocol defines method to share a public link with other users
|
||||
public protocol FileProviderSharing {
|
||||
/**
|
||||
Genrates a public url to a file to be shared with other users and can be downloaded without authentication.
|
||||
|
||||
- Important: In some providers url will be available for a limitied time, determined in `expiration` argument.
|
||||
e.g. Dropbox links will be expired after 4 hours.
|
||||
|
||||
- Parameters:
|
||||
- to: path of file, including file/directory name.
|
||||
- completionHandler: a closure with result of directory entries or error.
|
||||
- link: a url returned by Dropbox to share.
|
||||
- attribute: a `FileObject` containing the attributes of the item.
|
||||
- expiration: a `Date` object, determines when the public url will expires.
|
||||
- error: Error returned by server.
|
||||
*/
|
||||
func publicLink(to path: String, completionHandler: @escaping (_ link: URL?, _ attribute: FileObject?, _ expiration: Date?, _ error: Error?) -> Void)
|
||||
}
|
||||
|
||||
/// Defines protocol for provider allows all common operations.
|
||||
public protocol FileProvider: FileProviderBasic, FileProviderOperations, FileProviderReadWrite, NSCopying {
|
||||
public protocol FileProvider: FileProviderOperations, FileProviderReadWrite, NSCopying {
|
||||
}
|
||||
|
||||
internal let pathTrimSet = CharacterSet(charactersIn: " /")
|
||||
extension FileProviderBasic {
|
||||
public extension FileProviderBasic {
|
||||
public var type: String {
|
||||
#if swift(>=3.1)
|
||||
return Swift.type(of: self).type
|
||||
#else
|
||||
return type(of: self).type
|
||||
#endif
|
||||
}
|
||||
|
||||
/// **OBSOLETED** This property never worked as expected and is redundant as only supported by `LocalFileProvider`.
|
||||
/// To simulate `false` value, assign `URL(fileURLWithPath: "/")` to `baseURL`.
|
||||
@available(*, obsoleted: 1.0, message: "Redundant property, now is always true.")
|
||||
var isPathRelative: Bool { return true }
|
||||
|
||||
public func url(of path: String? = nil) -> URL {
|
||||
var rpath: String = path ?? self.currentPath
|
||||
rpath = rpath.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? rpath
|
||||
public func url(of path: String) -> URL {
|
||||
var rpath: String = path
|
||||
rpath = rpath.addingPercentEncoding(withAllowedCharacters: .filePathAllowed) ?? rpath
|
||||
if let baseURL = baseURL {
|
||||
if rpath.hasPrefix("/") {
|
||||
rpath.remove(at: rpath.startIndex)
|
||||
}
|
||||
return URL(string: rpath, relativeTo: baseURL) ?? baseURL
|
||||
} else {
|
||||
return URL(string: rpath)!
|
||||
return URL(string: rpath) ?? URL(string: "/")!
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Returns the relative path of url, wothout percent encoding. Even if url is absolute or
|
||||
/// retrieved from another provider, it will try to resolve the url against `baseURL` of
|
||||
/// current provider. It's highly recomended to use this method for displaying purposes.
|
||||
///
|
||||
/// - Parameter url: Absolute url to file or directory.
|
||||
/// - Returns: A `String` contains relative path of url against base url.
|
||||
public func relativePathOf(url: URL) -> String {
|
||||
// check if url derieved from current base url
|
||||
let relativePath = url.relativePath
|
||||
if !relativePath.isEmpty, url.baseURL == self.baseURL {
|
||||
return relativePath.removingPercentEncoding ?? relativePath
|
||||
return (relativePath.removingPercentEncoding ?? relativePath).replacingOccurrences(of: "/", with: "", options: .anchored)
|
||||
}
|
||||
|
||||
// resolve url string against baseurl
|
||||
guard let baseURL = self.baseURL?.standardizedFileURL else { return url.absoluteString }
|
||||
let standardPath = url.absoluteString.replacingOccurrences(of: "file:///private/var/", with: "file:///var/", options: .anchored)
|
||||
let standardBase = baseURL.absoluteString.replacingOccurrences(of: "file:///private/var/", with: "file:///var/", options: .anchored)
|
||||
let standardRelativePath = standardPath.replacingOccurrences(of: standardBase, with: "/")
|
||||
return standardRelativePath.removingPercentEncoding ?? standardRelativePath
|
||||
}
|
||||
|
||||
internal func correctPath(_ path: String?) -> String? {
|
||||
guard let path = path else { return nil }
|
||||
var p = path.hasPrefix("/") ? path : "/" + path
|
||||
if p.hasSuffix("/") {
|
||||
p.remove(at: p.index(before:p.endIndex))
|
||||
guard let baseURL = self.baseURL else { return url.absoluteString }
|
||||
let standardRelativePath = url.absoluteString.replacingOccurrences(of: baseURL.absoluteString, with: "/").replacingOccurrences(of: "/", with: "", options: .anchored)
|
||||
if URLComponents(string: standardRelativePath)?.host?.isEmpty ?? true {
|
||||
return standardRelativePath.removingPercentEncoding ?? standardRelativePath
|
||||
} else {
|
||||
return relativePath.replacingOccurrences(of: "/", with: "", options: .anchored)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
/// Returns a file name supposed to be unique with adding numbers to end of file.
|
||||
/// - Important: It's a synchronous method. Don't use it on matin thread.
|
||||
/// - Important: It's a synchronous method. Don't use it on main thread.
|
||||
/// - Parameter filePath: supposed path of file which should be examined.
|
||||
public func fileByUniqueName(_ filePath: String) -> String {
|
||||
//assert(!Thread.isMainThread, "\(#function) is not recommended to be executed on Main Thread.")
|
||||
let fileUrl = URL(fileURLWithPath: filePath)
|
||||
let dirPath = fileUrl.deletingLastPathComponent().path
|
||||
let fileName = fileUrl.deletingPathExtension().lastPathComponent
|
||||
@@ -678,7 +813,7 @@ extension FileProviderBasic {
|
||||
}
|
||||
var i = number ?? 2
|
||||
let similiar = contents.map {
|
||||
$0.url?.lastPathComponent ?? $0.name
|
||||
$0.url.lastPathComponent.isEmpty ? $0.name : $0.url.lastPathComponent
|
||||
}.filter {
|
||||
$0.hasPrefix(result)
|
||||
}
|
||||
@@ -693,16 +828,21 @@ extension FileProviderBasic {
|
||||
return (dirPath as NSString).appendingPathComponent(finalFile)
|
||||
}
|
||||
|
||||
internal func throwError(_ path: String, code: FoundationErrorEnum) -> NSError {
|
||||
internal func urlError(_ path: String, code: URLError.Code) -> Error {
|
||||
let fileURL = self.url(of: path)
|
||||
let domain: String
|
||||
switch code {
|
||||
case is URLError:
|
||||
domain = NSURLErrorDomain
|
||||
default:
|
||||
domain = NSCocoaErrorDomain
|
||||
}
|
||||
return NSError(domain: domain, code: code.rawValue, userInfo: [NSURLErrorFailingURLErrorKey: fileURL, NSURLErrorFailingURLStringErrorKey: fileURL.absoluteString])
|
||||
let userInfo: [String: Any] = [NSURLErrorKey: fileURL,
|
||||
NSURLErrorFailingURLErrorKey: fileURL,
|
||||
NSURLErrorFailingURLStringErrorKey: fileURL.absoluteString,
|
||||
]
|
||||
return URLError(code, userInfo: userInfo)
|
||||
}
|
||||
|
||||
internal func cocoaError(_ path: String, code: CocoaError.Code) -> Error {
|
||||
let fileURL = self.url(of: path)
|
||||
let userInfo: [String: Any] = [NSFilePathErrorKey: path,
|
||||
NSURLErrorKey: fileURL,
|
||||
]
|
||||
return CocoaError(code, userInfo: userInfo)
|
||||
}
|
||||
|
||||
internal func NotImplemented(_ fn: String = #function, file: StaticString = #file) {
|
||||
@@ -712,12 +852,6 @@ extension FileProviderBasic {
|
||||
|
||||
/// Define methods to get preview and thumbnail for files or folders
|
||||
public protocol ExtendedFileProvider: FileProviderBasic {
|
||||
/// Returuns true if thumbnail preview is supported by provider and file type accordingly.
|
||||
///
|
||||
/// - Parameter path: path of file.
|
||||
/// - Returns: A `Bool` idicates path can have thumbnail.
|
||||
func thumbnailOfFileSupported(path: String) -> Bool
|
||||
|
||||
/// Returns true if provider supports fetching properties of file like dimensions, duration, etc.
|
||||
/// Usually media or document files support these meta-infotmations.
|
||||
///
|
||||
@@ -726,19 +860,44 @@ public protocol ExtendedFileProvider: FileProviderBasic {
|
||||
func propertiesOfFileSupported(path: String) -> Bool
|
||||
|
||||
/**
|
||||
Generates ans returns a thumbnail preview of document asynchronously. The defualt dimension of returned image is different
|
||||
Fetching properties of file like dimensions, duration, etc. It's variant depending on file type.
|
||||
Images, videos and audio files meta-information will be returned.
|
||||
|
||||
- Note: `LocalFileInformationGenerator` variables can be set to change default behavior of
|
||||
thumbnail and properties generator of `LocalFileProvider`.
|
||||
|
||||
- Parameters:
|
||||
- path: path of file.
|
||||
- completionHandler: a closure with result of preview image or error.
|
||||
- propertiesDictionary: A `Dictionary` of proprty keys and values.
|
||||
- keys: An `Array` contains ordering of keys.
|
||||
- error: Error returned by system.
|
||||
*/
|
||||
@discardableResult
|
||||
func propertiesOfFile(path: String, completionHandler: @escaping (_ propertiesDictionary: [String: Any], _ keys: [String], _ error: Error?) -> Void) -> Progress?
|
||||
|
||||
#if os(macOS) || os(iOS) || os(tvOS)
|
||||
/// Returuns true if thumbnail preview is supported by provider and file type accordingly.
|
||||
///
|
||||
/// - Parameter path: path of file.
|
||||
/// - Returns: A `Bool` idicates path can have thumbnail.
|
||||
func thumbnailOfFileSupported(path: String) -> Bool
|
||||
|
||||
/**
|
||||
Generates and returns a thumbnail preview of document asynchronously. The defualt dimension of returned image is different
|
||||
regarding provider type, usually 64x64 pixels.
|
||||
|
||||
- Parameters:
|
||||
- path: path of file.
|
||||
- completionHandler: a closure with result of preview image or error.
|
||||
- `image`: `NSImage`/`UIImage` object contains preview.
|
||||
- `error`: Error returned by system.
|
||||
- image: `NSImage`/`UIImage` object contains preview.
|
||||
- error: `Error` returned by system.
|
||||
*/
|
||||
func thumbnailOfFile(path: String, completionHandler: @escaping ((_ image: ImageClass?, _ error: Error?) -> Void))
|
||||
@discardableResult
|
||||
func thumbnailOfFile(path: String, completionHandler: @escaping (_ image: ImageClass?, _ error: Error?) -> Void) -> Progress?
|
||||
|
||||
/**
|
||||
Generates ans returns a thumbnail preview of document asynchronously. The defualt dimension of returned image is different
|
||||
Generates and returns a thumbnail preview of document asynchronously. The defualt dimension of returned image is different
|
||||
regarding provider type, usually 64x64 pixels. Default value used when `dimenstion` is `nil`.
|
||||
|
||||
- Note: `LocalFileInformationGenerator` variables can be set to change default behavior of
|
||||
@@ -748,31 +907,19 @@ public protocol ExtendedFileProvider: FileProviderBasic {
|
||||
- path: path of file.
|
||||
- dimension: width and height of result preview image.
|
||||
- completionHandler: a closure with result of preview image or error.
|
||||
- `image`: `NSImage`/`UIImage` object contains preview.
|
||||
- `error`: Error returned by system.
|
||||
- image: `NSImage`/`UIImage` object contains preview.
|
||||
- error: `Error` returned by system.
|
||||
*/
|
||||
func thumbnailOfFile(path: String, dimension: CGSize?, completionHandler: @escaping ((_ image: ImageClass?, _ error: Error?) -> Void))
|
||||
|
||||
/**
|
||||
Fetching properties of file like dimensions, duration, etc. It's variant depending on file type.
|
||||
Images, videos and audio files meta-information will be returned.
|
||||
|
||||
- Note: `LocalFileInformationGenerator` variables can be set to change default behavior of
|
||||
thumbnail and properties generator of `LocalFileProvider`.
|
||||
|
||||
- Parameters:
|
||||
- path: path of file.
|
||||
- completionHandler: a closure with result of preview image or error.
|
||||
- `propertiesDictionary`: A `Dictionary` of proprty keys and values.
|
||||
- `keys`: An `Array` contains ordering of keys.
|
||||
- `error`: Error returned by system.
|
||||
*/
|
||||
func propertiesOfFile(path: String, completionHandler: @escaping ((_ propertiesDictionary: [String: Any], _ keys: [String], _ error: Error?) -> Void))
|
||||
@discardableResult
|
||||
func thumbnailOfFile(path: String, dimension: CGSize?, completionHandler: @escaping (_ image: ImageClass?, _ error: Error?) -> Void) -> Progress?
|
||||
#endif
|
||||
}
|
||||
|
||||
#if os(macOS) || os(iOS) || os(tvOS)
|
||||
extension ExtendedFileProvider {
|
||||
public func thumbnailOfFile(path: String, completionHandler: @escaping ((_ image: ImageClass?, _ error: Error?) -> Void)) {
|
||||
self.thumbnailOfFile(path: path, dimension: nil, completionHandler: completionHandler)
|
||||
@discardableResult
|
||||
public func thumbnailOfFile(path: String, completionHandler: @escaping ((_ image: ImageClass?, _ error: Error?) -> Void)) -> Progress? {
|
||||
return self.thumbnailOfFile(path: path, dimension: nil, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
internal static func convertToImage(pdfData: Data?, page: Int = 1) -> ImageClass? {
|
||||
@@ -801,21 +948,47 @@ extension ExtendedFileProvider {
|
||||
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
|
||||
|
||||
#if os(macOS)
|
||||
#if swift(>=4.0)
|
||||
let ppp = Int(NSScreen.main?.backingScaleFactor ?? 1) // fetch device is retina or not
|
||||
#elseif swift(>=3.3)
|
||||
let ppp = Int(NSScreen.main()?.backingScaleFactor ?? 1) // fetch device is retina or not
|
||||
#elseif swift(>=3.2)
|
||||
let ppp = Int(NSScreen.main?.backingScaleFactor ?? 1) // fetch device is retina or not
|
||||
#else
|
||||
let ppp = Int(NSScreen.main()?.backingScaleFactor ?? 1) // fetch device is retina or not
|
||||
#endif
|
||||
|
||||
size.width *= CGFloat(ppp)
|
||||
size.height *= CGFloat(ppp)
|
||||
|
||||
|
||||
#if swift(>=4.0)
|
||||
let rep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(size.width), pixelsHigh: Int(size.height),
|
||||
bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: .calibratedRGB,
|
||||
bytesPerRow: 0, bitsPerPixel: 0)
|
||||
#elseif swift(>=3.3)
|
||||
let rep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(size.width), pixelsHigh: Int(size.height),
|
||||
bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: NSCalibratedRGBColorSpace,
|
||||
bytesPerRow: 0, bitsPerPixel: 0)
|
||||
#elseif swift(>=3.2)
|
||||
let rep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(size.width), pixelsHigh: Int(size.height),
|
||||
bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: .calibratedRGB,
|
||||
bytesPerRow: 0, bitsPerPixel: 0)
|
||||
#else
|
||||
let rep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(size.width), pixelsHigh: Int(size.height),
|
||||
bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: NSCalibratedRGBColorSpace,
|
||||
bytesPerRow: 0, bitsPerPixel: 0)
|
||||
#endif
|
||||
|
||||
guard let context = NSGraphicsContext(bitmapImageRep: rep!) else {
|
||||
return nil
|
||||
}
|
||||
|
||||
NSGraphicsContext.saveGraphicsState()
|
||||
#if swift(>=4.0)
|
||||
NSGraphicsContext.current = context
|
||||
#else
|
||||
NSGraphicsContext.setCurrent(context)
|
||||
#endif
|
||||
|
||||
let transform = pdfPage.getDrawingTransform(CGPDFBox.mediaBox, rect: rect, rotate: 0, preserveAspectRatio: true)
|
||||
context.cgContext.concatenate(transform)
|
||||
@@ -829,19 +1002,20 @@ extension ExtendedFileProvider {
|
||||
return resultingImage
|
||||
#else
|
||||
let ppp = Int(UIScreen.main.scale) // fetch device is retina or not
|
||||
guard let context = UIGraphicsGetCurrentContext() else {
|
||||
return nil
|
||||
}
|
||||
size.width *= CGFloat(ppp)
|
||||
size.height *= CGFloat(ppp)
|
||||
UIGraphicsBeginImageContext(size)
|
||||
|
||||
guard let context = UIGraphicsGetCurrentContext() else {
|
||||
return nil
|
||||
}
|
||||
context.saveGState()
|
||||
let transform = pdfPage.getDrawingTransform(CGPDFBox.mediaBox, rect: rect, rotate: 0, preserveAspectRatio: true)
|
||||
context.concatenate(transform)
|
||||
|
||||
context.translateBy(x: 0, y: size.height)
|
||||
context.scaleBy(x: CGFloat(ppp), y: CGFloat(-ppp))
|
||||
context.setFillColor(UIColor.white.cgColor)
|
||||
context.fill(rect)
|
||||
context.drawPDFPage(pdfPage)
|
||||
|
||||
context.restoreGState()
|
||||
@@ -878,6 +1052,7 @@ extension ExtendedFileProvider {
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Operation type description of file operation, included files path in associated values.
|
||||
public enum FileOperationType: CustomStringConvertible {
|
||||
@@ -914,10 +1089,10 @@ public enum FileOperationType: CustomStringConvertible {
|
||||
}
|
||||
|
||||
/// Path of subjecting file.
|
||||
public var source: String? {
|
||||
guard let reflect = Mirror(reflecting: self).children.first?.value else { return nil }
|
||||
public var source: String {
|
||||
let reflect = Mirror(reflecting: self).children.first!.value
|
||||
let mirror = Mirror(reflecting: reflect)
|
||||
return reflect as? String ?? mirror.children.first?.value as? String
|
||||
return reflect as? String ?? mirror.children.first?.value as! String
|
||||
}
|
||||
|
||||
/// Path of subjecting file.
|
||||
@@ -938,6 +1113,8 @@ public enum FileOperationType: CustomStringConvertible {
|
||||
}
|
||||
let dest = json["dest"] as? String
|
||||
switch type {
|
||||
case "Fetch":
|
||||
self = .fetch(path: source)
|
||||
case "Create":
|
||||
self = .create(path: source)
|
||||
case "Modify":
|
||||
@@ -966,35 +1143,6 @@ public enum FileOperationType: CustomStringConvertible {
|
||||
}
|
||||
}
|
||||
|
||||
/// Allows to get progress or cancel an in-progress operation, useful for remote providers
|
||||
public protocol OperationHandle {
|
||||
/// Operation supposed to be done on files. Contains file paths as associated value.
|
||||
var operationType: FileOperationType { get }
|
||||
|
||||
/// Bytes written/read by operation so far.
|
||||
var bytesSoFar: Int64 { get }
|
||||
|
||||
/// Total bytes of operation.
|
||||
var totalBytes: Int64 { get }
|
||||
|
||||
/// Operation is progress or not, Returns false if operation is done or not initiated yet.
|
||||
var inProgress: Bool { get }
|
||||
|
||||
/// Progress of operation, usually equals with `bytesSoFar/totalBytes`. or NaN if not available.
|
||||
var progress: Float { get }
|
||||
|
||||
/// Cancels operation while in progress, or cancels data/download/upload url session task.
|
||||
func cancel() -> Bool
|
||||
}
|
||||
|
||||
public extension OperationHandle {
|
||||
public var progress: Float {
|
||||
let bytesSoFar = self.bytesSoFar
|
||||
let totalBytes = self.totalBytes
|
||||
return totalBytes > 0 ? Float(Double(bytesSoFar) / Double(totalBytes)) : Float.nan
|
||||
}
|
||||
}
|
||||
|
||||
/// Delegate methods for reporting provider's operation result and progress, when it's ready to update
|
||||
/// user interface.
|
||||
/// All methods are called in main thread to avoids UI bugs.
|
||||
@@ -1004,7 +1152,7 @@ public protocol FileProviderDelegate: class {
|
||||
func fileproviderSucceed(_ fileProvider: FileProviderOperations, operation: FileOperationType)
|
||||
/// fileproviderSucceed(_:operation:) gives delegate a notification when an operation finished with failure.
|
||||
/// This method is called in main thread to avoids UI bugs.
|
||||
func fileproviderFailed(_ fileProvider: FileProviderOperations, operation: FileOperationType)
|
||||
func fileproviderFailed(_ fileProvider: FileProviderOperations, operation: FileOperationType, error: Error)
|
||||
/// fileproviderSucceed(_:operation:) gives delegate a notification when an operation progess.
|
||||
/// Supported by some providers, especially remote ones.
|
||||
/// This method is called in main thread to avoids UI bugs.
|
||||
@@ -1020,12 +1168,3 @@ public protocol FileOperationDelegate: class {
|
||||
/// fileProvider(_:shouldProceedAfterError:copyingItemAtPath:toPath:) gives the delegate an opportunity to recover from or continue copying after an error. If an error occurs, the error object will contain an ErrorType indicating the problem. The source path and destination paths are also provided. If this method returns true, the FileProvider instance will continue as if the error had not occurred. If this method returns false, the NSFileManager instance will stop copying, return false from copyItemAtPath:toPath:error: and the error will be provied there.
|
||||
func fileProvider(_ fileProvider: FileProviderOperations, shouldProceedAfterError error: Error, operation: FileOperationType) -> Bool
|
||||
}
|
||||
|
||||
/// For internal use in `FileProvider` framework
|
||||
public protocol FoundationErrorEnum {
|
||||
/// Init from error code
|
||||
init? (rawValue: Int)
|
||||
// Raw error code
|
||||
var rawValue: Int { get }
|
||||
}
|
||||
|
||||
|
||||
@@ -1,228 +0,0 @@
|
||||
//
|
||||
// FileProviderExtensions.swift
|
||||
// FileProvider
|
||||
//
|
||||
// Created by Amir Abbas on 12/27/1395 AP.
|
||||
//
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
extension Array where Element: FileObject {
|
||||
/// Returns a sorted array of `FileObject`s by criterias set in attributes.
|
||||
public func sort(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]
|
||||
}
|
||||
|
||||
/// Sorts array of `FileObject`s by criterias set in attributes.
|
||||
public mutating func sorted(by type: FileObjectSorting.SortType, ascending: Bool = true, isDirectoriesFirst: Bool = false) {
|
||||
self = self.sort(by: type, ascending: ascending, isDirectoriesFirst: isDirectoriesFirst)
|
||||
}
|
||||
}
|
||||
|
||||
extension URLFileResourceType {
|
||||
/// Returns corresponding `URLFileResourceType` of a `FileAttributeType` value
|
||||
public init(fileTypeValue: FileAttributeType) {
|
||||
switch fileTypeValue {
|
||||
case FileAttributeType.typeCharacterSpecial: self = .characterSpecial
|
||||
case FileAttributeType.typeDirectory: self = .directory
|
||||
case FileAttributeType.typeBlockSpecial: self = .blockSpecial
|
||||
case FileAttributeType.typeRegular: self = .regular
|
||||
case FileAttributeType.typeSymbolicLink: self = .symbolicLink
|
||||
case FileAttributeType.typeSocket: self = .socket
|
||||
case FileAttributeType.typeUnknown: self = .unknown
|
||||
default: self = .unknown
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal extension URLResourceKey {
|
||||
static let fileURLKey = URLResourceKey(rawValue: "NSURLFileURLKey")
|
||||
static let serverDateKey = URLResourceKey(rawValue: "NSURLServerDateKey")
|
||||
static let entryTagKey = URLResourceKey(rawValue: "NSURLEntryTagKey")
|
||||
static let mimeTypeKey = URLResourceKey(rawValue: "NSURLMIMETypeIdentifierKey")
|
||||
|
||||
@available(*, deprecated, renamed: "fileURLKey")
|
||||
static let fileURL = fileURLKey
|
||||
@available(*, deprecated, renamed: "serverDateKey")
|
||||
static let serverDate = serverDateKey
|
||||
@available(*, deprecated, renamed: "entryTagKey")
|
||||
static let entryTag = entryTagKey
|
||||
@available(*, deprecated, renamed: "mimeTypeKey")
|
||||
static let mimeType = mimeTypeKey
|
||||
}
|
||||
|
||||
internal extension URL {
|
||||
var uw_scheme: String {
|
||||
return self.scheme ?? ""
|
||||
}
|
||||
|
||||
var fileIsDirectory: Bool {
|
||||
return (try? self.resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory ?? false
|
||||
}
|
||||
|
||||
var fileSize: Int64 {
|
||||
return Int64((try? self.resourceValues(forKeys: [.fileSizeKey]))?.fileSize ?? -1)
|
||||
}
|
||||
|
||||
var fileExists: Bool {
|
||||
return self.isFileURL && FileManager.default.fileExists(atPath: self.path)
|
||||
}
|
||||
}
|
||||
|
||||
internal extension Data {
|
||||
internal var isPDF: Bool {
|
||||
return self.count > 4 && self.scanString(length: 4, using: .ascii) == "%PDF"
|
||||
}
|
||||
|
||||
init? (jsonDictionary dictionary: [String: AnyObject]) {
|
||||
guard let data = try? JSONSerialization.data(withJSONObject: dictionary, options: []) else {
|
||||
return nil
|
||||
}
|
||||
self = data
|
||||
}
|
||||
|
||||
func deserializeJSON() -> [String: AnyObject]? {
|
||||
if let dic = try? JSONSerialization.jsonObject(with: self, options: []) as? [String: AnyObject] {
|
||||
return dic
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
init<T>(value: T) {
|
||||
var value = value
|
||||
self = Data(buffer: UnsafeBufferPointer(start: &value, count: 1))
|
||||
}
|
||||
|
||||
func scanValue<T>() -> T? {
|
||||
guard MemoryLayout<T>.size <= self.count else { return nil }
|
||||
return self.withUnsafeBytes { $0.pointee }
|
||||
}
|
||||
|
||||
func scanValue<T>(start: Int) -> T? {
|
||||
let length = MemoryLayout<T>.size
|
||||
guard self.count >= start + length else { return nil }
|
||||
return self.subdata(in: start..<start+length).withUnsafeBytes { $0.pointee }
|
||||
}
|
||||
|
||||
func scanString(start: Int = 0, length: Int, using encoding: String.Encoding = .utf8) -> String? {
|
||||
guard self.count >= start + length else { return nil }
|
||||
return String(data: self.subdata(in: start..<start+length), encoding: encoding)
|
||||
}
|
||||
|
||||
static func mapMemory<T, U>(from: T) -> U? {
|
||||
guard MemoryLayout<T>.size >= MemoryLayout<U>.size else { return nil }
|
||||
let data = Data(value: from)
|
||||
return data.scanValue()
|
||||
}
|
||||
}
|
||||
|
||||
internal extension String {
|
||||
init? (jsonDictionary: [String: AnyObject]) {
|
||||
guard let data = Data(jsonDictionary: jsonDictionary) else {
|
||||
return nil
|
||||
}
|
||||
self.init(data: data, encoding: .utf8)
|
||||
}
|
||||
|
||||
func deserializeJSON(using encoding: String.Encoding = .utf8) -> [String: AnyObject]? {
|
||||
guard let data = self.data(using: encoding) else {
|
||||
return nil
|
||||
}
|
||||
return data.deserializeJSON()
|
||||
}
|
||||
}
|
||||
|
||||
internal extension TimeInterval {
|
||||
internal var formatshort: String {
|
||||
var result = "0:00"
|
||||
if self < TimeInterval(Int32.max) {
|
||||
result = ""
|
||||
var time = DateComponents()
|
||||
time.hour = Int(self / 3600)
|
||||
time.minute = Int((self.truncatingRemainder(dividingBy: 3600)) / 60)
|
||||
time.second = Int(self.truncatingRemainder(dividingBy: 60))
|
||||
let formatter = NumberFormatter()
|
||||
formatter.paddingCharacter = "0"
|
||||
formatter.minimumIntegerDigits = 2
|
||||
formatter.maximumFractionDigits = 0
|
||||
let formatterFirst = NumberFormatter()
|
||||
formatterFirst.maximumFractionDigits = 0
|
||||
if time.hour! > 0 {
|
||||
result = "\(formatterFirst.string(from: NSNumber(value: time.hour!))!):\(formatter.string(from: NSNumber(value: time.minute!))!):\(formatter.string(from: NSNumber(value: time.second!))!)"
|
||||
} else {
|
||||
result = "\(formatterFirst.string(from: NSNumber(value: time.minute!))!):\(formatter.string(from: NSNumber(value: time.second!))!)"
|
||||
}
|
||||
}
|
||||
result = result.trimmingCharacters(in: CharacterSet(charactersIn: ": "))
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
extension Date {
|
||||
init?(rfcString: String) {
|
||||
let dateFor: DateFormatter = DateFormatter()
|
||||
dateFor.locale = Locale(identifier: "en_US")
|
||||
dateFor.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ssZ"
|
||||
if let rfc3339 = dateFor.date(from: rfcString) {
|
||||
self = rfc3339
|
||||
return
|
||||
}
|
||||
dateFor.dateFormat = "EEE',' dd' 'MMM' 'yyyy HH':'mm':'ss z"
|
||||
if let rfc1123 = dateFor.date(from: rfcString) {
|
||||
self = rfc1123
|
||||
return
|
||||
}
|
||||
dateFor.dateFormat = "EEEE',' dd'-'MMM'-'yy HH':'mm':'ss z"
|
||||
if let rfc850 = dateFor.date(from: rfcString) {
|
||||
self = rfc850
|
||||
return
|
||||
}
|
||||
dateFor.dateFormat = "EEE MMM d HH':'mm':'ss yyyy"
|
||||
if let asctime = dateFor.date(from: rfcString) {
|
||||
self = asctime
|
||||
return
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
internal func rfc3339utc() -> String {
|
||||
let fm = DateFormatter()
|
||||
fm.dateFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"
|
||||
fm.timeZone = TimeZone(identifier: "UTC")
|
||||
fm.locale = Locale(identifier: "en_US_POSIX")
|
||||
return fm.string(from: self)
|
||||
}
|
||||
}
|
||||
|
||||
extension NSPredicate {
|
||||
func findValue(forKey key: String?, operator op: NSComparisonPredicate.Operator? = nil) -> Any? {
|
||||
let val = findAllValues(forKey: key).lazy.filter { (op == nil || $0.operator == op!) && !$0.not }
|
||||
return val.first?.value
|
||||
}
|
||||
|
||||
func findAllValues(forKey key: String?) -> [(value: Any, operator: NSComparisonPredicate.Operator, not: Bool)] {
|
||||
if let cQuery = self as? NSCompoundPredicate {
|
||||
let find = cQuery.subpredicates.flatMap { ($0 as! NSPredicate).findAllValues(forKey: key) }
|
||||
if cQuery.compoundPredicateType == .not {
|
||||
return find.map { return ($0.value, $0.operator, !$0.not) }
|
||||
}
|
||||
return find
|
||||
} else if let cQuery = self as? NSComparisonPredicate {
|
||||
if cQuery.leftExpression.expressionType == .keyPath, key == nil || cQuery.leftExpression.keyPath == key!, let const = cQuery.rightExpression.constantValue {
|
||||
return [(value: const, operator: cQuery.predicateOperatorType, false)]
|
||||
}
|
||||
if cQuery.rightExpression.expressionType == .keyPath, key == nil || cQuery.rightExpression.keyPath == key!, let const = cQuery.leftExpression.constantValue {
|
||||
return [(value: const, operator: cQuery.predicateOperatorType, false)]
|
||||
}
|
||||
return []
|
||||
} else {
|
||||
return []
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension URLError.Code: FoundationErrorEnum {}
|
||||
extension CocoaError.Code: FoundationErrorEnum {}
|
||||
@@ -0,0 +1,641 @@
|
||||
//
|
||||
// HTTPFileProvider.swift
|
||||
// FilesProvider
|
||||
//
|
||||
// Created by Amir Abbas Mousavian.
|
||||
// Copyright © 2017 Mousavian. Distributed under MIT license.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
/**
|
||||
The abstract base class for all REST/Web based providers such as WebDAV, Dropbox, OneDrive, Google Drive, etc. and encapsulates basic
|
||||
functionalitis such as downloading/uploading.
|
||||
|
||||
No instance of this class should (and can) be created. Use derived classes instead. It leads to a crash with `fatalError()`.
|
||||
*/
|
||||
open class HTTPFileProvider: NSObject, FileProviderBasicRemote, FileProviderOperations, FileProviderReadWrite, FileProviderReadWriteProgressive {
|
||||
open class var type: String { fatalError("HTTPFileProvider is an abstract class. Please implement \(#function) in subclass.") }
|
||||
open let baseURL: URL?
|
||||
open var dispatch_queue: DispatchQueue
|
||||
open var operation_queue: OperationQueue {
|
||||
willSet {
|
||||
assert(_session == nil, "It's not effective to change dispatch_queue property after session is initialized.")
|
||||
}
|
||||
}
|
||||
|
||||
open weak var delegate: FileProviderDelegate?
|
||||
open var credential: URLCredential? {
|
||||
didSet {
|
||||
sessionDelegate?.credential = self.credential
|
||||
}
|
||||
}
|
||||
open private(set) var cache: URLCache?
|
||||
public var useCache: Bool
|
||||
public var validatingCache: Bool
|
||||
|
||||
fileprivate var _session: URLSession!
|
||||
internal fileprivate(set) var sessionDelegate: SessionDelegate?
|
||||
public var session: URLSession {
|
||||
get {
|
||||
if _session == nil {
|
||||
self.sessionDelegate = SessionDelegate(fileProvider: self)
|
||||
let config = URLSessionConfiguration.default
|
||||
config.urlCache = cache
|
||||
config.requestCachePolicy = .returnCacheDataElseLoad
|
||||
_session = URLSession(configuration: config, delegate: sessionDelegate as URLSessionDelegate?, delegateQueue: self.operation_queue)
|
||||
_session.sessionDescription = UUID().uuidString
|
||||
initEmptySessionHandler(_session.sessionDescription!)
|
||||
}
|
||||
return _session
|
||||
}
|
||||
|
||||
set {
|
||||
assert(newValue.delegate is SessionDelegate, "session instances should have a SessionDelegate instance as delegate.")
|
||||
_session = newValue
|
||||
if _session.sessionDescription?.isEmpty ?? true {
|
||||
_session.sessionDescription = UUID().uuidString
|
||||
}
|
||||
self.sessionDelegate = newValue.delegate as? SessionDelegate
|
||||
initEmptySessionHandler(_session.sessionDescription!)
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate var _longpollSession: URLSession?
|
||||
/// This session has extended timeout up to 10 minutes, suitable for monitoring.
|
||||
internal var longpollSession: URLSession {
|
||||
if _longpollSession == nil {
|
||||
let config = URLSessionConfiguration.default
|
||||
config.timeoutIntervalForRequest = 600
|
||||
_longpollSession = URLSession(configuration: config, delegate: nil, delegateQueue: nil)
|
||||
}
|
||||
return _longpollSession!
|
||||
}
|
||||
|
||||
/**
|
||||
This is parent initializer for subclasses. Using this method on `HTTPFileProvider` will fail as `type` is not implemented.
|
||||
|
||||
- Parameters:
|
||||
- baseURL: Location of server.
|
||||
- credential: An `URLCredential` object with `user` and `password`.
|
||||
- cache: A URLCache to cache downloaded files and contents.
|
||||
*/
|
||||
public init(baseURL: URL?, credential: URLCredential?, cache: URLCache?) {
|
||||
// Make base url absolute and path as directory
|
||||
let urlStr = baseURL?.absoluteString
|
||||
self.baseURL = urlStr.flatMap { $0.hasSuffix("/") ? URL(string: $0) : URL(string: $0 + "/") }
|
||||
self.useCache = false
|
||||
self.validatingCache = true
|
||||
self.cache = cache
|
||||
self.credential = credential
|
||||
|
||||
#if swift(>=3.1)
|
||||
let queueLabel = "FileProvider.\(Swift.type(of: self).type)"
|
||||
#else
|
||||
let queueLabel = "FileProvider.\(type(of: self).type)"
|
||||
#endif
|
||||
dispatch_queue = DispatchQueue(label: queueLabel, attributes: .concurrent)
|
||||
operation_queue = OperationQueue()
|
||||
operation_queue.name = "\(queueLabel).Operation"
|
||||
|
||||
super.init()
|
||||
}
|
||||
|
||||
public required convenience init?(coder aDecoder: NSCoder) {
|
||||
fatalError("HTTPFileProvider is an abstract class. Please implement \(#function) in subclass.")
|
||||
}
|
||||
|
||||
deinit {
|
||||
if let sessionuuid = _session?.sessionDescription {
|
||||
removeSessionHandler(for: sessionuuid)
|
||||
}
|
||||
|
||||
if fileProviderCancelTasksOnInvalidating {
|
||||
_session?.invalidateAndCancel()
|
||||
} else {
|
||||
_session?.finishTasksAndInvalidate()
|
||||
}
|
||||
longpollSession.invalidateAndCancel()
|
||||
}
|
||||
|
||||
public func encode(with aCoder: NSCoder) {
|
||||
aCoder.encode(self.baseURL, forKey: "baseURL")
|
||||
aCoder.encode(self.credential, forKey: "credential")
|
||||
aCoder.encode(self.useCache, forKey: "useCache")
|
||||
aCoder.encode(self.validatingCache, forKey: "validatingCache")
|
||||
}
|
||||
|
||||
public static var supportsSecureCoding: Bool {
|
||||
return true
|
||||
}
|
||||
|
||||
open func copy(with zone: NSZone? = nil) -> Any {
|
||||
fatalError("HTTPFileProvider is an abstract class. Please implement \(#function) in subclass.")
|
||||
}
|
||||
|
||||
open func contentsOfDirectory(path: String, completionHandler: @escaping (_ contents: [FileObject], _ error: Error?) -> Void) {
|
||||
fatalError("HTTPFileProvider is an abstract class. Please implement \(#function) in subclass.")
|
||||
}
|
||||
|
||||
open func attributesOfItem(path: String, completionHandler: @escaping (_ attributes: FileObject?, _ error: Error?) -> Void) {
|
||||
fatalError("HTTPFileProvider is an abstract class. Please implement \(#function) in subclass.")
|
||||
}
|
||||
|
||||
open func storageProperties(completionHandler: @escaping (_ volumeInfo: VolumeObject?) -> Void) {
|
||||
fatalError("HTTPFileProvider is an abstract class. Please implement \(#function) in subclass.")
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func searchFiles(path: String, recursive: Bool, query: NSPredicate, foundItemHandler: ((FileObject) -> Void)?, completionHandler: @escaping (_ files: [FileObject], _ error: Error?) -> Void) -> Progress? {
|
||||
fatalError("HTTPFileProvider is an abstract class. Please implement \(#function) in subclass.")
|
||||
}
|
||||
|
||||
open func isReachable(completionHandler: @escaping (_ success: Bool, _ error: Error?) -> Void) {
|
||||
self.storageProperties { volume in
|
||||
if volume != nil {
|
||||
completionHandler(volume != nil, nil)
|
||||
return
|
||||
} else {
|
||||
self.contentsOfDirectory(path: "", completionHandler: { (files, error) in
|
||||
completionHandler(false, error)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Nothing special for these two funcs, just reimplemented to workaround a bug in swift to allow override in subclasses!
|
||||
open func url(of path: String) -> URL {
|
||||
var rpath: String = path
|
||||
rpath = rpath.addingPercentEncoding(withAllowedCharacters: .filePathAllowed) ?? rpath
|
||||
if let baseURL = baseURL {
|
||||
if rpath.hasPrefix("/") {
|
||||
rpath.remove(at: rpath.startIndex)
|
||||
}
|
||||
return URL(string: rpath, relativeTo: baseURL) ?? baseURL
|
||||
} else {
|
||||
return URL(string: rpath) ?? URL(string: "/")!
|
||||
}
|
||||
}
|
||||
|
||||
open func relativePathOf(url: URL) -> String {
|
||||
// check if url derieved from current base url
|
||||
let relativePath = url.relativePath
|
||||
if !relativePath.isEmpty, url.baseURL == self.baseURL {
|
||||
return (relativePath.removingPercentEncoding ?? relativePath).replacingOccurrences(of: "/", with: "", options: .anchored)
|
||||
}
|
||||
|
||||
// resolve url string against baseurl
|
||||
guard let baseURL = self.baseURL else { return url.absoluteString }
|
||||
let standardRelativePath = url.absoluteString.replacingOccurrences(of: baseURL.absoluteString, with: "/").replacingOccurrences(of: "/", with: "", options: .anchored)
|
||||
if URLComponents(string: standardRelativePath)?.host?.isEmpty ?? true {
|
||||
return standardRelativePath.removingPercentEncoding ?? standardRelativePath
|
||||
} else {
|
||||
return relativePath.replacingOccurrences(of: "/", with: "", options: .anchored)
|
||||
}
|
||||
}
|
||||
|
||||
open weak var fileOperationDelegate: FileOperationDelegate?
|
||||
|
||||
@discardableResult
|
||||
open func create(folder folderName: String, at atPath: String, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
let path = (atPath as NSString).appendingPathComponent(folderName) + "/"
|
||||
return doOperation(.create(path: path), completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func moveItem(path: String, to toPath: String, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
return doOperation(.move(source: path, destination: toPath), overwrite: overwrite, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func copyItem(path: String, to toPath: String, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
return doOperation(.copy(source: path, destination: toPath), overwrite: overwrite, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func removeItem(path: String, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
return doOperation(.remove(path: path), completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func copyItem(localFile: URL, to toPath: String, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
// check file is not a folder
|
||||
guard (try? localFile.resourceValues(forKeys: [.fileResourceTypeKey]))?.fileResourceType ?? .unknown == .regular else {
|
||||
dispatch_queue.async {
|
||||
completionHandler?(self.urlError(localFile.path, code: .fileIsDirectory))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
let operation = FileOperationType.copy(source: localFile.absoluteString, destination: toPath)
|
||||
guard fileOperationDelegate?.fileProvider(self, shouldDoOperation: operation) ?? true == true else {
|
||||
return nil
|
||||
}
|
||||
let request = self.request(for: operation, overwrite: overwrite)
|
||||
return upload_file(toPath, request: request, localFile: localFile, operation: operation, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func copyItem(path: String, toLocalURL destURL: URL, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
let operation = FileOperationType.copy(source: path, destination: destURL.absoluteString)
|
||||
let request = self.request(for: operation)
|
||||
let cantLoadError = urlError(path, code: .cannotLoadFromNetwork)
|
||||
return self.download_simple(path: path, request: request, operation: operation, completionHandler: { [weak self] (tempURL, error) in
|
||||
do {
|
||||
if let error = error {
|
||||
throw error
|
||||
}
|
||||
|
||||
guard let tempURL = tempURL else {
|
||||
throw cantLoadError
|
||||
}
|
||||
|
||||
var coordError: NSError?
|
||||
NSFileCoordinator().coordinate(writingItemAt: tempURL, options: .forMoving, writingItemAt: destURL, options: .forReplacing, error: &coordError, byAccessor: { (tempURL, destURL) in
|
||||
do {
|
||||
try FileManager.default.moveItem(at: tempURL, to: destURL)
|
||||
|
||||
completionHandler?(nil)
|
||||
self?.delegateNotify(operation)
|
||||
} catch {
|
||||
completionHandler?(error)
|
||||
self?.delegateNotify(operation, error: error)
|
||||
}
|
||||
})
|
||||
|
||||
if let error = coordError {
|
||||
throw error
|
||||
}
|
||||
} catch {
|
||||
completionHandler?(error)
|
||||
self?.delegateNotify(operation, error: error)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
Progressively fetch data of file and returns fetched data in `progressHandler`.
|
||||
If path specifies a directory, or if some other error occurs, data will be nil.
|
||||
|
||||
- Parameters:
|
||||
- path: Path of file.
|
||||
- progressHandler: a closure called every time a new `Data` is available.
|
||||
- position: start position of data fetched.
|
||||
- data: a portion of contents of file in a `Data` object.
|
||||
- completionHandler: a closure with result of file contents or error.
|
||||
- error: `Error` returned by system if occured.
|
||||
- Returns: An `Progress` to get progress or cancel progress.
|
||||
*/
|
||||
@discardableResult
|
||||
open func contents(path: String, offset: Int64 = 0, length: Int = -1, responseHandler: ((_ response: URLResponse) -> Void)? = nil, progressHandler: @escaping (_ position: Int64, _ data: Data) -> Void, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
let operation = FileOperationType.fetch(path: path)
|
||||
var request = self.request(for: operation)
|
||||
request.setValue(rangeWithOffset: offset, length: length)
|
||||
var position: Int64 = offset
|
||||
return download_progressive(path: path, request: request, operation: operation, responseHandler: responseHandler, progressHandler: { data in
|
||||
progressHandler(position, data)
|
||||
position += Int64(data.count)
|
||||
}, completionHandler: (completionHandler ?? { _ in return }))
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func contents(path: String, offset: Int64, length: Int, completionHandler: @escaping ((_ contents: Data?, _ error: Error?) -> Void)) -> Progress? {
|
||||
if length == 0 || offset < 0 {
|
||||
dispatch_queue.async {
|
||||
completionHandler(Data(), nil)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
let operation = FileOperationType.fetch(path: path)
|
||||
var request = self.request(for: operation)
|
||||
let cantLoadError = urlError(path, code: .cannotLoadFromNetwork)
|
||||
request.setValue(rangeWithOffset: offset, length: length)
|
||||
return self.download_simple(path: path, request: request, operation: operation, completionHandler: { (tempURL, error) in
|
||||
do {
|
||||
if let error = error {
|
||||
throw error
|
||||
}
|
||||
|
||||
guard let tempURL = tempURL else {
|
||||
throw cantLoadError
|
||||
}
|
||||
|
||||
let data = try Data(contentsOf: tempURL)
|
||||
completionHandler(data, nil)
|
||||
} catch {
|
||||
completionHandler(nil, error)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func writeContents(path: String, contents data: Data?, atomically: Bool, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
let operation = FileOperationType.modify(path: path)
|
||||
guard fileOperationDelegate?.fileProvider(self, shouldDoOperation: operation) ?? true == true else {
|
||||
return nil
|
||||
}
|
||||
let request = self.request(for: operation, overwrite: overwrite, attributes: [.contentModificationDateKey: Date()])
|
||||
return upload_data(path, request: request, data: data ?? Data(), operation: operation, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
internal func request(for operation: FileOperationType, overwrite: Bool = false, attributes: [URLResourceKey: Any] = [:]) -> URLRequest {
|
||||
fatalError("HTTPFileProvider is an abstract class. Please implement \(#function) in subclass.")
|
||||
}
|
||||
|
||||
internal func serverError(with code: FileProviderHTTPErrorCode, path: String?, data: Data?) -> FileProviderHTTPError {
|
||||
fatalError("HTTPFileProvider is an abstract class. Please implement \(#function) in subclass.")
|
||||
}
|
||||
|
||||
internal func multiStatusHandler(source: String, data: Data, completionHandler: SimpleCompletionHandler) -> Void {
|
||||
// WebDAV will override this function
|
||||
}
|
||||
|
||||
/**
|
||||
This is the main function to init urlsession task for specified file operation.
|
||||
|
||||
You won't need to override this function unless another network request must be done before intended operation,
|
||||
such as retrieving file id from file path. Then you must call `super.doOperation()`
|
||||
|
||||
In case you have to call super method asyncronously, create a `Progress` object and pass ot to `progress` parameter.
|
||||
*/
|
||||
@discardableResult
|
||||
internal func doOperation(_ operation: FileOperationType, overwrite: Bool = false, progress: Progress? = nil,
|
||||
completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
guard fileOperationDelegate?.fileProvider(self, shouldDoOperation: operation) ?? true == true else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let progress = progress ?? Progress(totalUnitCount: 1)
|
||||
progress.setUserInfoObject(operation, forKey: .fileProvderOperationTypeKey)
|
||||
progress.kind = .file
|
||||
progress.setUserInfoObject(Progress.FileOperationKind.downloading, forKey: .fileOperationKindKey)
|
||||
|
||||
let request = self.request(for: operation, overwrite: overwrite)
|
||||
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var serverError: FileProviderHTTPError?
|
||||
if let response = response as? HTTPURLResponse {
|
||||
if response.statusCode >= 300, let code = FileProviderHTTPErrorCode(rawValue: response.statusCode) {
|
||||
serverError = self.serverError(with: code, path: operation.source, data: data)
|
||||
}
|
||||
|
||||
if FileProviderHTTPErrorCode(rawValue: response.statusCode) == .multiStatus, let data = data {
|
||||
self.multiStatusHandler(source: operation.source, data: data, completionHandler: completionHandler)
|
||||
}
|
||||
}
|
||||
|
||||
if serverError == nil && error == nil {
|
||||
progress.completedUnitCount = 1
|
||||
} else {
|
||||
progress.cancel()
|
||||
}
|
||||
|
||||
completionHandler?(serverError ?? error)
|
||||
self.delegateNotify(operation, error: serverError ?? error)
|
||||
})
|
||||
task.taskDescription = operation.json
|
||||
progress.cancellationHandler = { [weak task] in
|
||||
task?.cancel()
|
||||
}
|
||||
progress.setUserInfoObject(Date(), forKey: .startingTimeKey)
|
||||
task.resume()
|
||||
return progress
|
||||
}
|
||||
|
||||
// codebeat:disable[ARITY]
|
||||
|
||||
/**
|
||||
This method should be used in subclasses to fetch directory content from servers which support paginated results.
|
||||
Almost all HTTP based provider, except WebDAV, supports this method.
|
||||
|
||||
- Important: Please use `[weak self]` when implementing handlers to prevent retain cycles. In these cases,
|
||||
return `nil` as the result of handler as the operation will be aborted.
|
||||
|
||||
- Parameters:
|
||||
- path: path of directory which enqueued for listing, for informational use like errpr reporting.
|
||||
- requestHandler: Get token of next page and returns appropriate `URLRequest` to be sent to server.
|
||||
handler can return `nil` to cancel entire operation.
|
||||
- token: Token of the page which `URLRequest` is needed, token will be `nil` for initial page.
|
||||
- pageHandler: Handler which is called after fetching results of a page to parse data. will return parse result as
|
||||
array of `FileObject` or error if data is nil or parsing is failed. Method will not continue to next page if
|
||||
`error` is returned, otherwise `nextToken` will be used for next page. `nil` value for `newToken` will indicate
|
||||
last page of directory contents.
|
||||
- data: Raw data returned from server. Handler should parse them and return files.
|
||||
- progress: `Progress` object that `completedUnits` will be increased when a new `FileObject` is parsed in method.
|
||||
- completionHandler: All file objects returned by `pageHandler` will be passed to this handler, or error if occured.
|
||||
This handler will be called when `pageHandler` returns `nil for `newToken`.
|
||||
- contents: all files parsed via `pageHandler` will be return aggregated.
|
||||
- error: `Error` returned by server. `nil` means success. If exists, it means `contents` are incomplete.
|
||||
*/
|
||||
internal func paginated(_ path: String, requestHandler: @escaping (_ token: String?) -> URLRequest?,
|
||||
pageHandler: @escaping (_ data: Data?, _ progress: Progress) -> (files: [FileObject], error: Error?, newToken: String?),
|
||||
completionHandler: @escaping (_ contents: [FileObject], _ error: Error?) -> Void) -> Progress {
|
||||
let progress = Progress(totalUnitCount: -1)
|
||||
self.paginated(path, startToken: nil, currentProgress: progress, previousResult: [], requestHandler: requestHandler, pageHandler: pageHandler, completionHandler: completionHandler)
|
||||
return progress
|
||||
}
|
||||
|
||||
private func paginated(_ path: String, startToken: String?, currentProgress progress: Progress,
|
||||
previousResult: [FileObject], requestHandler: @escaping (_ token: String?) -> URLRequest?,
|
||||
pageHandler: @escaping (_ data: Data?, _ progress: Progress) -> (files: [FileObject], error: Error?, newToken: String?),
|
||||
completionHandler: @escaping (_ contents: [FileObject], _ error: Error?) -> Void) {
|
||||
guard !progress.isCancelled, let request = requestHandler(startToken) else {
|
||||
return
|
||||
}
|
||||
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
if let error = error {
|
||||
completionHandler(previousResult, error)
|
||||
return
|
||||
}
|
||||
if let code = (response as? HTTPURLResponse)?.statusCode , code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) {
|
||||
let responseError = self.serverError(with: rCode, path: path, data: data)
|
||||
completionHandler(previousResult, responseError)
|
||||
return
|
||||
}
|
||||
|
||||
let (newFiles, err, newToken) = pageHandler(data, progress)
|
||||
if let error = err {
|
||||
completionHandler(previousResult, error)
|
||||
return
|
||||
}
|
||||
let files = previousResult + newFiles
|
||||
if let newToken = newToken, !progress.isCancelled {
|
||||
_ = self.paginated(path, startToken: newToken, currentProgress: progress, previousResult: files, requestHandler: requestHandler, pageHandler: pageHandler, completionHandler: completionHandler)
|
||||
} else {
|
||||
completionHandler(files, nil)
|
||||
}
|
||||
|
||||
})
|
||||
progress.cancellationHandler = { [weak task] in
|
||||
task?.cancel()
|
||||
}
|
||||
progress.setUserInfoObject(Date(), forKey: .startingTimeKey)
|
||||
task.resume()
|
||||
}
|
||||
// codebeat:enable[ARITY]
|
||||
|
||||
internal var maxUploadSimpleSupported: Int64 { return Int64.max }
|
||||
|
||||
func upload_task(_ targetPath: String, progress: Progress, task: URLSessionTask, operation: FileOperationType,
|
||||
completionHandler: SimpleCompletionHandler) -> Void {
|
||||
var progress = progress
|
||||
|
||||
var allData = Data()
|
||||
dataCompletionHandlersForTasks[session.sessionDescription!]?[task.taskIdentifier] = { data in
|
||||
allData.append(data)
|
||||
}
|
||||
|
||||
completionHandlersForTasks[self.session.sessionDescription!]?[task.taskIdentifier] = { [weak self] error in
|
||||
var responseError: FileProviderHTTPError?
|
||||
if let code = (task.response as? HTTPURLResponse)?.statusCode , code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) {
|
||||
responseError = self?.serverError(with: rCode, path: targetPath, data: allData)
|
||||
}
|
||||
if !(responseError == nil && error == nil) {
|
||||
progress.cancel()
|
||||
}
|
||||
completionHandler?(responseError ?? error)
|
||||
self?.delegateNotify(operation, error: responseError ?? error)
|
||||
}
|
||||
task.taskDescription = operation.json
|
||||
task.addObserver(self.sessionDelegate!, forKeyPath: #keyPath(URLSessionTask.countOfBytesSent), options: .new, context: &progress)
|
||||
progress.cancellationHandler = { [weak task] in
|
||||
task?.cancel()
|
||||
}
|
||||
progress.setUserInfoObject(Date(), forKey: .startingTimeKey)
|
||||
task.resume()
|
||||
}
|
||||
|
||||
func upload_data(_ targetPath: String, request: URLRequest, data: Data, operation: FileOperationType,
|
||||
completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
let size: Int64 = Int64(data.count)
|
||||
if size > maxUploadSimpleSupported {
|
||||
let error = self.serverError(with: .payloadTooLarge, path: targetPath, data: nil)
|
||||
completionHandler?(error)
|
||||
self.delegateNotify(operation, error: error)
|
||||
return nil
|
||||
}
|
||||
|
||||
let progress = Progress(totalUnitCount: size)
|
||||
progress.setUserInfoObject(operation, forKey: .fileProvderOperationTypeKey)
|
||||
progress.kind = .file
|
||||
progress.setUserInfoObject(Progress.FileOperationKind.downloading, forKey: .fileOperationKindKey)
|
||||
|
||||
let task = session.uploadTask(with: request, from: data)
|
||||
self.upload_task(targetPath, progress: progress, task: task, operation: operation, completionHandler: completionHandler)
|
||||
|
||||
return progress
|
||||
}
|
||||
|
||||
func upload_file(_ targetPath: String, request: URLRequest, localFile: URL, operation: FileOperationType,
|
||||
completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
let fSize = (try? localFile.resourceValues(forKeys: [.fileSizeKey]))?.fileSize
|
||||
let size = Int64(fSize ?? -1)
|
||||
if size > maxUploadSimpleSupported {
|
||||
let error = self.serverError(with: .payloadTooLarge, path: targetPath, data: nil)
|
||||
completionHandler?(error)
|
||||
self.delegateNotify(operation, error: error)
|
||||
return nil
|
||||
}
|
||||
|
||||
let progress = Progress(totalUnitCount: size)
|
||||
progress.setUserInfoObject(operation, forKey: .fileProvderOperationTypeKey)
|
||||
progress.kind = .file
|
||||
progress.setUserInfoObject(Progress.FileOperationKind.downloading, forKey: .fileOperationKindKey)
|
||||
|
||||
var error: NSError?
|
||||
NSFileCoordinator().coordinate(readingItemAt: localFile, options: .forUploading, error: &error, byAccessor: { (url) in
|
||||
let task = self.session.uploadTask(with: request, fromFile: localFile)
|
||||
self.upload_task(targetPath, progress: progress, task: task, operation: operation, completionHandler: completionHandler)
|
||||
})
|
||||
if let error = error {
|
||||
completionHandler?(error)
|
||||
}
|
||||
|
||||
return progress
|
||||
}
|
||||
|
||||
internal func download_progressive(path: String, request: URLRequest, operation: FileOperationType,
|
||||
responseHandler: ((_ response: URLResponse) -> Void)? = nil,
|
||||
progressHandler: @escaping (_ data: Data) -> Void,
|
||||
completionHandler: @escaping (_ error: Error?) -> Void) -> Progress? {
|
||||
var progress = Progress(totalUnitCount: -1)
|
||||
progress.setUserInfoObject(operation, forKey: .fileProvderOperationTypeKey)
|
||||
progress.kind = .file
|
||||
progress.setUserInfoObject(Progress.FileOperationKind.downloading, forKey: .fileOperationKindKey)
|
||||
|
||||
let task = session.dataTask(with: request)
|
||||
if let responseHandler = responseHandler {
|
||||
responseCompletionHandlersForTasks[session.sessionDescription!]?[task.taskIdentifier] = { response in
|
||||
responseHandler(response)
|
||||
}
|
||||
}
|
||||
|
||||
dataCompletionHandlersForTasks[session.sessionDescription!]?[task.taskIdentifier] = { [weak task, weak self] data in
|
||||
task.flatMap { self?.delegateNotify(operation, progress: Double($0.countOfBytesReceived) / Double($0.countOfBytesExpectedToReceive)) }
|
||||
progressHandler(data)
|
||||
}
|
||||
|
||||
completionHandlersForTasks[session.sessionDescription!]?[task.taskIdentifier] = { error in
|
||||
if error != nil {
|
||||
progress.cancel()
|
||||
}
|
||||
completionHandler(error)
|
||||
self.delegateNotify(operation, error: error)
|
||||
}
|
||||
|
||||
task.taskDescription = operation.json
|
||||
task.addObserver(sessionDelegate!, forKeyPath: #keyPath(URLSessionTask.countOfBytesReceived), options: .new, context: &progress)
|
||||
task.addObserver(sessionDelegate!, forKeyPath: #keyPath(URLSessionTask.countOfBytesExpectedToReceive), options: .new, context: &progress)
|
||||
progress.cancellationHandler = { [weak task] in
|
||||
task?.cancel()
|
||||
}
|
||||
progress.setUserInfoObject(Date(), forKey: .startingTimeKey)
|
||||
task.resume()
|
||||
return progress
|
||||
}
|
||||
|
||||
internal func download_simple(path: String, request: URLRequest, operation: FileOperationType,
|
||||
completionHandler: @escaping ((_ tempURL: URL?, _ error: Error?) -> Void)) -> Progress? {
|
||||
var progress = Progress(totalUnitCount: -1)
|
||||
progress.setUserInfoObject(operation, forKey: .fileProvderOperationTypeKey)
|
||||
progress.kind = .file
|
||||
progress.setUserInfoObject(Progress.FileOperationKind.downloading, forKey: .fileOperationKindKey)
|
||||
|
||||
let task = session.downloadTask(with: request)
|
||||
completionHandlersForTasks[session.sessionDescription!]?[task.taskIdentifier] = { error in
|
||||
if error != nil {
|
||||
progress.cancel()
|
||||
}
|
||||
completionHandler(nil, error)
|
||||
self.delegateNotify(operation, error: error)
|
||||
}
|
||||
downloadCompletionHandlersForTasks[session.sessionDescription!]?[task.taskIdentifier] = { tempURL in
|
||||
guard let httpResponse = task.response as? HTTPURLResponse , httpResponse.statusCode < 300 else {
|
||||
let code = FileProviderHTTPErrorCode(rawValue: (task.response as? HTTPURLResponse)?.statusCode ?? -1)
|
||||
let errorData : Data? = try? Data(contentsOf: tempURL)
|
||||
let serverError = code.flatMap { self.serverError(with: $0, path: path, data: errorData) }
|
||||
if serverError != nil {
|
||||
progress.cancel()
|
||||
}
|
||||
completionHandler(nil, serverError)
|
||||
self.delegateNotify(operation)
|
||||
return
|
||||
}
|
||||
|
||||
completionHandler(tempURL, nil)
|
||||
}
|
||||
task.taskDescription = operation.json
|
||||
task.addObserver(sessionDelegate!, forKeyPath: #keyPath(URLSessionTask.countOfBytesReceived), options: .new, context: &progress)
|
||||
task.addObserver(sessionDelegate!, forKeyPath: #keyPath(URLSessionTask.countOfBytesExpectedToReceive), options: .new, context: &progress)
|
||||
progress.cancellationHandler = { [weak task] in
|
||||
task?.cancel()
|
||||
}
|
||||
progress.setUserInfoObject(Date(), forKey: .startingTimeKey)
|
||||
task.resume()
|
||||
return progress
|
||||
}
|
||||
}
|
||||
|
||||
extension HTTPFileProvider: FileProvider { }
|
||||
+271
-181
@@ -14,10 +14,9 @@ import Foundation
|
||||
|
||||
it uses `FileManager` foundation class with some additions like searching and reading a portion of file.
|
||||
*/
|
||||
open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndoable {
|
||||
open class LocalFileProvider: NSObject, FileProvider, FileProviderMonitor {
|
||||
open class var type: String { return "Local" }
|
||||
open fileprivate(set) var baseURL: URL?
|
||||
open var currentPath: String
|
||||
open var dispatch_queue: DispatchQueue
|
||||
open var operation_queue: OperationQueue
|
||||
open weak var delegate: FileProviderDelegate?
|
||||
@@ -29,8 +28,9 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
|
||||
open private(set) var opFileManager = FileManager()
|
||||
fileprivate var fileProviderManagerDelegate: LocalFileProviderManagerDelegate? = nil
|
||||
|
||||
#if os(macOS) || os(iOS) || os(tvOS)
|
||||
open var undoManager: UndoManager? = nil
|
||||
|
||||
|
||||
/**
|
||||
Forces file operations to use `NSFileCoordinating`, should be set `true` if:
|
||||
- Files are on ubiquity (iCloud) container.
|
||||
@@ -41,6 +41,7 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
|
||||
otherwise it's `false` to accelerate operations.
|
||||
*/
|
||||
open var isCoorinating: Bool
|
||||
#endif
|
||||
|
||||
/**
|
||||
Initializes provider for the specified common directory in the requested domains.
|
||||
@@ -54,6 +55,7 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
|
||||
self.init(baseURL: FileManager.default.urls(for: directory, in: domainMask).first!)
|
||||
}
|
||||
|
||||
#if os(macOS) || os(iOS) || os(tvOS)
|
||||
/**
|
||||
Failable initializer for the specified shared container directory, allows data and files to be shared among app
|
||||
and extensions regarding sandbox requirements. Container ID is same with app group specified in project `Capabilities`
|
||||
@@ -90,6 +92,7 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
|
||||
|
||||
try? fileManager.createDirectory(at: finalBaseURL, withIntermediateDirectories: true)
|
||||
}
|
||||
#endif
|
||||
|
||||
/// Initializes provider for the specified local URL.
|
||||
///
|
||||
@@ -98,31 +101,43 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
|
||||
guard baseURL.isFileURL else {
|
||||
fatalError("Cannot initialize a Local provider from remote URL.")
|
||||
}
|
||||
self.baseURL = baseURL
|
||||
self.currentPath = ""
|
||||
self.baseURL = URL(fileURLWithPath: baseURL.path, isDirectory: true)
|
||||
self.credential = nil
|
||||
self.isCoorinating = false
|
||||
|
||||
dispatch_queue = DispatchQueue(label: "FileProvider.\(type(of: self).type)", attributes: .concurrent)
|
||||
#if swift(>=3.1)
|
||||
let queueLabel = "FileProvider.\(Swift.type(of: self).type)"
|
||||
#else
|
||||
let queueLabel = "FileProvider.\(type(of: self).type)"
|
||||
#endif
|
||||
dispatch_queue = DispatchQueue(label: queueLabel, attributes: .concurrent)
|
||||
operation_queue = OperationQueue()
|
||||
operation_queue.name = "FileProvider.\(type(of: self).type).Operation"
|
||||
operation_queue.name = "\(queueLabel).Operation"
|
||||
|
||||
super.init()
|
||||
|
||||
fileProviderManagerDelegate = LocalFileProviderManagerDelegate(provider: self)
|
||||
opFileManager.delegate = fileProviderManagerDelegate
|
||||
}
|
||||
|
||||
public required convenience init?(coder aDecoder: NSCoder) {
|
||||
guard let baseURL = aDecoder.decodeObject(forKey: "baseURL") as? URL else {
|
||||
guard let baseURL = aDecoder.decodeObject(of: NSURL.self, forKey: "baseURL") as URL? else {
|
||||
return nil
|
||||
}
|
||||
self.init(baseURL: baseURL)
|
||||
self.currentPath = aDecoder.decodeObject(forKey: "currentPath") as? String ?? ""
|
||||
self.isCoorinating = aDecoder.decodeBool(forKey: "isCoorinating")
|
||||
}
|
||||
|
||||
deinit {
|
||||
let monitors = self.monitors
|
||||
self.monitors = []
|
||||
for monitor in monitors {
|
||||
monitor.stop()
|
||||
}
|
||||
}
|
||||
|
||||
open func encode(with aCoder: NSCoder) {
|
||||
aCoder.encode(self.baseURL, forKey: "currentPath")
|
||||
aCoder.encode(self.currentPath, forKey: "currentPath")
|
||||
aCoder.encode(self.baseURL, forKey: "baseURL")
|
||||
aCoder.encode(self.isCoorinating, forKey: "isCoorinating")
|
||||
}
|
||||
|
||||
@@ -132,139 +147,155 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
|
||||
|
||||
public func copy(with zone: NSZone? = nil) -> Any {
|
||||
let copy = LocalFileProvider(baseURL: self.baseURL!)
|
||||
copy.currentPath = self.currentPath
|
||||
copy.undoManager = self.undoManager
|
||||
#if os(macOS) || os(iOS) || os(tvOS)
|
||||
copy.isCoorinating = self.isCoorinating
|
||||
#endif
|
||||
copy.delegate = self.delegate
|
||||
copy.fileOperationDelegate = self.fileOperationDelegate
|
||||
return copy
|
||||
}
|
||||
|
||||
/// **OBSOLETED:** No longer is in use and overriding this method has no effect anymore.
|
||||
@available(*, obsoleted: 1.0, message: "Overriding this method has no effect anymore.")
|
||||
open class func defaultBaseURL() -> URL {
|
||||
return FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
|
||||
}
|
||||
|
||||
open func contentsOfDirectory(path: String, completionHandler: @escaping ((_ contents: [FileObject], _ error: Error?) -> Void)) {
|
||||
open func contentsOfDirectory(path: String, completionHandler: @escaping (_ contents: [FileObject], _ error: Error?) -> Void) {
|
||||
dispatch_queue.async {
|
||||
do {
|
||||
let contents = try self.fileManager.contentsOfDirectory(at: self.url(of: path), includingPropertiesForKeys: nil, options: .skipsSubdirectoryDescendants)
|
||||
let filesAttributes = contents.flatMap({ (fileURL) -> LocalFileObject? in
|
||||
let filesAttributes = contents.compactMap({ (fileURL) -> LocalFileObject? in
|
||||
let path = self.relativePathOf(url: fileURL)
|
||||
return LocalFileObject(fileWithPath: path, relativeTo: self.baseURL)
|
||||
})
|
||||
completionHandler(filesAttributes, nil)
|
||||
} catch let e {
|
||||
completionHandler([], e)
|
||||
} catch {
|
||||
completionHandler([], error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
open func attributesOfItem(path: String, completionHandler: @escaping ((_ attributes: FileObject?, _ error: Error?) -> Void)) {
|
||||
open func attributesOfItem(path: String, completionHandler: @escaping (_ attributes: FileObject?, _ error: Error?) -> Void) {
|
||||
dispatch_queue.async {
|
||||
completionHandler(LocalFileObject(fileWithPath: path, relativeTo: self.baseURL), nil)
|
||||
}
|
||||
}
|
||||
|
||||
open func storageProperties(completionHandler: (@escaping (_ total: Int64, _ used: Int64) -> Void)) {
|
||||
let values = try? baseURL?.resourceValues(forKeys: [.volumeTotalCapacityKey, .volumeAvailableCapacityKey])
|
||||
let totalSize = Int64(values??.volumeTotalCapacity ?? -1)
|
||||
let freeSize = Int64(values??.volumeAvailableCapacity ?? 0)
|
||||
completionHandler(totalSize, totalSize - freeSize)
|
||||
public func storageProperties(completionHandler: @escaping (_ volumeInfo: VolumeObject?) -> Void) {
|
||||
dispatch_queue.async {
|
||||
var keys: Set<URLResourceKey> = [.volumeTotalCapacityKey, .volumeAvailableCapacityKey, .volumeURLKey, .volumeNameKey, .volumeIsReadOnlyKey, .volumeCreationDateKey]
|
||||
if #available(iOS 10.0, macOS 10.12, tvOS 10.0, *) {
|
||||
keys.insert(.isEncryptedKey)
|
||||
}
|
||||
let values: URLResourceValues? = self.baseURL.flatMap { try? $0.resourceValues(forKeys: keys) }
|
||||
completionHandler(values.flatMap({ VolumeObject(allValues: $0.allValues) }))
|
||||
}
|
||||
}
|
||||
|
||||
open func searchFiles(path: String, recursive: Bool, query: NSPredicate, foundItemHandler: ((FileObject) -> Void)?, completionHandler: @escaping ((_ files: [FileObject], _ error: Error?) -> Void)) {
|
||||
@discardableResult
|
||||
open func searchFiles(path: String, recursive: Bool, query: NSPredicate, foundItemHandler: ((FileObject) -> Void)?, completionHandler: @escaping (_ files: [FileObject], _ error: Error?) -> Void) -> Progress? {
|
||||
let progress = Progress(totalUnitCount: -1)
|
||||
progress.setUserInfoObject(self.url(of: path), forKey: .fileURLKey)
|
||||
|
||||
dispatch_queue.async {
|
||||
progress.setUserInfoObject(Date(), forKey: .startingTimeKey)
|
||||
let iterator = self.fileManager.enumerator(at: self.url(of: path), includingPropertiesForKeys: nil, options: recursive ? [] : [.skipsSubdirectoryDescendants, .skipsPackageDescendants]) { (url, e) -> Bool in
|
||||
completionHandler([], e)
|
||||
return true
|
||||
}
|
||||
var result = [LocalFileObject]()
|
||||
while let fileURL = iterator?.nextObject() as? URL {
|
||||
if progress.isCancelled {
|
||||
break
|
||||
}
|
||||
let path = self.relativePathOf(url: fileURL)
|
||||
if let fileObject = LocalFileObject(fileWithPath: path, relativeTo: self.baseURL), query.evaluate(with: fileObject.mapPredicate()) {
|
||||
result.append(fileObject)
|
||||
progress.completedUnitCount = Int64(result.count)
|
||||
foundItemHandler?(fileObject)
|
||||
}
|
||||
}
|
||||
completionHandler(result, nil)
|
||||
}
|
||||
|
||||
return progress
|
||||
}
|
||||
|
||||
open func isReachable(completionHandler: @escaping (Bool) -> Void) {
|
||||
open func isReachable(completionHandler: @escaping (_ success: Bool, _ error: Error?) -> Void) {
|
||||
dispatch_queue.async {
|
||||
completionHandler(self.fileManager.isReadableFile(atPath: self.baseURL!.path))
|
||||
do {
|
||||
let isReachable = try self.baseURL!.checkResourceIsReachable()
|
||||
completionHandler(isReachable, nil)
|
||||
} catch {
|
||||
completionHandler(false, error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
open func relativePathOf(url: URL) -> String {
|
||||
// check if url derieved from current base url
|
||||
let relativePath = url.relativePath
|
||||
if !relativePath.isEmpty, url.baseURL == self.baseURL {
|
||||
return (relativePath.removingPercentEncoding ?? relativePath).replacingOccurrences(of: "/", with: "", options: .anchored)
|
||||
}
|
||||
|
||||
guard let baseURL = self.baseURL?.standardizedFileURL else { return url.absoluteString }
|
||||
let standardPath = url.absoluteString.replacingOccurrences(of: "file:///private/var/", with: "file:///var/", options: .anchored)
|
||||
let standardBase = baseURL.absoluteString.replacingOccurrences(of: "file:///private/var/", with: "file:///var/", options: .anchored)
|
||||
let standardRelativePath = standardPath.replacingOccurrences(of: standardBase, with: "/").replacingOccurrences(of: "/", with: "", options: .anchored)
|
||||
return standardRelativePath.removingPercentEncoding ?? standardRelativePath
|
||||
}
|
||||
|
||||
open weak var fileOperationDelegate : FileOperationDelegate?
|
||||
|
||||
@discardableResult
|
||||
open func create(folder folderName: String, at atPath: String, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
let opType = FileOperationType.create(path: (atPath as NSString).appendingPathComponent(folderName) + "/")
|
||||
return self.doOperation(opType, completionHandler: completionHandler)
|
||||
open func create(folder folderName: String, at atPath: String, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
let operation = FileOperationType.create(path: (atPath as NSString).appendingPathComponent(folderName) + "/")
|
||||
return self.doOperation(operation, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func moveItem(path: String, to toPath: String, overwrite: Bool = false, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
let opType = FileOperationType.move(source: path, destination: toPath)
|
||||
|
||||
if !overwrite && self.fileManager.fileExists(atPath: self.url(of: toPath).path) {
|
||||
completionHandler?(self.throwError(toPath, code: CocoaError.fileWriteFileExists as FoundationErrorEnum))
|
||||
return nil
|
||||
}
|
||||
|
||||
return self.doOperation(opType, completionHandler: completionHandler)
|
||||
open func moveItem(path: String, to toPath: String, overwrite: Bool = false, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
let operation = FileOperationType.move(source: path, destination: toPath)
|
||||
return self.doOperation(operation, overwrite: overwrite, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func copyItem(path: String, to toPath: String, overwrite: Bool = false, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
let opType = FileOperationType.copy(source: path, destination: toPath)
|
||||
|
||||
if !overwrite && self.fileManager.fileExists(atPath: self.url(of: toPath).path) {
|
||||
self.dispatch_queue.async {
|
||||
completionHandler?(self.throwError(toPath, code: CocoaError.fileWriteFileExists as FoundationErrorEnum))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
return self.doOperation(opType, completionHandler: completionHandler)
|
||||
open func copyItem(path: String, to toPath: String, overwrite: Bool = false, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
let operation = FileOperationType.copy(source: path, destination: toPath)
|
||||
return self.doOperation(operation, overwrite: overwrite, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func removeItem(path: String, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
let opType = FileOperationType.remove(path: path)
|
||||
return self.doOperation(opType, completionHandler: completionHandler)
|
||||
open func removeItem(path: String, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
let operation = FileOperationType.remove(path: path)
|
||||
return self.doOperation(operation, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func copyItem(localFile: URL, to toPath: String, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
if !overwrite && self.fileManager.fileExists(atPath: self.url(of: toPath).path) {
|
||||
self.dispatch_queue.async {
|
||||
completionHandler?(self.throwError(toPath, code: CocoaError.fileWriteFileExists as FoundationErrorEnum))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
let opType = FileOperationType.copy(source: localFile.absoluteString, destination: toPath)
|
||||
return self.doOperation(opType, forUploading: true, completionHandler: completionHandler)
|
||||
open func copyItem(localFile: URL, to toPath: String, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
let operation = FileOperationType.copy(source: localFile.absoluteString, destination: toPath)
|
||||
return self.doOperation(operation, overwrite: overwrite, forUploading: true, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func copyItem(path: String, toLocalURL: URL, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
let opType = FileOperationType.copy(source: path, destination: toLocalURL.absoluteString)
|
||||
return self.doOperation(opType, completionHandler: completionHandler)
|
||||
open func copyItem(path: String, toLocalURL: URL, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
let operation = FileOperationType.copy(source: path, destination: toLocalURL.absoluteString)
|
||||
return self.doOperation(operation, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
dynamic func doSimpleOperation(_ box: UndoBox) {
|
||||
#if os(macOS) || os(iOS) || os(tvOS)
|
||||
@objc dynamic func doSimpleOperation(_ box: UndoBox) {
|
||||
guard let _ = self.undoManager else { return }
|
||||
_ = self.doOperation(box.undoOperation) { (_) in
|
||||
return
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@discardableResult
|
||||
fileprivate func doOperation(_ opType: FileOperationType, data: Data? = nil, atomically: Bool = false, forUploading: Bool = false, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
fileprivate func doOperation(_ operation: FileOperationType, data: Data? = nil, overwrite: Bool = true, atomically: Bool = false, forUploading: Bool = false, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
let progress = Progress(totalUnitCount: -1)
|
||||
progress.setUserInfoObject(operation, forKey: .fileProvderOperationTypeKey)
|
||||
progress.kind = .file
|
||||
progress.isCancellable = false
|
||||
progress.setUserInfoObject(Progress.FileOperationKind.receiving, forKey: .fileOperationKindKey)
|
||||
|
||||
func urlofpath(path: String) -> URL {
|
||||
if path.hasPrefix("file://") {
|
||||
@@ -276,76 +307,96 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
|
||||
}
|
||||
}
|
||||
|
||||
guard let sourcePath = opType.source else { return nil }
|
||||
let destPath = opType.destination
|
||||
let sourcePath = operation.source
|
||||
let destPath = operation.destination
|
||||
let source: URL = urlofpath(path: sourcePath)
|
||||
progress.setUserInfoObject(source, forKey: .fileURLKey)
|
||||
|
||||
let dest: URL?
|
||||
if let destPath = destPath {
|
||||
dest = urlofpath(path: destPath)
|
||||
} else {
|
||||
dest = nil
|
||||
let dest = destPath.map(urlofpath(path:))
|
||||
|
||||
if !overwrite, let dest = dest, /* fileExists */ ((try? dest.checkResourceIsReachable()) ?? false) ||
|
||||
((try? dest.checkPromisedItemIsReachable()) ?? false) {
|
||||
let e = self.cocoaError(destPath!, code: .fileWriteFileExists)
|
||||
dispatch_queue.async {
|
||||
completionHandler?(e)
|
||||
}
|
||||
self.delegateNotify(operation, error: e)
|
||||
return nil
|
||||
}
|
||||
|
||||
if let undoManager = self.undoManager, let undoOp = self.undoOperation(for: opType) {
|
||||
let undoBox = UndoBox(provider: self, operation: opType, undoOperation: undoOp)
|
||||
#if os(macOS) || os(iOS) || os(tvOS)
|
||||
if let undoManager = self.undoManager, let undoOp = self.undoOperation(for: operation) {
|
||||
let undoBox = UndoBox(provider: self, operation: operation, undoOperation: undoOp)
|
||||
undoManager.beginUndoGrouping()
|
||||
undoManager.registerUndo(withTarget: self, selector: #selector(LocalFileProvider.doSimpleOperation(_:)), object: undoBox)
|
||||
undoManager.setActionName(opType.actionDescription)
|
||||
undoManager.setActionName(operation.actionDescription)
|
||||
undoManager.endUndoGrouping()
|
||||
}
|
||||
|
||||
var successfulSecurityScopedResourceAccess = false
|
||||
#endif
|
||||
|
||||
let operationHandler: (URL, URL?) -> Void = { source, dest in
|
||||
do {
|
||||
switch opType {
|
||||
progress.setUserInfoObject(Date(), forKey: .startingTimeKey)
|
||||
switch operation {
|
||||
case .create:
|
||||
if sourcePath.hasSuffix("/") {
|
||||
progress.totalUnitCount = 1
|
||||
try self.opFileManager.createDirectory(at: source, withIntermediateDirectories: true, attributes: [:])
|
||||
} else {
|
||||
progress.totalUnitCount = Int64(data?.count ?? -1)
|
||||
try data?.write(to: source, options: .atomic)
|
||||
}
|
||||
case .modify:
|
||||
progress.totalUnitCount = Int64(data?.count ?? -1)
|
||||
try data?.write(to: source, options: atomically ? [.atomic] : [])
|
||||
case .copy:
|
||||
guard let dest = dest else { return }
|
||||
progress.setUserInfoObject(Progress.FileOperationKind.copying, forKey: .fileOperationKindKey)
|
||||
progress.totalUnitCount = abs(source.fileSize)
|
||||
try self.opFileManager.copyItem(at: source, to: dest)
|
||||
case .move:
|
||||
progress.setUserInfoObject(Progress.FileOperationKind.copying, forKey: .fileOperationKindKey)
|
||||
guard let dest = dest else { return }
|
||||
progress.totalUnitCount = abs(source.fileSize)
|
||||
try self.opFileManager.moveItem(at: source, to: dest)
|
||||
case.remove:
|
||||
progress.totalUnitCount = abs(source.fileSize)
|
||||
try self.opFileManager.removeItem(at: source)
|
||||
default:
|
||||
return
|
||||
}
|
||||
#if os(macOS) || os(iOS) || os(tvOS)
|
||||
if successfulSecurityScopedResourceAccess {
|
||||
source.stopAccessingSecurityScopedResource()
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
progress.completedUnitCount = progress.totalUnitCount
|
||||
self.dispatch_queue.async {
|
||||
completionHandler?(nil)
|
||||
}
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.fileproviderSucceed(self, operation: opType)
|
||||
}
|
||||
} catch let e {
|
||||
self.delegateNotify(operation)
|
||||
} catch {
|
||||
#if os(macOS) || os(iOS) || os(tvOS)
|
||||
if successfulSecurityScopedResourceAccess {
|
||||
source.stopAccessingSecurityScopedResource()
|
||||
}
|
||||
#endif
|
||||
progress.cancel()
|
||||
self.dispatch_queue.async {
|
||||
completionHandler?(e)
|
||||
}
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.fileproviderFailed(self, operation: opType)
|
||||
completionHandler?(error)
|
||||
}
|
||||
self.delegateNotify(operation, error: error)
|
||||
}
|
||||
}
|
||||
|
||||
#if os(macOS) || os(iOS) || os(tvOS)
|
||||
if isCoorinating {
|
||||
successfulSecurityScopedResourceAccess = source.startAccessingSecurityScopedResource()
|
||||
var intents = [NSFileAccessIntent]()
|
||||
switch opType {
|
||||
switch operation {
|
||||
case .create, .modify:
|
||||
intents.append(NSFileAccessIntent.writingIntent(with: source, options: .forReplacing))
|
||||
case .copy:
|
||||
@@ -361,144 +412,182 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
self.coordinated(intents: intents, completionHandler: operationHandler, errorHandler: { error in
|
||||
self.coordinated(intents: intents, moving: true, operationHandler: operationHandler, errorHandler: { error in
|
||||
self.dispatch_queue.async {
|
||||
completionHandler?(error)
|
||||
}
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.fileproviderFailed(self, operation: opType)
|
||||
}
|
||||
self.delegateNotify(operation, error: error)
|
||||
})
|
||||
} else {
|
||||
operation_queue.addOperation {
|
||||
operationHandler(source, dest)
|
||||
}
|
||||
}
|
||||
|
||||
return LocalOperationHandle(operationType: opType, baseURL: self.baseURL)
|
||||
#else
|
||||
operation_queue.addOperation {
|
||||
operationHandler(source, dest)
|
||||
}
|
||||
#endif
|
||||
return progress
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func contents(path: String, completionHandler: @escaping ((_ contents: Data?, _ error: Error?) -> Void)) -> OperationHandle? {
|
||||
let opType = FileOperationType.fetch(path: path)
|
||||
open func contents(path: String, completionHandler: @escaping ((_ contents: Data?, _ error: Error?) -> Void)) -> Progress? {
|
||||
let operation = FileOperationType.fetch(path: path)
|
||||
let url = self.url(of: path)
|
||||
|
||||
let progress = Progress(totalUnitCount: url.fileSize)
|
||||
progress.setUserInfoObject(operation, forKey: .fileProvderOperationTypeKey)
|
||||
progress.kind = .file
|
||||
progress.isCancellable = false
|
||||
progress.setUserInfoObject(Progress.FileOperationKind.receiving, forKey: .fileOperationKindKey)
|
||||
progress.setUserInfoObject(url, forKey: .fileURLKey)
|
||||
|
||||
let operationHandler: (URL) -> Void = { url in
|
||||
progress.setUserInfoObject(Date(), forKey: .startingTimeKey)
|
||||
do {
|
||||
let data = try Data(contentsOf: url)
|
||||
progress.completedUnitCount = progress.totalUnitCount
|
||||
self.dispatch_queue.async {
|
||||
completionHandler(data, nil)
|
||||
}
|
||||
} catch let e {
|
||||
self.dispatch_queue.async {
|
||||
completionHandler(nil, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if isCoorinating {
|
||||
let intent = NSFileAccessIntent.readingIntent(with: url, options: .withoutChanges)
|
||||
coordinated(intents: [intent], completionHandler: operationHandler, errorHandler: { error in
|
||||
self.delegateNotify(operation)
|
||||
} catch {
|
||||
progress.cancel()
|
||||
self.dispatch_queue.async {
|
||||
completionHandler(nil, error)
|
||||
}
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.fileproviderFailed(self, operation: opType)
|
||||
self.delegateNotify(operation, error: error)
|
||||
}
|
||||
}
|
||||
|
||||
#if os(macOS) || os(iOS) || os(tvOS)
|
||||
if isCoorinating {
|
||||
let intent = NSFileAccessIntent.readingIntent(with: url, options: .withoutChanges)
|
||||
coordinated(intents: [intent], operationHandler: operationHandler, errorHandler: { error in
|
||||
self.dispatch_queue.async {
|
||||
completionHandler(nil, error)
|
||||
}
|
||||
self.delegateNotify(operation, error: error)
|
||||
})
|
||||
} else {
|
||||
dispatch_queue.async {
|
||||
operationHandler(url)
|
||||
}
|
||||
}
|
||||
|
||||
return LocalOperationHandle(operationType: opType, baseURL: self.baseURL)
|
||||
#else
|
||||
dispatch_queue.async {
|
||||
operationHandler(url)
|
||||
}
|
||||
#endif
|
||||
|
||||
return progress
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func contents(path: String, offset: Int64, length: Int, completionHandler: @escaping ((_ contents: Data?, _ error: Error?) -> Void)) -> OperationHandle? {
|
||||
open func contents(path: String, offset: Int64, length: Int, completionHandler: @escaping (_ contents: Data?, _ error: Error?) -> Void) -> Progress? {
|
||||
if length == 0 || offset < 0 {
|
||||
dispatch_queue.async {
|
||||
completionHandler(Data(), nil)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
if offset == 0 && length < 0 {
|
||||
return self.contents(path: path, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
let opType = FileOperationType.fetch(path: path)
|
||||
|
||||
let operation = FileOperationType.fetch(path: path)
|
||||
let url = self.url(of: path)
|
||||
|
||||
let progress = Progress(totalUnitCount: -1)
|
||||
progress.setUserInfoObject(operation, forKey: .fileProvderOperationTypeKey)
|
||||
progress.kind = .file
|
||||
progress.isCancellable = false
|
||||
progress.setUserInfoObject(url, forKey: .fileURLKey)
|
||||
progress.setUserInfoObject(Progress.FileOperationKind.receiving, forKey: .fileOperationKindKey)
|
||||
|
||||
let operationHandler: (URL) -> Void = { url in
|
||||
guard let handle = FileHandle(forReadingAtPath: url.path) else {
|
||||
self.dispatch_queue.async {
|
||||
completionHandler(nil, self.throwError(path, code: CocoaError.fileNoSuchFile as FoundationErrorEnum))
|
||||
do {
|
||||
guard let handle = FileHandle(forReadingAtPath: url.path) else {
|
||||
throw self.cocoaError(path, code: .fileNoSuchFile)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
defer {
|
||||
handle.closeFile()
|
||||
}
|
||||
|
||||
let size = LocalFileObject(fileWithURL: url)?.size ?? -1
|
||||
guard size > offset else {
|
||||
self.dispatch_queue.async {
|
||||
completionHandler(nil, self.throwError(path, code: CocoaError.fileReadTooLarge as FoundationErrorEnum))
|
||||
|
||||
defer {
|
||||
handle.closeFile()
|
||||
}
|
||||
return
|
||||
}
|
||||
handle.seek(toFileOffset: UInt64(offset))
|
||||
guard Int64(handle.offsetInFile) == offset else {
|
||||
self.dispatch_queue.async {
|
||||
completionHandler(nil, self.throwError(path, code: CocoaError.fileReadTooLarge as FoundationErrorEnum))
|
||||
|
||||
let size = LocalFileObject(fileWithURL: url)?.size ?? -1
|
||||
progress.totalUnitCount = size
|
||||
guard size > offset else {
|
||||
progress.cancel()
|
||||
throw self.cocoaError(path, code: .fileReadTooLarge)
|
||||
}
|
||||
progress.setUserInfoObject(Date(), forKey: .startingTimeKey)
|
||||
handle.seek(toFileOffset: UInt64(offset))
|
||||
guard Int64(handle.offsetInFile) == offset else {
|
||||
progress.cancel()
|
||||
throw self.cocoaError(path, code: .fileReadTooLarge)
|
||||
}
|
||||
|
||||
let data = handle.readData(ofLength: length)
|
||||
progress.completedUnitCount = progress.totalUnitCount
|
||||
self.dispatch_queue.async {
|
||||
completionHandler(data, nil)
|
||||
self.delegateNotify(operation)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
let data = handle.readData(ofLength: length)
|
||||
|
||||
self.dispatch_queue.async {
|
||||
completionHandler(data, nil)
|
||||
catch {
|
||||
self.dispatch_queue.async {
|
||||
completionHandler(nil, error)
|
||||
self.delegateNotify(operation, error: error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if os(macOS) || os(iOS) || os(tvOS)
|
||||
if isCoorinating {
|
||||
let intent = NSFileAccessIntent.readingIntent(with: url, options: .withoutChanges)
|
||||
coordinated(intents: [intent], completionHandler: operationHandler, errorHandler: { error in
|
||||
coordinated(intents: [intent], operationHandler: operationHandler, errorHandler: { error in
|
||||
completionHandler(nil, error)
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.fileproviderFailed(self, operation: opType)
|
||||
}
|
||||
self.delegateNotify(operation, error: error)
|
||||
})
|
||||
} else {
|
||||
dispatch_queue.async {
|
||||
operationHandler(url)
|
||||
}
|
||||
}
|
||||
|
||||
return LocalOperationHandle(operationType: opType, baseURL: self.baseURL)
|
||||
#else
|
||||
dispatch_queue.async {
|
||||
operationHandler(url)
|
||||
}
|
||||
#endif
|
||||
return progress
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func writeContents(path: String, contents data: Data?, atomically: Bool, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
let fileExists = fileManager.fileExists(atPath: url(of: path).path)
|
||||
let opType: FileOperationType = fileExists ? .modify(path: path) : .create(path: path)
|
||||
return self.doOperation(opType, data: data ?? Data(), atomically: atomically, completionHandler: completionHandler)
|
||||
open func writeContents(path: String, contents data: Data?, atomically: Bool, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
let fileExists = ((try? self.url(of: path).checkResourceIsReachable()) ?? false) ||
|
||||
((try? self.url(of: path).checkPromisedItemIsReachable()) ?? false)
|
||||
if !overwrite && fileExists {
|
||||
let e = self.cocoaError(path, code: .fileWriteFileExists)
|
||||
dispatch_queue.async {
|
||||
completionHandler?(e)
|
||||
}
|
||||
self.delegateNotify(.modify(path: path), error: e)
|
||||
return nil
|
||||
}
|
||||
|
||||
let operation: FileOperationType = fileExists ? .modify(path: path) : .create(path: path)
|
||||
return self.doOperation(operation, data: data ?? Data(), atomically: atomically, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
fileprivate var monitors = [LocalFolderMonitor]()
|
||||
fileprivate var monitors = [LocalFileMonitor]()
|
||||
|
||||
open func registerNotifcation(path: String, eventHandler: @escaping (() -> Void)) {
|
||||
self.unregisterNotifcation(path: path)
|
||||
let dirurl = self.url(of: path)
|
||||
let isdir = (try? dirurl.resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory ?? false
|
||||
if !isdir {
|
||||
return
|
||||
}
|
||||
let monitor = LocalFolderMonitor(url: dirurl) {
|
||||
let url = self.url(of: path)
|
||||
let monitor = LocalFileMonitor(url: url) {
|
||||
eventHandler()
|
||||
}
|
||||
monitor.start()
|
||||
@@ -506,7 +595,7 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
|
||||
}
|
||||
|
||||
open func unregisterNotifcation(path: String) {
|
||||
var removedMonitor: LocalFolderMonitor?
|
||||
var removedMonitor: LocalFileMonitor?
|
||||
for (i, monitor) in monitors.enumerated() {
|
||||
if self.relativePathOf(url: monitor.url) == path {
|
||||
removedMonitor = monitors.remove(at: i)
|
||||
@@ -519,9 +608,7 @@ open class LocalFileProvider: FileProvider, FileProviderMonitor, FileProvideUndo
|
||||
open func isRegisteredForNotification(path: String) -> Bool {
|
||||
return monitors.map( { self.relativePathOf(url: $0.url) } ).contains(path.trimmingCharacters(in: CharacterSet(charactersIn: "/")))
|
||||
}
|
||||
}
|
||||
|
||||
public extension LocalFileProvider {
|
||||
|
||||
/**
|
||||
Creates a symbolic link at the specified path that points to an item at the given path.
|
||||
This method does not traverse symbolic links contained in destination path, making it possible
|
||||
@@ -533,19 +620,16 @@ public extension LocalFileProvider {
|
||||
- withDestinationPath: The path that contains the item to be pointed to by the link. In other words, this is the destination of the link.
|
||||
- completionHandler: If an error parameter was provided, a presentable `Error` will be returned.
|
||||
*/
|
||||
public func create(symbolicLink path: String, withDestinationPath destPath: String, completionHandler: SimpleCompletionHandler) {
|
||||
open func create(symbolicLink path: String, withDestinationPath destPath: String, completionHandler: SimpleCompletionHandler) {
|
||||
operation_queue.addOperation {
|
||||
let operation = FileOperationType.link(link: path, target: destPath)
|
||||
do {
|
||||
try self.opFileManager.createSymbolicLink(at: self.url(of: path), withDestinationURL: self.url(of: destPath))
|
||||
completionHandler?(nil)
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.fileproviderSucceed(self, operation: .link(link: path, target: destPath))
|
||||
}
|
||||
} catch let e {
|
||||
completionHandler?(e)
|
||||
DispatchQueue.main.async {
|
||||
self.delegate?.fileproviderFailed(self, operation: .link(link: path, target: destPath))
|
||||
}
|
||||
self.delegateNotify(operation)
|
||||
} catch {
|
||||
completionHandler?(error)
|
||||
self.delegateNotify(operation, error: error)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -555,32 +639,36 @@ public extension LocalFileProvider {
|
||||
/// - Parameters:
|
||||
/// - path: The path of a file or directory.
|
||||
/// - completionHandler: Returns destination url of given symbolic link, or an `Error` object if it fails.
|
||||
public func destination(ofSymbolicLink path: String, completionHandler: @escaping (_ url: URL?, _ error: Error?) -> Void) {
|
||||
open func destination(ofSymbolicLink path: String, completionHandler: @escaping (_ url: URL?, _ error: Error?) -> Void) {
|
||||
dispatch_queue.async {
|
||||
do {
|
||||
let destPath = try self.opFileManager.destinationOfSymbolicLink(atPath: self.url(of: path).path)
|
||||
let destUrl = URL(fileURLWithPath: destPath)
|
||||
completionHandler(destUrl, nil)
|
||||
} catch let e{
|
||||
completionHandler(nil, e)
|
||||
} catch {
|
||||
completionHandler(nil, error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if os(macOS) || os(iOS) || os(tvOS)
|
||||
|
||||
extension LocalFileProvider: FileProvideUndoable { }
|
||||
|
||||
internal extension LocalFileProvider {
|
||||
func coordinated(intents: [NSFileAccessIntent], completionHandler: @escaping (_ url: URL) -> Void, errorHandler: ((_ error: Error) -> Void)? = nil) {
|
||||
func coordinated(intents: [NSFileAccessIntent], operationHandler: @escaping (_ url: URL) -> Void, errorHandler: ((_ error: Error) -> Void)? = nil) {
|
||||
let coordinator = NSFileCoordinator(filePresenter: nil)
|
||||
coordinator.coordinate(with: intents, queue: operation_queue) { (error) in
|
||||
if let error = error {
|
||||
errorHandler?(error)
|
||||
return
|
||||
}
|
||||
completionHandler(intents.first!.url)
|
||||
operationHandler(intents.first!.url)
|
||||
}
|
||||
}
|
||||
|
||||
func coordinated(intents: [NSFileAccessIntent], moving: Bool = false, completionHandler: @escaping (_ sourceUrl: URL, _ destURL: URL?) -> Void, errorHandler: ((_ error: Error) -> Void)? = nil) {
|
||||
func coordinated(intents: [NSFileAccessIntent], moving: Bool = false, operationHandler: @escaping (_ sourceUrl: URL, _ destURL: URL?) -> Void, errorHandler: ((_ error: Error) -> Void)? = nil) {
|
||||
let coordinator = NSFileCoordinator(filePresenter: nil)
|
||||
coordinator.coordinate(with: intents, queue: operation_queue) { (error) in
|
||||
if let error = error {
|
||||
@@ -592,10 +680,12 @@ internal extension LocalFileProvider {
|
||||
if moving, let newDest = newDest {
|
||||
coordinator.item(at: newSource, willMoveTo: newDest)
|
||||
}
|
||||
completionHandler(newSource, newDest)
|
||||
operationHandler(newSource, newDest)
|
||||
if moving, let newDest = newDest {
|
||||
coordinator.item(at: newSource, didMoveTo: newDest)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
+26
-110
@@ -10,21 +10,19 @@ import Foundation
|
||||
|
||||
/// Containts path, url and attributes of a local file or resource.
|
||||
public final class LocalFileObject: FileObject {
|
||||
internal override init(url: URL, name: String, path: String) {
|
||||
internal override init(url: URL?, name: String, path: String) {
|
||||
super.init(url: url, name: name, path: path)
|
||||
}
|
||||
|
||||
/// Initiates a `LocalFileObject` with attributes of file in path.
|
||||
public convenience init? (fileWithPath path: String, relativeTo relativeURL: URL?) {
|
||||
var fileURL: URL?
|
||||
var rpath = path.replacingOccurrences(of: relativeURL?.path ?? "", with: "", options: .anchored)
|
||||
if relativeURL != nil && rpath.hasPrefix("/") {
|
||||
rpath.remove(at: rpath.startIndex)
|
||||
}
|
||||
if #available(iOS 9.0, macOS 10.11, tvOS 9.0, *) {
|
||||
var rpath = path.replacingOccurrences(of: relativeURL?.path ?? "", with: "", options: .anchored).replacingOccurrences(of: "/", with: "", options: .anchored)
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
fileURL = URL(fileURLWithPath: rpath, relativeTo: relativeURL)
|
||||
} else {
|
||||
fileURL = URL(string: rpath.isEmpty ? "./" : rpath, relativeTo: relativeURL)
|
||||
rpath = rpath.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? rpath
|
||||
fileURL = URL(string: rpath, relativeTo: relativeURL) ?? relativeURL
|
||||
}
|
||||
|
||||
if let fileURL = fileURL {
|
||||
@@ -79,21 +77,33 @@ public final class LocalFileObject: FileObject {
|
||||
return data?.map { String(format: "%02hhx", $0) }.joined()
|
||||
}
|
||||
}
|
||||
|
||||
/// Count of children items of a driectory. It costs disk access for local directories.
|
||||
open public(set) override var childrensCount: Int? {
|
||||
get {
|
||||
return try? FileManager.default.contentsOfDirectory(atPath: self.url.path).count
|
||||
}
|
||||
set {
|
||||
//
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal final class LocalFolderMonitor {
|
||||
public final class LocalFileMonitor {
|
||||
fileprivate let source: DispatchSourceFileSystemObject
|
||||
fileprivate let descriptor: CInt
|
||||
fileprivate let qq: DispatchQueue = DispatchQueue.global(qos: .default)
|
||||
fileprivate var state: Bool = false
|
||||
fileprivate var monitoredTime: TimeInterval = Date().timeIntervalSinceReferenceDate
|
||||
var url: URL
|
||||
public var url: URL
|
||||
|
||||
/// Creates a folder monitor object with monitoring enabled.
|
||||
init(url: URL, handler: @escaping ()->Void) {
|
||||
public init(url: URL, handler: @escaping ()->Void) {
|
||||
self.url = url
|
||||
descriptor = open((url as NSURL).fileSystemRepresentation, O_EVTONLY)
|
||||
source = DispatchSource.makeFileSystemObjectSource(fileDescriptor: descriptor, eventMask: .write, queue: qq)
|
||||
descriptor = open((url.absoluteURL as NSURL).fileSystemRepresentation, O_EVTONLY)
|
||||
let event: DispatchSource.FileSystemEvent = url.fileIsDirectory ? [.write] : .all
|
||||
source = DispatchSource.makeFileSystemObjectSource(fileDescriptor: descriptor, eventMask: event, queue: qq)
|
||||
|
||||
// Folder monitoring is recursive and deep. Monitoring a root folder may be very costly
|
||||
// We have a 0.2 second delay to ensure we wont call handler 1000s times when there is
|
||||
// a huge file operation. This ensures app will work smoothly while this 250 milisec won't
|
||||
@@ -118,7 +128,7 @@ internal final class LocalFolderMonitor {
|
||||
}
|
||||
|
||||
/// Starts sending notifications if currently stopped
|
||||
func start() {
|
||||
public func start() {
|
||||
if !state {
|
||||
state = true
|
||||
source.resume()
|
||||
@@ -126,7 +136,7 @@ internal final class LocalFolderMonitor {
|
||||
}
|
||||
|
||||
/// Stops sending notifications if currently enabled
|
||||
func stop() {
|
||||
public func stop() {
|
||||
if state {
|
||||
state = false
|
||||
source.suspend()
|
||||
@@ -216,102 +226,7 @@ internal class LocalFileProviderManagerDelegate: NSObject, FileManagerDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
/// Local operation handling is limited. Please don't use as much as possible.
|
||||
open class LocalOperationHandle: OperationHandle {
|
||||
public let baseURL: URL
|
||||
public let operationType: FileOperationType
|
||||
|
||||
init (operationType: FileOperationType, baseURL: URL?) {
|
||||
self.baseURL = baseURL ?? URL(fileURLWithPath: "/")
|
||||
self.operationType = operationType
|
||||
}
|
||||
|
||||
private var sourceURL: URL? {
|
||||
guard let source = operationType.source else { return nil }
|
||||
return source.hasPrefix("file://") ? URL(fileURLWithPath: source) : baseURL.appendingPathComponent(source)
|
||||
}
|
||||
|
||||
private var destURL: URL? {
|
||||
guard let dest = operationType.destination else { return nil }
|
||||
return dest.hasPrefix("file://") ? URL(fileURLWithPath: dest) : baseURL.appendingPathComponent(dest)
|
||||
}
|
||||
|
||||
/// Caution: may put pressure on CPU, may have latency
|
||||
open var bytesSoFar: Int64 {
|
||||
assert(!Thread.isMainThread, "Don't run \(#function) method on main thread")
|
||||
switch operationType {
|
||||
case .modify:
|
||||
guard let url = sourceURL, url.isFileURL else { return 0 }
|
||||
if url.fileIsDirectory {
|
||||
return iterateDirectory(url, deep: true).totalsize
|
||||
} else {
|
||||
return url.fileSize
|
||||
}
|
||||
case .copy, .move:
|
||||
guard let url = destURL, url.isFileURL else { return 0 }
|
||||
if url.fileIsDirectory {
|
||||
return iterateDirectory(url, deep: true).totalsize
|
||||
} else {
|
||||
return url.fileSize
|
||||
}
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// Caution: may put pressure on CPU, may have latency
|
||||
open var totalBytes: Int64 {
|
||||
assert(!Thread.isMainThread, "Don't run \(#function) method on main thread")
|
||||
switch operationType {
|
||||
case .copy, .move:
|
||||
guard let url = sourceURL, url.isFileURL else { return 0 }
|
||||
if url.fileIsDirectory {
|
||||
return iterateDirectory(url, deep: true).totalsize
|
||||
} else {
|
||||
return url.fileSize
|
||||
}
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
/// Not usable in local provider
|
||||
open var inProgress: Bool {
|
||||
return false
|
||||
}
|
||||
|
||||
/// Not usable in local provider
|
||||
open func cancel() -> Bool{
|
||||
return false
|
||||
}
|
||||
|
||||
func iterateDirectory(_ pathURL: URL, deep: Bool) -> (folders: Int, files: Int, totalsize: Int64) {
|
||||
var folders = 0
|
||||
var files = 0
|
||||
var totalsize: Int64 = 0
|
||||
let keys: [URLResourceKey] = [.isDirectoryKey, .fileSizeKey]
|
||||
let enumOpt: FileManager.DirectoryEnumerationOptions = !deep ? [.skipsSubdirectoryDescendants, .skipsPackageDescendants] : []
|
||||
|
||||
let fp = FileManager()
|
||||
let filesList = fp.enumerator(at: pathURL, includingPropertiesForKeys: keys, options: enumOpt, errorHandler: nil)
|
||||
while let fileURL = filesList?.nextObject() as? URL {
|
||||
guard let values = try? fileURL.resourceValues(forKeys: [.isDirectoryKey, .fileSizeKey]) else { continue }
|
||||
let isdir = values.isDirectory ?? false
|
||||
let size = Int64(values.fileSize ?? 0)
|
||||
if isdir {
|
||||
folders += 1
|
||||
} else {
|
||||
files += 1
|
||||
}
|
||||
totalsize += size
|
||||
}
|
||||
|
||||
return (folders, files, totalsize)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#if os(macOS) || os(iOS) || os(tvOS)
|
||||
class UndoBox: NSObject {
|
||||
weak var provider: FileProvideUndoable?
|
||||
let operation: FileOperationType
|
||||
@@ -323,3 +238,4 @@ class UndoBox: NSObject {
|
||||
self.undoOperation = undoOperation
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,474 +0,0 @@
|
||||
|
||||
//
|
||||
// OneDriveFileProvider.swift
|
||||
// FileProvider
|
||||
//
|
||||
// Created by Amir Abbas Mousavian.
|
||||
// Copyright © 2017 Mousavian. Distributed under MIT license.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import CoreGraphics
|
||||
|
||||
/**
|
||||
Allows accessing to OneDrive stored files, either hosted on Microsoft servers or business coprporate one.
|
||||
This provider doesn't cache or save files internally, however you can set `useCache` and `cache` properties
|
||||
to use Foundation `NSURLCache` system.
|
||||
|
||||
- Note: Uploading files and data are limited to 100MB, for now.
|
||||
*/
|
||||
open class OneDriveFileProvider: FileProviderBasicRemote {
|
||||
open class var type: String { return "OneDrive" }
|
||||
open let baseURL: URL?
|
||||
/// Drive name for user, default is `root`. Changing its value will effect on new operations.
|
||||
open var drive: String
|
||||
/// Generated storage url from server url and drive name
|
||||
open var currentPath: String
|
||||
|
||||
open var dispatch_queue: DispatchQueue
|
||||
open var operation_queue: OperationQueue {
|
||||
willSet {
|
||||
assert(_session == nil, "It's not effective to change dispatch_queue property after session is initialized.")
|
||||
}
|
||||
}
|
||||
|
||||
open weak var delegate: FileProviderDelegate?
|
||||
open var credential: URLCredential?
|
||||
open private(set) var cache: URLCache?
|
||||
public var useCache: Bool
|
||||
public var validatingCache: Bool
|
||||
|
||||
fileprivate var _session: URLSession?
|
||||
fileprivate var sessionDelegate: SessionDelegate?
|
||||
public var session: URLSession {
|
||||
if _session == nil {
|
||||
self.sessionDelegate = SessionDelegate(fileProvider: self, credential: credential)
|
||||
let queue = OperationQueue()
|
||||
//queue.underlyingQueue = dispatch_queue
|
||||
let config = URLSessionConfiguration.default
|
||||
config.urlCache = cache
|
||||
config.requestCachePolicy = .returnCacheDataElseLoad
|
||||
_session = URLSession(configuration: config, delegate: sessionDelegate as URLSessionDelegate?, delegateQueue: queue)
|
||||
}
|
||||
return _session!
|
||||
}
|
||||
|
||||
/**
|
||||
Initializer for Onedrive provider with given client ID and Token.
|
||||
These parameters must be retrieved via [Authentication for the OneDrive API](https://dev.onedrive.com/auth/readme.htm).
|
||||
|
||||
There are libraries like [p2/OAuth2](https://github.com/p2/OAuth2) or [OAuthSwift](https://github.com/OAuthSwift/OAuthSwift) which can facilate the procedure to retrieve token.
|
||||
The latter is easier to use and prefered. Also you can use [auth0/Lock](https://github.com/auth0/Lock.iOS-OSX) which provides graphical user interface.
|
||||
|
||||
- Parameters:
|
||||
- credential: a `URLCredential` object with Client ID set as `user` and Token set as `password`.
|
||||
- serverURL: server url, Set it if you are trying to connect OneDrive Business server, otherwise leave it
|
||||
`nil` to connect to OneDrive Personal uses.
|
||||
- drive: drive name for user on server, default value is `root`.
|
||||
- cache: A URLCache to cache downloaded files and contents.
|
||||
*/
|
||||
public init(credential: URLCredential?, serverURL: URL? = nil, drive: String = "root", cache: URLCache? = nil) {
|
||||
let baseURL = serverURL?.absoluteURL ?? URL(string: "https://api.onedrive.com/")!
|
||||
self.baseURL = baseURL.path.hasSuffix("/") ? baseURL : baseURL.appendingPathComponent("")
|
||||
self.drive = drive
|
||||
self.currentPath = ""
|
||||
self.useCache = false
|
||||
self.validatingCache = true
|
||||
self.cache = cache
|
||||
self.credential = credential
|
||||
dispatch_queue = DispatchQueue(label: "FileProvider.\(type(of: self).type)", attributes: .concurrent)
|
||||
operation_queue = OperationQueue()
|
||||
operation_queue.name = "FileProvider.\(type(of: self).type).Operation"
|
||||
}
|
||||
|
||||
public required convenience init?(coder aDecoder: NSCoder) {
|
||||
self.init(credential: aDecoder.decodeObject(forKey: "credential") as? URLCredential,
|
||||
serverURL: aDecoder.decodeObject(forKey: "baseURL") as? URL,
|
||||
drive: aDecoder.decodeObject(forKey: "drive") as? String ?? "root")
|
||||
self.currentPath = aDecoder.decodeObject(forKey: "currentPath") as? String ?? ""
|
||||
self.useCache = aDecoder.decodeBool(forKey: "useCache")
|
||||
self.validatingCache = aDecoder.decodeBool(forKey: "validatingCache")
|
||||
}
|
||||
|
||||
open func encode(with aCoder: NSCoder) {
|
||||
aCoder.encode(self.credential, forKey: "credential")
|
||||
aCoder.encode(self.baseURL, forKey: "baseURL")
|
||||
aCoder.encode(self.drive, forKey: "drive")
|
||||
aCoder.encode(self.currentPath, forKey: "currentPath")
|
||||
aCoder.encode(self.useCache, forKey: "useCache")
|
||||
aCoder.encode(self.validatingCache, forKey: "validatingCache")
|
||||
}
|
||||
|
||||
public static var supportsSecureCoding: Bool {
|
||||
return true
|
||||
}
|
||||
|
||||
open func copy(with zone: NSZone? = nil) -> Any {
|
||||
let copy = OneDriveFileProvider(credential: self.credential, serverURL: self.baseURL, drive: self.drive, cache: self.cache)
|
||||
copy.currentPath = self.currentPath
|
||||
copy.delegate = self.delegate
|
||||
copy.fileOperationDelegate = self.fileOperationDelegate
|
||||
copy.useCache = self.useCache
|
||||
copy.validatingCache = self.validatingCache
|
||||
return copy
|
||||
}
|
||||
|
||||
deinit {
|
||||
if fileProviderCancelTasksOnInvalidating {
|
||||
_session?.invalidateAndCancel()
|
||||
} else {
|
||||
_session?.finishTasksAndInvalidate()
|
||||
}
|
||||
}
|
||||
|
||||
open func contentsOfDirectory(path: String, completionHandler: @escaping ((_ contents: [FileObject], _ error: Error?) -> Void)) {
|
||||
list(path) { (contents, cursor, error) in
|
||||
completionHandler(contents, error)
|
||||
}
|
||||
}
|
||||
|
||||
open func attributesOfItem(path: String, completionHandler: @escaping ((_ attributes: FileObject?, _ error: Error?) -> Void)) {
|
||||
var request = URLRequest(url: url(of: path))
|
||||
request.httpMethod = "GET"
|
||||
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var serverError: FileProviderOneDriveError?
|
||||
var fileObject: OneDriveFileObject?
|
||||
if let response = response as? HTTPURLResponse {
|
||||
let code = FileProviderHTTPErrorCode(rawValue: response.statusCode)
|
||||
serverError = code != nil ? FileProviderOneDriveError(code: code!, path: path, errorDescription: String(data: data ?? Data(), encoding: .utf8)) : nil
|
||||
if let json = data?.deserializeJSON(), let file = OneDriveFileObject(baseURL: self.baseURL, drive: self.drive, json: json) {
|
||||
fileObject = file
|
||||
}
|
||||
}
|
||||
completionHandler(fileObject, serverError ?? error)
|
||||
})
|
||||
task.resume()
|
||||
}
|
||||
|
||||
open func storageProperties(completionHandler: @escaping ((_ total: Int64, _ used: Int64) -> Void)) {
|
||||
var request = URLRequest(url: url())
|
||||
request.httpMethod = "GET"
|
||||
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var totalSize: Int64 = -1
|
||||
var usedSize: Int64 = 0
|
||||
if let json = data?.deserializeJSON() {
|
||||
totalSize = (json["total"] as? NSNumber)?.int64Value ?? -1
|
||||
usedSize = (json["used"] as? NSNumber)?.int64Value ?? 0
|
||||
}
|
||||
completionHandler(totalSize, usedSize)
|
||||
})
|
||||
task.resume()
|
||||
}
|
||||
|
||||
open func searchFiles(path: String, recursive: Bool, query: NSPredicate, foundItemHandler: ((FileObject) -> Void)?, completionHandler: @escaping ((_ files: [FileObject], _ error: Error?) -> Void)) {
|
||||
var foundFiles = [OneDriveFileObject]()
|
||||
var queryStr: String?
|
||||
queryStr = query.findValue(forKey: "name") as? String ?? query.findAllValues(forKey: nil).flatMap { $0.value as? String }.first
|
||||
guard let finalQueryStr = queryStr else { return }
|
||||
search(path, query: finalQueryStr, foundItem: { (file) in
|
||||
if query.evaluate(with: file.mapPredicate()) {
|
||||
foundFiles.append(file)
|
||||
foundItemHandler?(file)
|
||||
}
|
||||
}, completionHandler: { (error) in
|
||||
completionHandler(foundFiles, error)
|
||||
})
|
||||
}
|
||||
|
||||
open func url(of path: String? = nil, modifier: String? = nil) -> URL {
|
||||
var rpath: String
|
||||
if let path = path {
|
||||
rpath = path
|
||||
} else {
|
||||
rpath = self.currentPath
|
||||
}
|
||||
|
||||
if rpath.hasPrefix("/") {
|
||||
rpath.remove(at: rpath.startIndex)
|
||||
}
|
||||
if rpath.isEmpty {
|
||||
if let modifier = modifier {
|
||||
return baseURL!.appendingPathComponent("drive/\(drive)/\(modifier)")
|
||||
}
|
||||
return baseURL!.appendingPathComponent("drive/\(drive)")
|
||||
}
|
||||
let driveURL = baseURL!.appendingPathComponent("drive/\(drive):/")
|
||||
rpath = (rpath.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? rpath)
|
||||
rpath = rpath.trimmingCharacters(in: pathTrimSet)
|
||||
if let modifier = modifier {
|
||||
rpath = rpath + ":/" + modifier
|
||||
}
|
||||
return URL(string: rpath, relativeTo: driveURL) ?? driveURL
|
||||
}
|
||||
|
||||
open func isReachable(completionHandler: @escaping (Bool) -> Void) {
|
||||
var request = URLRequest(url: url())
|
||||
request.httpMethod = "HEAD"
|
||||
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
let status = (response as? HTTPURLResponse)?.statusCode ?? 400
|
||||
completionHandler(status == 200)
|
||||
})
|
||||
task.resume()
|
||||
}
|
||||
|
||||
open weak var fileOperationDelegate: FileOperationDelegate?
|
||||
}
|
||||
|
||||
extension OneDriveFileProvider: FileProviderOperations {
|
||||
|
||||
|
||||
open func create(folder folderName: String, at atPath: String, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
let path = (atPath as NSString).appendingPathComponent(folderName) + "/"
|
||||
return doOperation(.create(path: path), completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
open func moveItem(path: String, to toPath: String, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
return doOperation(.move(source: path, destination: toPath), completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
open func copyItem(path: String, to toPath: String, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
return doOperation(.copy(source: path, destination: toPath), completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
open func removeItem(path: String, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
return doOperation(.remove(path: path), completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
fileprivate func doOperation(_ operation: FileOperationType, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
guard fileOperationDelegate?.fileProvider(self, shouldDoOperation: operation) ?? true == true else {
|
||||
return nil
|
||||
}
|
||||
guard let sourcePath = operation.source else { return nil }
|
||||
let destPath = operation.destination
|
||||
var request = URLRequest(url: url(of: sourcePath))
|
||||
switch operation {
|
||||
case .create:
|
||||
request.httpMethod = "CREATE"
|
||||
case .copy:
|
||||
request.httpMethod = "POST"
|
||||
case .move:
|
||||
request.httpMethod = "PATCH"
|
||||
case .remove:
|
||||
request.httpMethod = "DELETE"
|
||||
default: // modify, link, fetch
|
||||
return nil
|
||||
}
|
||||
|
||||
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
|
||||
var requestDictionary = [String: AnyObject]()
|
||||
if let dest = correctPath(destPath) as NSString? {
|
||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||
requestDictionary["parentReference"] = ("/drive/\(drive):" + dest.deletingLastPathComponent) as NSString
|
||||
requestDictionary["name"] = dest.lastPathComponent as NSString
|
||||
request.httpBody = Data(jsonDictionary: requestDictionary)
|
||||
}
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var serverError: FileProviderOneDriveError?
|
||||
if let response = response as? HTTPURLResponse, response.statusCode >= 300, let code = FileProviderHTTPErrorCode(rawValue: response.statusCode) {
|
||||
serverError = FileProviderOneDriveError(code: code, path: sourcePath, errorDescription: String(data: data ?? Data(), encoding: .utf8))
|
||||
}
|
||||
completionHandler?(serverError ?? error)
|
||||
self.delegateNotify(operation, error: serverError ?? error)
|
||||
})
|
||||
task.taskDescription = operation.json
|
||||
task.resume()
|
||||
return RemoteOperationHandle(operationType: operation, tasks: [task])
|
||||
}
|
||||
|
||||
open func copyItem(localFile: URL, to toPath: String, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
let opType = FileOperationType.copy(source: localFile.absoluteString, destination: toPath)
|
||||
guard fileOperationDelegate?.fileProvider(self, shouldDoOperation: opType) ?? true == true else {
|
||||
return nil
|
||||
}
|
||||
return upload_simple(toPath, localFile: localFile, overwrite: overwrite, operation: opType, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
open func copyItem(path: String, toLocalURL destURL: URL, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
let opType = FileOperationType.copy(source: path, destination: destURL.absoluteString)
|
||||
guard fileOperationDelegate?.fileProvider(self, shouldDoOperation: opType) ?? true == true else {
|
||||
return nil
|
||||
}
|
||||
var request = URLRequest(url: self.url(of: path, modifier: "content"))
|
||||
request.httpMethod = "GET"
|
||||
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
|
||||
let task = session.downloadTask(with: request)
|
||||
completionHandlersForTasks[task.taskIdentifier] = completionHandler
|
||||
downloadCompletionHandlersForTasks[task.taskIdentifier] = { tempURL in
|
||||
guard let httpResponse = task.response as? HTTPURLResponse , httpResponse.statusCode < 300 else {
|
||||
let code = FileProviderHTTPErrorCode(rawValue: (task.response as? HTTPURLResponse)?.statusCode ?? -1)
|
||||
let errorData : Data? = nil //Data(contentsOf: cacheURL) // TODO: Figure out how to get error response data for the error description
|
||||
let serverError : FileProviderOneDriveError? = code != nil ? FileProviderOneDriveError(code: code!, path: path, errorDescription: String(data: errorData ?? Data(), encoding: .utf8)) : nil
|
||||
completionHandler?(serverError)
|
||||
return
|
||||
}
|
||||
do {
|
||||
try FileManager.default.moveItem(at: tempURL, to: destURL)
|
||||
completionHandler?(nil)
|
||||
} catch let e {
|
||||
completionHandler?(e)
|
||||
}
|
||||
}
|
||||
task.taskDescription = opType.json
|
||||
task.resume()
|
||||
return RemoteOperationHandle(operationType: opType, tasks: [task])
|
||||
}
|
||||
}
|
||||
|
||||
extension OneDriveFileProvider: FileProviderReadWrite {
|
||||
open func contents(path: String, offset: Int64, length: Int, completionHandler: @escaping ((_ contents: Data?, _ error: Error?) -> Void)) -> OperationHandle? {
|
||||
if length == 0 || offset < 0 {
|
||||
dispatch_queue.async {
|
||||
completionHandler(Data(), nil)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
let opType = FileOperationType.fetch(path: path)
|
||||
var request = URLRequest(url: self.url(of: path, modifier: "content"))
|
||||
request.httpMethod = "GET"
|
||||
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
|
||||
if length > 0 {
|
||||
request.setValue("bytes=\(offset)-\(offset + Int64(length) - 1)", forHTTPHeaderField: "Range")
|
||||
} else if offset > 0 && length < 0 {
|
||||
request.setValue("bytes=\(offset)-", forHTTPHeaderField: "Range")
|
||||
}
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var serverError: FileProviderOneDriveError?
|
||||
if let httpResponse = response as? HTTPURLResponse , httpResponse.statusCode >= 300, let code = FileProviderHTTPErrorCode(rawValue: httpResponse.statusCode) {
|
||||
serverError = FileProviderOneDriveError(code: code, path: path, errorDescription: String(data: data ?? Data(), encoding: .utf8))
|
||||
}
|
||||
let filedata = serverError ?? error == nil ? data : nil
|
||||
completionHandler(filedata, serverError ?? error)
|
||||
})
|
||||
task.taskDescription = opType.json
|
||||
task.resume()
|
||||
return RemoteOperationHandle(operationType: opType, tasks: [task])
|
||||
}
|
||||
|
||||
open func writeContents(path: String, contents data: Data?, atomically: Bool, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
let opType = FileOperationType.modify(path: path)
|
||||
guard fileOperationDelegate?.fileProvider(self, shouldDoOperation: opType) ?? true == true else {
|
||||
return nil
|
||||
}
|
||||
// FIXME: remove 150MB restriction
|
||||
return upload_simple(path, data: data ?? Data(), overwrite: overwrite, operation: opType, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
fileprivate func registerNotifcation(path: String, eventHandler: (() -> Void)) {
|
||||
/* There is two ways to monitor folders changing in OneDrive. Either using webooks
|
||||
* which means you have to implement a server to translate it to push notifications
|
||||
* or using apiv2 list_folder/longpoll method. The second one is implemeted here.
|
||||
* Tough webhooks are much more efficient, longpoll is much simpler to implement!
|
||||
* You can implemnt your own webhook service and replace this method accordingly.
|
||||
*/
|
||||
NotImplemented()
|
||||
}
|
||||
fileprivate func unregisterNotifcation(path: String) {
|
||||
NotImplemented()
|
||||
}
|
||||
|
||||
/**
|
||||
Genrates a public url to a file to be shared with other users and can be downloaded without authentication.
|
||||
|
||||
- Parameters:
|
||||
- to: path of file, including file/directory name.
|
||||
- completionHandler: a closure with result of directory entries or error.
|
||||
`link`: a url returned by OneDrive to share.
|
||||
`attribute`: `nil` for OneDrive.
|
||||
`expiration`: `nil` for OneDrive, as it doesn't expires.
|
||||
`error`: Error returned by OneDrive.
|
||||
*/
|
||||
open func publicLink(to path: String, completionHandler: @escaping ((_ link: URL?, _ attribute: OneDriveFileObject?, _ expiration: Date?, _ error: Error?) -> Void)) {
|
||||
var request = URLRequest(url: self.url(of: path, modifier: "action.createLink"))
|
||||
request.httpMethod = "POST"
|
||||
let requestDictionary: [String: AnyObject] = ["type": "view" as NSString]
|
||||
request.httpBody = Data(jsonDictionary: requestDictionary)
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var serverError: FileProviderOneDriveError?
|
||||
var link: URL?
|
||||
if let response = response as? HTTPURLResponse {
|
||||
let code = FileProviderHTTPErrorCode(rawValue: response.statusCode)
|
||||
serverError = code != nil ? FileProviderOneDriveError(code: code!, path: path, errorDescription: String(data: data ?? Data(), encoding: .utf8)) : nil
|
||||
if let json = data?.deserializeJSON() {
|
||||
if let linkDic = json["link"] as? NSDictionary, let linkStr = linkDic["webUrl"] as? String {
|
||||
link = URL(string: linkStr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
completionHandler(link, nil, nil, serverError ?? error)
|
||||
})
|
||||
task.resume()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension OneDriveFileProvider: ExtendedFileProvider {
|
||||
open func thumbnailOfFileSupported(path: String) -> Bool {
|
||||
return true
|
||||
}
|
||||
|
||||
open func propertiesOfFileSupported(path: String) -> Bool {
|
||||
let fileExt = (path as NSString).pathExtension.lowercased()
|
||||
switch fileExt {
|
||||
case "jpg", "jpeg", "bmp", "gif", "png", "tif", "tiff":
|
||||
return true
|
||||
case "mp3", "aac", "m4a", "wma":
|
||||
return true
|
||||
case "mp4", "mpg", "3gp", "mov", "avi", "wmv":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
open func thumbnailOfFile(path: String, dimension: CGSize?, completionHandler: @escaping ((_ image: ImageClass?, _ error: Error?) -> Void)) {
|
||||
let url: URL
|
||||
if let dimension = dimension {
|
||||
url = self.url(of: path, modifier: "thumbnails/0/=c\(dimension.width)x\(dimension.height)/content")
|
||||
} else {
|
||||
url = self.url(of: path, modifier: "thumbnails/0/small/content")
|
||||
}
|
||||
|
||||
var request = URLRequest(url: url)
|
||||
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
|
||||
let task = self.session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var image: ImageClass? = nil
|
||||
if let code = (response as? HTTPURLResponse)?.statusCode , code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) {
|
||||
let responseError = FileProviderOneDriveError(code: rCode, path: path, errorDescription: String(data: data ?? Data(), encoding: .utf8))
|
||||
completionHandler(nil, responseError)
|
||||
return
|
||||
}
|
||||
if let data = data {
|
||||
image = ImageClass(data: data)
|
||||
}
|
||||
completionHandler(image, error)
|
||||
})
|
||||
task.resume()
|
||||
}
|
||||
|
||||
open func propertiesOfFile(path: String, completionHandler: @escaping ((_ propertiesDictionary: [String : Any], _ keys: [String], _ error: Error?) -> Void)) {
|
||||
var request = URLRequest(url: url(of: path))
|
||||
request.httpMethod = "GET"
|
||||
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var serverError: FileProviderOneDriveError?
|
||||
var dic = [String: Any]()
|
||||
var keys = [String]()
|
||||
if let response = response as? HTTPURLResponse {
|
||||
let code = FileProviderHTTPErrorCode(rawValue: response.statusCode)
|
||||
serverError = code != nil ? FileProviderOneDriveError(code: code!, path: path, errorDescription: String(data: data ?? Data(), encoding: .utf8)) : nil
|
||||
if let json = data?.deserializeJSON() {
|
||||
(dic, keys) = self.mapMediaInfo(json)
|
||||
}
|
||||
}
|
||||
completionHandler(dic, keys, serverError ?? error)
|
||||
})
|
||||
task.resume()
|
||||
}
|
||||
}
|
||||
|
||||
extension OneDriveFileProvider: FileProvider { }
|
||||
@@ -0,0 +1,645 @@
|
||||
//
|
||||
// OneDriveFileProvider.swift
|
||||
// FileProvider
|
||||
//
|
||||
// Created by Amir Abbas Mousavian.
|
||||
// Copyright © 2017 Mousavian. Distributed under MIT license.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
#if os(macOS) || os(iOS) || os(tvOS)
|
||||
import CoreGraphics
|
||||
#endif
|
||||
|
||||
/**
|
||||
Allows accessing to OneDrive stored files, either hosted on Microsoft servers or business coprporate one.
|
||||
This provider doesn't cache or save files internally, however you can set `useCache` and `cache` properties
|
||||
to use Foundation `NSURLCache` system.
|
||||
|
||||
- Note: You can pass file id instead of file path, e.g `"id:1234abcd"`, to point to a file or folder by ID.
|
||||
|
||||
- Note: Uploading files and data are limited to 100MB, for now.
|
||||
*/
|
||||
open class OneDriveFileProvider: HTTPFileProvider, FileProviderSharing {
|
||||
override open class var type: String { return "OneDrive" }
|
||||
|
||||
/// Route to access file container on OneDrive. For default logined user use `.me` otherwise you can acesss
|
||||
/// container based on drive id, group id, site id or user id for another user's default container
|
||||
public enum Route: RawRepresentable {
|
||||
/// Access to default container for current user
|
||||
case me
|
||||
/// Access to a specific drive by id
|
||||
case drive(uuid: UUID)
|
||||
/// Access to a default drive of a group by their id
|
||||
case group(uuid: UUID)
|
||||
/// Access to a default drive of a site by their id
|
||||
case site(uuid: UUID)
|
||||
/// Access to a default drive of a user by their id
|
||||
case user(uuid: UUID)
|
||||
|
||||
public init?(rawValue: String) {
|
||||
let components = rawValue.components(separatedBy: ";")
|
||||
guard let type = components.first else {
|
||||
return nil
|
||||
}
|
||||
if type == "me" {
|
||||
self = .me
|
||||
}
|
||||
guard let uuid = components.last.flatMap({ UUID(uuidString: $0) }) else {
|
||||
return nil
|
||||
}
|
||||
switch type {
|
||||
case "drive":
|
||||
self = .drive(uuid: uuid)
|
||||
case "group":
|
||||
self = .group(uuid: uuid)
|
||||
case "site":
|
||||
self = .site(uuid: uuid)
|
||||
case "user":
|
||||
self = .user(uuid: uuid)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
public var rawValue: String {
|
||||
switch self {
|
||||
case .me:
|
||||
return "me;"
|
||||
case .drive(uuid: let uuid):
|
||||
return "drive;" + uuid.uuidString
|
||||
case .group(uuid: let uuid):
|
||||
return "group;" + uuid.uuidString
|
||||
case .site(uuid: let uuid):
|
||||
return "site;" + uuid.uuidString
|
||||
case .user(uuid: let uuid):
|
||||
return "user;" + uuid.uuidString
|
||||
}
|
||||
}
|
||||
|
||||
/// Return path component in URL for selected drive
|
||||
var drivePath: String {
|
||||
switch self {
|
||||
case .me:
|
||||
return "me/drive"
|
||||
case .drive(uuid: let uuid):
|
||||
return "drives/" + uuid.uuidString
|
||||
case .group(uuid: let uuid):
|
||||
return "groups/" + uuid.uuidString + "/drive"
|
||||
case .site(uuid: let uuid):
|
||||
return "sites/" + uuid.uuidString + "/drive"
|
||||
case .user(uuid: let uuid):
|
||||
return "users/" + uuid.uuidString + "/drive"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Microsoft Graph URL
|
||||
public static var graphURL = URL(string: "https://graph.microsoft.com/")!
|
||||
|
||||
/// Microsoft Graph URL
|
||||
public static var graphVersion = "v1.0"
|
||||
|
||||
/// Route for container, default is `.me`.
|
||||
open let route: Route
|
||||
|
||||
/**
|
||||
Initializer for Onedrive provider with given client ID and Token.
|
||||
These parameters must be retrieved via [Authentication for the OneDrive API](https://dev.onedrive.com/auth/readme.htm).
|
||||
|
||||
There are libraries like [p2/OAuth2](https://github.com/p2/OAuth2) or [OAuthSwift](https://github.com/OAuthSwift/OAuthSwift) which can facilate the procedure to retrieve token. The latter is easier to use and prefered.
|
||||
|
||||
- Parameters:
|
||||
- credential: a `URLCredential` object with Client ID set as `user` and Token set as `password`.
|
||||
- serverURL: server url, Set it if you are trying to connect OneDrive Business server, otherwise leave it
|
||||
`nil` to connect to OneDrive Personal user.
|
||||
- drive: drive name for user on server, default value is `root`.
|
||||
- cache: A URLCache to cache downloaded files and contents.
|
||||
*/
|
||||
@available(*, deprecated, message: "use init(credential:, serverURL:, route:, cache:) instead.")
|
||||
public convenience init(credential: URLCredential?, serverURL: URL? = nil, drive: String?, cache: URLCache? = nil) {
|
||||
let route: Route = drive.flatMap({ UUID(uuidString: $0) }).flatMap({ Route.drive(uuid: $0) }) ?? .me
|
||||
self.init(credential: credential, serverURL: serverURL, route: route, cache: cache)
|
||||
}
|
||||
|
||||
/**
|
||||
Initializer for Onedrive provider with given client ID and Token.
|
||||
These parameters must be retrieved via [Authentication for the OneDrive API](https://dev.onedrive.com/auth/readme.htm).
|
||||
|
||||
There are libraries like [p2/OAuth2](https://github.com/p2/OAuth2) or [OAuthSwift](https://github.com/OAuthSwift/OAuthSwift) which can facilate the procedure to retrieve token.
|
||||
The latter is easier to use and prefered. Also you can use [auth0/Lock](https://github.com/auth0/Lock.iOS-OSX) which provides graphical user interface.
|
||||
|
||||
- Parameters:
|
||||
- credential: a `URLCredential` object with Client ID set as `user` and Token set as `password`.
|
||||
- serverURL: server url, Set it if you are trying to connect OneDrive Business server, otherwise leave it
|
||||
`nil` to connect to OneDrive Personal uses.
|
||||
- route: drive name for user on server, default value is `.me`.
|
||||
- cache: A URLCache to cache downloaded files and contents.
|
||||
*/
|
||||
public init(credential: URLCredential?, serverURL: URL? = nil, route: Route = .me, cache: URLCache? = nil) {
|
||||
let baseURL = (serverURL?.absoluteURL ?? OneDriveFileProvider.graphURL)
|
||||
.appendingPathComponent(OneDriveFileProvider.graphVersion, isDirectory: true)
|
||||
let refinedBaseURL = baseURL.absoluteString.hasSuffix("/") ? baseURL : baseURL.appendingPathComponent("")
|
||||
self.route = route
|
||||
super.init(baseURL: refinedBaseURL, credential: credential, cache: cache)
|
||||
}
|
||||
|
||||
public required convenience init?(coder aDecoder: NSCoder) {
|
||||
let route: Route
|
||||
if let driveId = aDecoder.decodeObject(of: NSString.self, forKey: "drive") as String?, let uuid = UUID(uuidString: driveId) {
|
||||
route = .drive(uuid: uuid)
|
||||
} else {
|
||||
route = (aDecoder.decodeObject(forKey: "route") as? String).flatMap({ Route(rawValue: $0) }) ?? .me
|
||||
}
|
||||
self.init(credential: aDecoder.decodeObject(of: URLCredential.self, forKey: "credential"),
|
||||
serverURL: aDecoder.decodeObject(of: NSURL.self, forKey: "baseURL") as URL?,
|
||||
route: route)
|
||||
self.useCache = aDecoder.decodeBool(forKey: "useCache")
|
||||
self.validatingCache = aDecoder.decodeBool(forKey: "validatingCache")
|
||||
}
|
||||
|
||||
open override func encode(with aCoder: NSCoder) {
|
||||
super.encode(with: aCoder)
|
||||
aCoder.encode(self.route.rawValue, forKey: "route")
|
||||
}
|
||||
|
||||
open override func copy(with zone: NSZone? = nil) -> Any {
|
||||
let copy = OneDriveFileProvider(credential: self.credential, serverURL: self.baseURL, route: self.route, cache: self.cache)
|
||||
copy.delegate = self.delegate
|
||||
copy.fileOperationDelegate = self.fileOperationDelegate
|
||||
copy.useCache = self.useCache
|
||||
copy.validatingCache = self.validatingCache
|
||||
return copy
|
||||
}
|
||||
|
||||
/**
|
||||
Returns an Array of `FileObject`s identifying the the directory entries via asynchronous completion handler.
|
||||
|
||||
If the directory contains no entries or an error is occured, this method will return the empty array.
|
||||
|
||||
- Parameters:
|
||||
- path: path to target directory. If empty, root will be iterated.
|
||||
- completionHandler: a closure with result of directory entries or error.
|
||||
- contents: An array of `FileObject` identifying the the directory entries.
|
||||
- error: Error returned by system.
|
||||
*/
|
||||
open override func contentsOfDirectory(path: String, completionHandler: @escaping (_ contents: [FileObject], _ error: Error?) -> Void) {
|
||||
_ = paginated(path, requestHandler: { [weak self] (token) -> URLRequest? in
|
||||
guard let `self` = self else { return nil }
|
||||
let url = token.flatMap(URL.init(string:)) ?? self.url(of: path, modifier: "children")
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "GET"
|
||||
request.setValue(authentication: self.credential, with: .oAuth2)
|
||||
return request
|
||||
}, pageHandler: { [weak self] (data, _) -> (files: [FileObject], error: Error?, newToken: String?) in
|
||||
guard let `self` = self else { return ([], nil, nil) }
|
||||
|
||||
guard let json = data?.deserializeJSON(), let entries = json["value"] as? [AnyObject] else {
|
||||
let err = self.urlError(path, code: .badServerResponse)
|
||||
return ([], err, nil)
|
||||
}
|
||||
|
||||
var files = [FileObject]()
|
||||
for entry in entries {
|
||||
if let entry = entry as? [String: AnyObject], let file = OneDriveFileObject(baseURL: self.baseURL, route: self.route, json: entry) {
|
||||
files.append(file)
|
||||
}
|
||||
}
|
||||
return (files, nil, json["@odata.nextLink"] as? String)
|
||||
}, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
/**
|
||||
Returns a `FileObject` containing the attributes of the item (file, directory, symlink, etc.) at the path in question via asynchronous completion handler.
|
||||
|
||||
If the directory contains no entries or an error is occured, this method will return the empty `FileObject`.
|
||||
|
||||
- Parameters:
|
||||
- path: path to target directory. If empty, attributes of root will be returned.
|
||||
- completionHandler: a closure with result of directory entries or error.
|
||||
- attributes: A `FileObject` containing the attributes of the item.
|
||||
- error: Error returned by system.
|
||||
*/
|
||||
open override func attributesOfItem(path: String, completionHandler: @escaping (_ attributes: FileObject?, _ error: Error?) -> Void) {
|
||||
var request = URLRequest(url: url(of: path))
|
||||
request.httpMethod = "GET"
|
||||
request.setValue(authentication: self.credential, with: .oAuth2)
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var serverError: FileProviderHTTPError?
|
||||
var fileObject: OneDriveFileObject?
|
||||
if let response = response as? HTTPURLResponse, response.statusCode >= 400 {
|
||||
let code = FileProviderHTTPErrorCode(rawValue: response.statusCode)
|
||||
serverError = code.flatMap { self.serverError(with: $0, path: path, data: data) }
|
||||
}
|
||||
if let json = data?.deserializeJSON(), let file = OneDriveFileObject(baseURL: self.baseURL, route: self.route, json: json) {
|
||||
fileObject = file
|
||||
}
|
||||
completionHandler(fileObject, serverError ?? error)
|
||||
})
|
||||
task.resume()
|
||||
}
|
||||
|
||||
/// Returns volume/provider information asynchronously.
|
||||
/// - Parameter volumeInfo: Information of filesystem/Provider returned by system/server.
|
||||
open override func storageProperties(completionHandler: @escaping (_ volumeInfo: VolumeObject?) -> Void) {
|
||||
let url = URL(string: route.drivePath, relativeTo: baseURL)!
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "GET"
|
||||
request.setValue(authentication: self.credential, with: .oAuth2)
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
guard let json = data?.deserializeJSON() else {
|
||||
completionHandler(nil)
|
||||
return
|
||||
}
|
||||
|
||||
let volume = VolumeObject(allValues: [:])
|
||||
volume.url = request.url
|
||||
volume.uuid = json["id"] as? String
|
||||
volume.name = json["name"] as? String
|
||||
volume.creationDate = (json["createdDateTime"] as? String).flatMap { Date(rfcString: $0) }
|
||||
volume.totalCapacity = (json["quota"]?["total"] as? NSNumber)?.int64Value ?? -1
|
||||
volume.availableCapacity = (json["quota"]?["remaining"] as? NSNumber)?.int64Value ?? 0
|
||||
completionHandler(volume)
|
||||
})
|
||||
task.resume()
|
||||
}
|
||||
|
||||
/**
|
||||
Search files inside directory using query asynchronously.
|
||||
|
||||
Sample predicates:
|
||||
```
|
||||
NSPredicate(format: "(name CONTAINS[c] 'hello') && (fileSize >= 10000)")
|
||||
NSPredicate(format: "(modifiedDate >= %@)", Date())
|
||||
NSPredicate(format: "(path BEGINSWITH %@)", "folder/child folder")
|
||||
```
|
||||
|
||||
- Note: Don't pass Spotlight predicates to this method directly, use `FileProvider.convertSpotlightPredicateTo()` method to get usable predicate.
|
||||
|
||||
- Important: A file name criteria should be provided for Dropbox.
|
||||
|
||||
- Parameters:
|
||||
- path: location of directory to start search
|
||||
- recursive: Searching subdirectories of path
|
||||
- query: An `NSPredicate` object with keys like `FileObject` members, except `size` which becomes `filesize`.
|
||||
- foundItemHandler: Closure which is called when a file is found
|
||||
- completionHandler: Closure which will be called after finishing search. Returns an arry of `FileObject` or error if occured.
|
||||
- files: all files meat the `query` criteria.
|
||||
- error: `Error` returned by server if occured.
|
||||
- Returns: An `Progress` to get progress or cancel progress. Use `completedUnitCount` to iterate count of found items.
|
||||
*/
|
||||
@discardableResult
|
||||
open override func searchFiles(path: String, recursive: Bool, query: NSPredicate, foundItemHandler: ((FileObject) -> Void)?, completionHandler: @escaping (_ files: [FileObject], _ error: Error?) -> Void) -> Progress? {
|
||||
let queryStr = query.findValue(forKey: "name") as? String ?? query.findAllValues(forKey: nil).compactMap { $0.value as? String }.first
|
||||
|
||||
return paginated(path, requestHandler: { [weak self] (token) -> URLRequest? in
|
||||
guard let `self` = self else { return nil }
|
||||
|
||||
let url: URL
|
||||
if let next = token.flatMap(URL.init(string:)) {
|
||||
url = next
|
||||
} else {
|
||||
let bURL = self.baseURL!.appendingPathComponent(self.route.drivePath).appendingPathComponent("root/search")
|
||||
var components = URLComponents(url: bURL, resolvingAgainstBaseURL: false)!
|
||||
let qItem = URLQueryItem(name: "q", value: (queryStr ?? "*"))
|
||||
components.queryItems = [qItem]
|
||||
if recursive {
|
||||
components.queryItems?.append(URLQueryItem(name: "expand", value: "children"))
|
||||
}
|
||||
url = components.url!
|
||||
}
|
||||
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "GET"
|
||||
return request
|
||||
}, pageHandler: { [weak self] (data, progress) -> (files: [FileObject], error: Error?, newToken: String?) in
|
||||
guard let `self` = self else { return ([], nil, nil) }
|
||||
guard let json = data?.deserializeJSON(), let entries = json["value"] as? [AnyObject] else {
|
||||
let err = self.urlError(path, code: .badServerResponse)
|
||||
return ([], err, nil)
|
||||
}
|
||||
|
||||
var foundFiles = [FileObject]()
|
||||
for entry in entries {
|
||||
if let entry = entry as? [String: AnyObject], let file = OneDriveFileObject(baseURL: self.baseURL, route: self.route, json: entry), query.evaluate(with: file.mapPredicate()) {
|
||||
foundFiles.append(file)
|
||||
foundItemHandler?(file)
|
||||
}
|
||||
}
|
||||
|
||||
return (foundFiles, nil, json["@odata.nextLink"] as? String)
|
||||
}, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
/**
|
||||
Returns an independent url to access the file. Some providers like `Dropbox` due to their nature.
|
||||
don't return an absolute url to be used to access file directly.
|
||||
- Parameter path: Relative path of file or directory.
|
||||
- Returns: An url, can be used to access to file directly.
|
||||
*/
|
||||
open override func url(of path: String) -> URL {
|
||||
return OneDriveFileObject.url(of: path, modifier: nil, baseURL: baseURL!, route: route)
|
||||
}
|
||||
|
||||
/**
|
||||
Returns an independent url to access the file. Some providers like `Dropbox` due to their nature.
|
||||
don't return an absolute url to be used to access file directly.
|
||||
- Parameter path: Relative path of file or directory.
|
||||
- Parameter modifier: Added to end of url to indicate what it can used for, e.g. `contents` to fetch data.
|
||||
- Returns: An url, can be used to access to file directly.
|
||||
*/
|
||||
open func url(of path: String, modifier: String? = nil) -> URL {
|
||||
return OneDriveFileObject.url(of: path, modifier: modifier, baseURL: baseURL!, route: route)
|
||||
}
|
||||
|
||||
open override func relativePathOf(url: URL) -> String {
|
||||
return OneDriveFileObject.relativePathOf(url: url, baseURL: baseURL, route: route)
|
||||
}
|
||||
|
||||
/// Checks the connection to server or permission on local
|
||||
///
|
||||
/// - Note: To prevent race condition, use this method wisely and avoid it as far possible.
|
||||
///
|
||||
/// - Parameter success: indicated server is reachable or not.
|
||||
open override func isReachable(completionHandler: @escaping (_ success: Bool, _ error: Error?) -> Void) {
|
||||
var request = URLRequest(url: url(of: ""))
|
||||
request.httpMethod = "HEAD"
|
||||
request.setValue(authentication: credential, with: .oAuth2)
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
let status = (response as? HTTPURLResponse)?.statusCode ?? 400
|
||||
if status >= 400, let code = FileProviderHTTPErrorCode(rawValue: status) {
|
||||
let errorDesc = data.flatMap({ String(data: $0, encoding: .utf8) })
|
||||
let error = FileProviderOneDriveError(code: code, path: "", serverDescription: errorDesc)
|
||||
completionHandler(false, error)
|
||||
return
|
||||
}
|
||||
completionHandler(status == 200, error)
|
||||
})
|
||||
task.resume()
|
||||
}
|
||||
|
||||
/**
|
||||
Uploads a file from local file url to designated path asynchronously.
|
||||
Method will fail if source is not a local url with `file://` scheme.
|
||||
|
||||
- Note: It's safe to assume that this method only works on individual files and **won't** copy folders recursively.
|
||||
|
||||
- Parameters:
|
||||
- localFile: a file url to file.
|
||||
- to: destination path of file, including file/directory name.
|
||||
- overwrite: Destination file should be overwritten if file is already exists. **Default** is `false`.
|
||||
- completionHandler: If an error parameter was provided, a presentable `Error` will be returned.
|
||||
- Returns: An `Progress` to get progress or cancel progress.
|
||||
*/
|
||||
@discardableResult
|
||||
open override func copyItem(localFile: URL, to toPath: String, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
// check file is not a folder
|
||||
guard (try? localFile.resourceValues(forKeys: [.fileResourceTypeKey]))?.fileResourceType ?? .unknown == .regular else {
|
||||
dispatch_queue.async {
|
||||
completionHandler?(self.urlError(localFile.path, code: .fileIsDirectory))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
let operation = FileOperationType.copy(source: localFile.absoluteString, destination: toPath)
|
||||
guard fileOperationDelegate?.fileProvider(self, shouldDoOperation: operation) ?? true == true else {
|
||||
return nil
|
||||
}
|
||||
return self.upload_multipart_file(toPath, file: localFile, operation: operation, overwrite: overwrite, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
/**
|
||||
Write the contents of the `Data` to a location asynchronously.
|
||||
It will return error if file is already exists.
|
||||
Not attomically by default, unless the provider enforces it.
|
||||
|
||||
- Parameters:
|
||||
- path: Path of target file.
|
||||
- contents: Data to be written into file, pass nil to create empty file.
|
||||
- completionHandler: If an error parameter was provided, a presentable `Error` will be returned.
|
||||
- Returns: An `Progress` to get progress or cancel progress. Doesn't work on `LocalFileProvider`.
|
||||
*/
|
||||
@discardableResult
|
||||
open override func writeContents(path: String, contents data: Data?, atomically: Bool, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
let operation = FileOperationType.modify(path: path)
|
||||
guard fileOperationDelegate?.fileProvider(self, shouldDoOperation: operation) ?? true == true else {
|
||||
return nil
|
||||
}
|
||||
return upload_multipart_data(path, data: data ?? Data(), operation: operation, overwrite: overwrite, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
override func request(for operation: FileOperationType, overwrite: Bool = false, attributes: [URLResourceKey : Any] = [:]) -> URLRequest {
|
||||
|
||||
func correctPath(_ path: String) -> String {
|
||||
if path.hasPrefix("id:") {
|
||||
return path
|
||||
}
|
||||
var p = path.hasPrefix("/") ? path : "/" + path
|
||||
if p.hasSuffix("/") {
|
||||
p.remove(at: p.index(before:p.endIndex))
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
let method: String
|
||||
let url: URL
|
||||
switch operation {
|
||||
case .fetch(path: let path):
|
||||
method = "GET"
|
||||
url = self.url(of: path, modifier: "content")
|
||||
case .create(path: let path) where path.hasSuffix("/"):
|
||||
method = "POST"
|
||||
let parent = (path as NSString).deletingLastPathComponent
|
||||
url = self.url(of: parent, modifier: "children")
|
||||
case .modify(path: let path):
|
||||
method = "PUT"
|
||||
let queryStr = overwrite ? "" : "?@name.conflictBehavior=fail"
|
||||
url = URL(string: self.url(of: path, modifier: "content").absoluteString + queryStr)!
|
||||
case .copy(source: let source, destination: let dest) where source.hasPrefix("file://"):
|
||||
method = "PUT"
|
||||
let queryStr = overwrite ? "" : "?@name.conflictBehavior=fail"
|
||||
url = URL(string: self.url(of: dest, modifier: "content").absoluteString + queryStr)!
|
||||
case .copy(source: let source, destination: let dest) where dest.hasPrefix("file://"):
|
||||
method = "GET"
|
||||
url = self.url(of: source, modifier: "content")
|
||||
case .copy(source: let source, destination: _):
|
||||
method = "POST"
|
||||
url = self.url(of: source, modifier: "copy")
|
||||
case .move(source: let source, destination: _):
|
||||
method = "PATCH"
|
||||
url = self.url(of: source)
|
||||
case .remove(path: let path):
|
||||
method = "DELETE"
|
||||
url = self.url(of: path)
|
||||
default: // link
|
||||
fatalError("Unimplemented operation \(operation.description) in \(#file)")
|
||||
}
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = method
|
||||
request.setValue(authentication: self.credential, with: .oAuth2)
|
||||
// Remove gzip to fix availability of progress re. (Oleg Marchik)[https://github.com/evilutioner] PR (#61)
|
||||
if method == "GET" {
|
||||
request.setValue(acceptEncoding: .deflate)
|
||||
request.addValue(acceptEncoding: .identity)
|
||||
}
|
||||
|
||||
switch operation {
|
||||
case .create(path: let path) where path.hasSuffix("/"):
|
||||
request.setValue(contentType: .json)
|
||||
var requestDictionary = [String: AnyObject]()
|
||||
let name = (path as NSString).lastPathComponent
|
||||
requestDictionary["name"] = name as NSString
|
||||
requestDictionary["folder"] = NSDictionary()
|
||||
requestDictionary["@microsoft.graph.conflictBehavior"] = "fail" as NSString
|
||||
request.httpBody = Data(jsonDictionary: requestDictionary)
|
||||
case .copy(let source, let dest) where !source.hasPrefix("file://") && !dest.hasPrefix("file://"),
|
||||
.move(source: let source, destination: let dest):
|
||||
request.setValue(contentType: .json, charset: .utf8)
|
||||
let cdest = correctPath(dest) as NSString
|
||||
var parentReference: [String: AnyObject] = [:]
|
||||
if cdest.hasPrefix("id:") {
|
||||
parentReference["id"] = cdest.components(separatedBy: "/").first?.replacingOccurrences(of: "id:", with: "", options: .anchored) as NSString?
|
||||
} else {
|
||||
parentReference["path"] = ("/drive/root:" as NSString).appendingPathComponent(cdest.deletingLastPathComponent) as NSString
|
||||
}
|
||||
switch self.route {
|
||||
case .drive(uuid: let uuid):
|
||||
parentReference["driveId"] = uuid.uuidString as NSString
|
||||
default:
|
||||
//parentReference["driveId"] = cachedDriveID as NSString? ?? ""
|
||||
break
|
||||
}
|
||||
var requestDictionary = [String: AnyObject]()
|
||||
requestDictionary["parentReference"] = parentReference as NSDictionary
|
||||
requestDictionary["name"] = (cdest as NSString).lastPathComponent as NSString
|
||||
request.httpBody = Data(jsonDictionary: requestDictionary)
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
return request
|
||||
}
|
||||
|
||||
override func serverError(with code: FileProviderHTTPErrorCode, path: String?, data: Data?) -> FileProviderHTTPError {
|
||||
let errorDesc: String?
|
||||
if let response = data?.deserializeJSON() {
|
||||
errorDesc = response["error"]?["message"] as? String
|
||||
} else {
|
||||
errorDesc = data.flatMap({ String(data: $0, encoding: .utf8) })
|
||||
}
|
||||
return FileProviderOneDriveError(code: code, path: path ?? "", serverDescription: errorDesc)
|
||||
}
|
||||
|
||||
override var maxUploadSimpleSupported: Int64 {
|
||||
return 4_194_304 // 4MB!
|
||||
}
|
||||
|
||||
fileprivate func registerNotifcation(path: String, eventHandler: (() -> Void)) {
|
||||
/* There is two ways to monitor folders changing in OneDrive. Either using webooks
|
||||
* which means you have to implement a server to translate it to push notifications
|
||||
* or using apiv2 list_folder/longpoll method. The second one is implemeted here.
|
||||
* Tough webhooks are much more efficient, longpoll is much simpler to implement!
|
||||
* You can implemnt your own webhook service and replace this method accordingly.
|
||||
*/
|
||||
NotImplemented()
|
||||
}
|
||||
fileprivate func unregisterNotifcation(path: String) {
|
||||
NotImplemented()
|
||||
}
|
||||
|
||||
open func publicLink(to path: String, completionHandler: @escaping ((_ link: URL?, _ attribute: FileObject?, _ expiration: Date?, _ error: Error?) -> Void)) {
|
||||
var request = URLRequest(url: self.url(of: path, modifier: "createLink"))
|
||||
request.httpMethod = "POST"
|
||||
let requestDictionary: [String: AnyObject] = ["type": "view" as NSString]
|
||||
request.httpBody = Data(jsonDictionary: requestDictionary)
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var serverError: FileProviderHTTPError?
|
||||
var link: URL?
|
||||
if let response = response as? HTTPURLResponse, response.statusCode >= 400 {
|
||||
let code = FileProviderHTTPErrorCode(rawValue: response.statusCode)
|
||||
serverError = code.flatMap { self.serverError(with: $0, path: path, data: data) }
|
||||
}
|
||||
if let json = data?.deserializeJSON() {
|
||||
if let linkDic = json["link"] as? NSDictionary, let linkStr = linkDic["webUrl"] as? String {
|
||||
link = URL(string: linkStr)
|
||||
}
|
||||
}
|
||||
completionHandler(link, nil, nil, serverError ?? error)
|
||||
})
|
||||
task.resume()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension OneDriveFileProvider: ExtendedFileProvider {
|
||||
open func propertiesOfFileSupported(path: String) -> Bool {
|
||||
return true
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func propertiesOfFile(path: String, completionHandler: @escaping ((_ propertiesDictionary: [String : Any], _ keys: [String], _ error: Error?) -> Void)) -> Progress? {
|
||||
var request = URLRequest(url: url(of: path))
|
||||
request.httpMethod = "GET"
|
||||
request.setValue(authentication: credential, with: .oAuth2)
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var serverError: FileProviderHTTPError?
|
||||
var dic = [String: Any]()
|
||||
var keys = [String]()
|
||||
if let response = response as? HTTPURLResponse {
|
||||
let code = FileProviderHTTPErrorCode(rawValue: response.statusCode)
|
||||
serverError = code.flatMap { self.serverError(with: $0, path: path, data: data) }
|
||||
}
|
||||
if let json = data?.deserializeJSON() {
|
||||
(dic, keys) = self.mapMediaInfo(json)
|
||||
}
|
||||
completionHandler(dic, keys, serverError ?? error)
|
||||
})
|
||||
task.resume()
|
||||
return nil
|
||||
}
|
||||
|
||||
#if os(macOS) || os(iOS) || os(tvOS)
|
||||
open func thumbnailOfFileSupported(path: String) -> Bool {
|
||||
let fileExt = (path as NSString).pathExtension.lowercased()
|
||||
switch fileExt {
|
||||
case "jpg", "jpeg", "bmp", "gif", "png", "tif", "tiff":
|
||||
return true
|
||||
case "mp3", "aac", "m4a", "wma":
|
||||
return true
|
||||
case "mp4", "mpg", "3gp", "mov", "avi", "wmv":
|
||||
return true
|
||||
case "doc", "docx", "xls", "xlsx", "ppt", "pptx", "pdf":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func thumbnailOfFile(path: String, dimension: CGSize?, completionHandler: @escaping ((_ image: ImageClass?, _ error: Error?) -> Void)) -> Progress? {
|
||||
let thumbQuery: String
|
||||
switch dimension.map( {max($0.width, $0.height) } ) ?? 0 {
|
||||
case 0...96: thumbQuery = "small"
|
||||
case 97...176: thumbQuery = "medium"
|
||||
default: thumbQuery = "large"
|
||||
}
|
||||
let url = self.url(of: path, modifier: "thumbnails")
|
||||
.appendingPathComponent("0").appendingPathComponent(thumbQuery)
|
||||
.appendingPathComponent("content")
|
||||
var request = URLRequest(url: url)
|
||||
request.setValue(authentication: credential, with: .oAuth2)
|
||||
let task = self.session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var image: ImageClass? = nil
|
||||
if let code = (response as? HTTPURLResponse)?.statusCode , code >= 400, let rCode = FileProviderHTTPErrorCode(rawValue: code) {
|
||||
let responseError = self.serverError(with: rCode, path: path, data: data)
|
||||
completionHandler(nil, responseError)
|
||||
return
|
||||
}
|
||||
image = data.flatMap(ImageClass.init(data:))
|
||||
completionHandler(image, error)
|
||||
})
|
||||
task.resume()
|
||||
return nil
|
||||
}
|
||||
#endif
|
||||
}
|
||||
+264
-137
@@ -12,59 +12,68 @@ import Foundation
|
||||
public struct FileProviderOneDriveError: FileProviderHTTPError {
|
||||
public let code: FileProviderHTTPErrorCode
|
||||
public let path: String
|
||||
public let errorDescription: String?
|
||||
public let serverDescription: String?
|
||||
}
|
||||
|
||||
/// Containts path, url and attributes of a OneDrive file or resource.
|
||||
public final class OneDriveFileObject: FileObject {
|
||||
internal init(baseURL: URL?, name: String, path: String) {
|
||||
var rpath = path
|
||||
if path.hasPrefix("/") {
|
||||
rpath.remove(at: rpath.startIndex)
|
||||
}
|
||||
let url = URL(string: rpath, relativeTo: baseURL) ?? URL(string: path)!
|
||||
super.init(url: url, name: name, path: path)
|
||||
}
|
||||
|
||||
internal convenience init? (baseURL: URL?, drive: String, jsonStr: String) {
|
||||
internal convenience init? (baseURL: URL?, route: OneDriveFileProvider.Route, jsonStr: String) {
|
||||
guard let json = jsonStr.deserializeJSON() else { return nil }
|
||||
self.init(baseURL: baseURL, drive: drive, json: json)
|
||||
self.init(baseURL: baseURL, route: route, json: json)
|
||||
}
|
||||
|
||||
internal convenience init? (baseURL: URL?, drive: String, json: [String: AnyObject]) {
|
||||
internal init? (baseURL: URL?, route: OneDriveFileProvider.Route, json: [String: AnyObject]) {
|
||||
guard let name = json["name"] as? String else { return nil }
|
||||
guard let path = (json["parentReference"] as? NSDictionary)?["path"] as? String else { return nil }
|
||||
var lPath = path.replacingOccurrences(of: "/drive/\(drive)", with: "/", options: .anchored, range: nil)
|
||||
lPath = lPath.replacingOccurrences(of: "/:", with: "", options: .anchored)
|
||||
lPath = lPath.replacingOccurrences(of: "//", with: "", options: .anchored)
|
||||
self.init(baseURL: baseURL, name: name, path: lPath)
|
||||
guard let id = json["id"] as? String else { return nil }
|
||||
let path: String
|
||||
if let refpath = json["parentReference"]?["path"] as? String {
|
||||
let parentPath: String
|
||||
if let colonIndex = refpath.index(of: ":") {
|
||||
#if swift(>=4.0)
|
||||
parentPath = String(refpath[refpath.index(after: colonIndex)...])
|
||||
#else
|
||||
parentPath = refpath.substring(from: refpath.index(after: colonIndex))
|
||||
#endif
|
||||
} else {
|
||||
parentPath = refpath
|
||||
}
|
||||
path = (parentPath as NSString).appendingPathComponent(name)
|
||||
} else {
|
||||
path = "id:\(id)"
|
||||
}
|
||||
let url = baseURL.map { OneDriveFileObject.url(of: path, modifier: nil, baseURL: $0, route: route) }
|
||||
super.init(url: url, name: name, path: path)
|
||||
self.id = id
|
||||
self.size = (json["size"] as? NSNumber)?.int64Value ?? -1
|
||||
self.modifiedDate = Date(rfcString: json["lastModifiedDateTime"] as? String ?? "")
|
||||
self.creationDate = Date(rfcString: json["createdDateTime"] as? String ?? "")
|
||||
self.type = (json["folder"] as? String) != nil ? .directory : .regular
|
||||
self.id = json["id"] as? String
|
||||
self.childrensCount = json["folder"]?["childCount"] as? Int
|
||||
self.modifiedDate = (json["lastModifiedDateTime"] as? String).flatMap { Date(rfcString: $0) }
|
||||
self.creationDate = (json["createdDateTime"] as? String).flatMap { Date(rfcString: $0) }
|
||||
self.type = json["folder"] != nil ? .directory : .regular
|
||||
self.contentType = (json["file"]?["mimeType"] as? String).flatMap(ContentMIMEType.init(rawValue:)) ?? .stream
|
||||
self.entryTag = json["eTag"] as? String
|
||||
let hashes = json["file"]?["hashes"] as? NSDictionary
|
||||
// checks for both sha1 or quickXor. First is available in personal drives, second in business one.
|
||||
self.fileHash = (hashes?["sha1Hash"] as? String) ?? (hashes?["quickXorHash"] as? String)
|
||||
}
|
||||
|
||||
/// The document identifier is a value assigned by the OneDrive to a file.
|
||||
/// This value is used to identify the document regardless of where it is moved on a volume.
|
||||
/// The identifier persists across system restarts.
|
||||
open internal(set) var id: String? {
|
||||
get {
|
||||
return allValues[.documentIdentifierKey] as? String
|
||||
return allValues[.fileResourceIdentifierKey] as? String
|
||||
}
|
||||
set {
|
||||
allValues[.documentIdentifierKey] = newValue
|
||||
allValues[.fileResourceIdentifierKey] = newValue
|
||||
}
|
||||
}
|
||||
|
||||
/// MIME type of file contents returned by OneDrive server.
|
||||
open internal(set) var contentType: String {
|
||||
open internal(set) var contentType: ContentMIMEType {
|
||||
get {
|
||||
return allValues[.mimeTypeKey] as? String ?? ""
|
||||
return (allValues[.mimeTypeKey] as? String).flatMap(ContentMIMEType.init(rawValue:)) ?? .stream
|
||||
}
|
||||
set {
|
||||
allValues[.mimeTypeKey] = newValue
|
||||
allValues[.mimeTypeKey] = newValue.rawValue
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,116 +86,235 @@ public final class OneDriveFileObject: FileObject {
|
||||
allValues[.entryTagKey] = newValue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// codebeat:disable[ARITY]
|
||||
internal extension OneDriveFileProvider {
|
||||
func list(_ path: String, cursor: URL? = nil, prevContents: [OneDriveFileObject] = [], completionHandler: @escaping ((_ contents: [FileObject], _ cursor: String?, _ error: Error?) -> Void)) {
|
||||
let url = cursor ?? self.url(of: path, modifier: "children")
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "GET"
|
||||
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var responseError: FileProviderOneDriveError?
|
||||
var files = prevContents
|
||||
if let code = (response as? HTTPURLResponse)?.statusCode , code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) {
|
||||
responseError = FileProviderOneDriveError(code: rCode, path: path, errorDescription: String(data: data ?? Data(), encoding: .utf8))
|
||||
}
|
||||
if let json = data?.deserializeJSON() {
|
||||
if let entries = json["value"] as? [AnyObject] , entries.count > 0 {
|
||||
for entry in entries {
|
||||
if let entry = entry as? [String: AnyObject], let file = OneDriveFileObject(baseURL: self.baseURL, drive: self.drive, json: entry) {
|
||||
files.append(file)
|
||||
}
|
||||
}
|
||||
let ncursor: URL? = (json["@odata.nextLink"] as? String).flatMap { URL(string: $0) }
|
||||
let hasmore = ncursor != nil
|
||||
if hasmore {
|
||||
self.list(path, cursor: ncursor, prevContents: files, completionHandler: completionHandler)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
completionHandler(files, nil, responseError ?? error)
|
||||
})
|
||||
task.taskDescription = FileOperationType.fetch(path: path).json
|
||||
task.resume()
|
||||
|
||||
/// Calculated hash from OneDrive server. Hex string SHA1 in personal or Base65 string [QuickXOR](https://dev.onedrive.com/snippets/quickxorhash.htm) in business drives.
|
||||
open internal(set) var fileHash: String? {
|
||||
get {
|
||||
return allValues[.documentIdentifierKey] as? String
|
||||
}
|
||||
set {
|
||||
allValues[.documentIdentifierKey] = newValue
|
||||
}
|
||||
}
|
||||
|
||||
func upload_simple(_ targetPath: String, data: Data? = nil , localFile: URL? = nil, modifiedDate: Date = Date(), overwrite: Bool, operation: FileOperationType, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
let size = data?.count ?? (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
|
||||
static func url(of path: String, modifier: String?, baseURL: URL, route: OneDriveFileProvider.Route) -> URL {
|
||||
var url: URL = baseURL
|
||||
let isId = path.hasPrefix("id:")
|
||||
var rpath: String = path.replacingOccurrences(of: "id:", with: "", options: .anchored)
|
||||
|
||||
//url.appendPathComponent("v1.0")
|
||||
url.appendPathComponent(route.drivePath)
|
||||
|
||||
if rpath.isEmpty {
|
||||
url.appendPathComponent("root")
|
||||
} else if isId {
|
||||
url.appendPathComponent("items")
|
||||
} else {
|
||||
url.appendPathComponent("root:")
|
||||
}
|
||||
let queryStr = overwrite ? "" : "?@name.conflictBehavior=fail"
|
||||
let url = self.url(of: targetPath, modifier: "content\(queryStr)")
|
||||
|
||||
rpath = rpath.trimmingCharacters(in: pathTrimSet)
|
||||
|
||||
switch (modifier == nil, rpath.isEmpty, isId) {
|
||||
case (true, false, _):
|
||||
url.appendPathComponent(rpath)
|
||||
case (true, true, _):
|
||||
break
|
||||
case (false, true, _):
|
||||
url.appendPathComponent(modifier!)
|
||||
case (false, false, true):
|
||||
url.appendPathComponent(rpath)
|
||||
url.appendPathComponent(modifier!)
|
||||
case (false, false, false):
|
||||
url.appendPathComponent(rpath + ":")
|
||||
url.appendPathComponent(modifier!)
|
||||
}
|
||||
|
||||
return url
|
||||
}
|
||||
|
||||
static func relativePathOf(url: URL, baseURL: URL?, route: OneDriveFileProvider.Route) -> String {
|
||||
let base = baseURL?.appendingPathComponent(route.drivePath).path ?? ""
|
||||
|
||||
let crudePath = url.path.replacingOccurrences(of: base, with: "", options: .anchored)
|
||||
.trimmingCharacters(in: CharacterSet(charactersIn: "/"))
|
||||
|
||||
switch crudePath {
|
||||
case hasPrefix("items/"):
|
||||
let components = (crudePath as NSString).pathComponents
|
||||
return components.dropFirst().first.map { "id:\($0)" } ?? ""
|
||||
case hasPrefix("root:"):
|
||||
return crudePath.components(separatedBy: ":").dropFirst().first ?? ""
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal extension OneDriveFileProvider {
|
||||
internal func upload_multipart_data(_ targetPath: String, data: Data, operation: FileOperationType,
|
||||
overwrite: Bool, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
return self.upload_multipart(targetPath, operation: operation, size: Int64(data.count), overwrite: overwrite, dataProvider: {
|
||||
let range = $0.clamped(to: 0..<Int64(data.count))
|
||||
return data[range]
|
||||
}, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
internal func upload_multipart_file(_ targetPath: String, file: URL, operation: FileOperationType,
|
||||
overwrite: Bool, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
// upload task can't handle uploading file
|
||||
|
||||
return self.upload_multipart(targetPath, operation: operation, size: file.fileSize, overwrite: overwrite, dataProvider: { range in
|
||||
guard let handle = FileHandle(forReadingAtPath: file.path) else {
|
||||
throw self.cocoaError(targetPath, code: .fileNoSuchFile)
|
||||
}
|
||||
|
||||
defer {
|
||||
handle.closeFile()
|
||||
}
|
||||
|
||||
let offset = range.lowerBound
|
||||
handle.seek(toFileOffset: UInt64(offset))
|
||||
guard Int64(handle.offsetInFile) == offset else {
|
||||
throw self.cocoaError(targetPath, code: .fileReadTooLarge)
|
||||
}
|
||||
|
||||
return handle.readData(ofLength: range.count)
|
||||
}, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
private func upload_multipart(_ targetPath: String, operation: FileOperationType, size: Int64, overwrite: Bool,
|
||||
dataProvider: @escaping (Range<Int64>) throws -> Data, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
guard size > 0 else { return nil }
|
||||
|
||||
let progress = Progress(totalUnitCount: size)
|
||||
progress.setUserInfoObject(operation, forKey: .fileProvderOperationTypeKey)
|
||||
progress.kind = .file
|
||||
progress.setUserInfoObject(Progress.FileOperationKind.downloading, forKey: .fileOperationKindKey)
|
||||
|
||||
let createURL = self.url(of: targetPath, modifier: "createUploadSession")
|
||||
var createRequest = URLRequest(url: createURL)
|
||||
createRequest.httpMethod = "POST"
|
||||
createRequest.setValue(authentication: self.credential, with: .oAuth2)
|
||||
createRequest.setValue(contentType: .json)
|
||||
if overwrite {
|
||||
createRequest.httpBody = Data(jsonDictionary: ["item": ["@microsoft.graph.conflictBehavior": "replace"] as NSDictionary])
|
||||
} else {
|
||||
createRequest.httpBody = Data(jsonDictionary: ["item": ["@microsoft.graph.conflictBehavior": "fail"] as NSDictionary])
|
||||
}
|
||||
let createSessionTask = session.dataTask(with: createRequest) { (data, response, error) in
|
||||
if let error = error {
|
||||
completionHandler?(error)
|
||||
return
|
||||
}
|
||||
|
||||
if let data = data, let json = data.deserializeJSON(),
|
||||
let uploadURL = (json["uploadUrl"] as? String).flatMap(URL.init(string:)) {
|
||||
self.upload_multipart(url: uploadURL, operation: operation, size: size, progress: progress, dataProvider: dataProvider, completionHandler: completionHandler)
|
||||
}
|
||||
}
|
||||
createSessionTask.resume()
|
||||
|
||||
return progress
|
||||
}
|
||||
|
||||
private func upload_multipart(url: URL, operation: FileOperationType, size: Int64, range: Range<Int64>? = nil, uploadedSoFar: Int64 = 0,
|
||||
progress: Progress, dataProvider: @escaping (Range<Int64>) throws -> Data, completionHandler: SimpleCompletionHandler) {
|
||||
guard !progress.isCancelled else { return }
|
||||
var progress = progress
|
||||
|
||||
let maximumSize: Int64 = 10_485_760 // Recommended by OneDrive documentations and divides evenly by 320 KiB, max 60MiB.
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "PUT"
|
||||
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
|
||||
request.setValue("application/octet-stream", forHTTPHeaderField: "Content-Type")
|
||||
let task: URLSessionUploadTask
|
||||
if let data = data {
|
||||
task = session.uploadTask(with: request, from: data)
|
||||
} else if let localFile = localFile {
|
||||
task = session.uploadTask(with: request, fromFile: localFile)
|
||||
request.setValue(authentication: self.credential, with: .oAuth2)
|
||||
|
||||
let finalRange: Range<Int64>
|
||||
if let range = range {
|
||||
if range.count > maximumSize {
|
||||
finalRange = range.lowerBound..<(range.upperBound + maximumSize)
|
||||
} else {
|
||||
finalRange = range
|
||||
}
|
||||
} else {
|
||||
return nil
|
||||
finalRange = 0..<min(maximumSize, size)
|
||||
}
|
||||
request.setValue(contentRange: finalRange, totalBytes: size)
|
||||
|
||||
let data: Data
|
||||
do {
|
||||
data = try dataProvider(finalRange)
|
||||
} catch {
|
||||
dispatch_queue.async {
|
||||
completionHandler?(error)
|
||||
}
|
||||
self.delegateNotify(operation, error: error)
|
||||
return
|
||||
}
|
||||
let task = session.uploadTask(with: request, from: data)
|
||||
|
||||
var dictionary: [String: AnyObject] = ["type": operation.description as NSString]
|
||||
dictionary["source"] = operation.source as NSString?
|
||||
dictionary["dest"] = operation.destination as NSString?
|
||||
dictionary["uploadedBytes"] = uploadedSoFar as NSNumber
|
||||
dictionary["totalBytes"] = data.count as NSNumber
|
||||
task.taskDescription = String(jsonDictionary: dictionary)
|
||||
task.addObserver(self.sessionDelegate!, forKeyPath: #keyPath(URLSessionTask.countOfBytesSent), options: .new, context: &progress)
|
||||
progress.cancellationHandler = { [weak task, weak self] in
|
||||
task?.cancel()
|
||||
var deleteRequest = URLRequest(url: url)
|
||||
deleteRequest.httpMethod = "DELETE"
|
||||
self?.session.dataTask(with: deleteRequest).resume()
|
||||
}
|
||||
progress.setUserInfoObject(Date(), forKey: .startingTimeKey)
|
||||
|
||||
var allData = Data()
|
||||
dataCompletionHandlersForTasks[session.sessionDescription!]?[task.taskIdentifier] = { data in
|
||||
allData.append(data)
|
||||
}
|
||||
// We retain self here intentionally to allow resuming upload, This behavior may change anytime!
|
||||
completionHandlersForTasks[session.sessionDescription!]?[task.taskIdentifier] = { [weak task] error in
|
||||
if let error = error {
|
||||
progress.cancel()
|
||||
completionHandler?(error)
|
||||
self.delegateNotify(operation, error: error)
|
||||
return
|
||||
}
|
||||
|
||||
guard let json = allData.deserializeJSON() else {
|
||||
let error = URLError(.badServerResponse, userInfo: [NSURLErrorKey: url, NSURLErrorFailingURLErrorKey: url, NSURLErrorFailingURLStringErrorKey: url.absoluteString])
|
||||
completionHandler?(error)
|
||||
self.delegateNotify(operation, error: error)
|
||||
return
|
||||
}
|
||||
|
||||
if let _ = json["error"] {
|
||||
let code = ((task?.response as? HTTPURLResponse)?.statusCode).flatMap(FileProviderHTTPErrorCode.init(rawValue:)) ?? .badRequest
|
||||
let error = self.serverError(with: code, path: self.relativePathOf(url: url), data: allData)
|
||||
completionHandler?(error)
|
||||
self.delegateNotify(operation, error: error)
|
||||
return
|
||||
}
|
||||
|
||||
if let ranges = json["nextExpectedRanges"] as? [String], let firstRange = ranges.first {
|
||||
let uploaded = uploadedSoFar + Int64(finalRange.count)
|
||||
let comp = firstRange.components(separatedBy: "-")
|
||||
let lower = comp.first.flatMap(Int64.init) ?? uploaded
|
||||
let upper = comp.dropFirst().first.flatMap(Int64.init) ?? Int64.max
|
||||
let range = Range<Int64>(uncheckedBounds: (lower: lower, upper: upper))
|
||||
self.upload_multipart(url: url, operation: operation, size: size, range: range, uploadedSoFar: uploaded, progress: progress,
|
||||
dataProvider: dataProvider, completionHandler: completionHandler)
|
||||
return
|
||||
}
|
||||
|
||||
if let _ = json["id"] as? String {
|
||||
completionHandler?(nil)
|
||||
self.delegateNotify(operation)
|
||||
}
|
||||
}
|
||||
|
||||
completionHandlersForTasks[task.taskIdentifier] = { [weak self] error in
|
||||
var responseError: FileProviderOneDriveError?
|
||||
if let code = (task.response as? HTTPURLResponse)?.statusCode , code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) {
|
||||
// We can't fetch server result from delegate!
|
||||
responseError = FileProviderOneDriveError(code: rCode, path: targetPath, errorDescription: nil)
|
||||
}
|
||||
completionHandler?(responseError ?? error)
|
||||
self?.delegateNotify(.create(path: targetPath), error: responseError ?? error)
|
||||
}
|
||||
task.taskDescription = operation.json
|
||||
task.resume()
|
||||
return RemoteOperationHandle(operationType: operation, tasks: [task])
|
||||
}
|
||||
|
||||
func search(_ startPath: String = "", query: String, next: URL? = nil, foundItem:@escaping ((_ file: OneDriveFileObject) -> Void), completionHandler: @escaping ((_ error: Error?) -> Void)) {
|
||||
let url: URL
|
||||
let q = query.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
|
||||
url = next ?? self.url(of: startPath, modifier: "view.search?q=\(q)")
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "GET"
|
||||
request.setValue("Bearer \(credential?.password ?? "")", forHTTPHeaderField: "Authorization")
|
||||
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var responseError: FileProviderOneDriveError?
|
||||
if let code = (response as? HTTPURLResponse)?.statusCode , code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) {
|
||||
responseError = FileProviderOneDriveError(code: rCode, path: startPath, errorDescription: String(data: data ?? Data(), encoding: .utf8))
|
||||
}
|
||||
if let json = data?.deserializeJSON() {
|
||||
if let entries = json["value"] as? [AnyObject] , entries.count > 0 {
|
||||
for entry in entries {
|
||||
if let entry = entry as? [String: AnyObject], let file = OneDriveFileObject(baseURL: self.baseURL, drive: self.drive, json: entry) {
|
||||
foundItem(file)
|
||||
}
|
||||
}
|
||||
let next: URL? = (json["@odata.nextLink"] as? String).flatMap { URL(string: $0) }
|
||||
if let next = next {
|
||||
self.search(startPath, query: query, next: next, foundItem: foundItem, completionHandler: completionHandler)
|
||||
} else {
|
||||
completionHandler(responseError ?? error)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
completionHandler(responseError ?? error)
|
||||
})
|
||||
task.resume()
|
||||
}
|
||||
}
|
||||
// codebeat:enable[ARITY]
|
||||
|
||||
internal extension OneDriveFileProvider {
|
||||
static let dateFormatter = DateFormatter()
|
||||
static let decimalFormatter = NumberFormatter()
|
||||
|
||||
@@ -248,8 +376,17 @@ internal extension OneDriveFileProvider {
|
||||
|
||||
if let audio = json["audio"] as? [String: Any] {
|
||||
for (key, value) in audio {
|
||||
var value = value
|
||||
if key == "bitrate" || key == "isVariableBitrate" { continue }
|
||||
let casedKey = spaceCamelCase(key)
|
||||
switch casedKey {
|
||||
case "Duration":
|
||||
value = (value as? Int64).map { (TimeInterval($0) / 1000).formatshort } as Any
|
||||
case "Bitrate":
|
||||
value = (value as? Int64).map { "\($0)kbps" } as Any
|
||||
default:
|
||||
break
|
||||
}
|
||||
add(key: casedKey, value: value)
|
||||
}
|
||||
}
|
||||
@@ -258,14 +395,4 @@ internal extension OneDriveFileProvider {
|
||||
|
||||
return (dic, keys)
|
||||
}
|
||||
|
||||
func delegateNotify(_ operation: FileOperationType, error: Error?) {
|
||||
DispatchQueue.main.async(execute: {
|
||||
if error == nil {
|
||||
self.delegate?.fileproviderSucceed(self, operation: operation)
|
||||
} else {
|
||||
self.delegate?.fileproviderFailed(self, operation: operation)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Regular → Executable
+155
-115
@@ -8,170 +8,211 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
/// Allows to get progress or cancel an in-progress operation, for remote, `URLSession` based providers.
|
||||
/// This class keeps strong reference to tasks.
|
||||
open class RemoteOperationHandle: OperationHandle {
|
||||
|
||||
internal var tasks: [URLSessionTask]
|
||||
|
||||
open private(set) var operationType: FileOperationType
|
||||
|
||||
init(operationType: FileOperationType, tasks: [URLSessionTask]) {
|
||||
self.operationType = operationType
|
||||
self.tasks = tasks
|
||||
}
|
||||
|
||||
internal func add(task: URLSessionTask) {
|
||||
tasks.append(task)
|
||||
}
|
||||
|
||||
internal func reape() {
|
||||
self.tasks = tasks.filter { $0.state != .completed }
|
||||
}
|
||||
|
||||
open var bytesSoFar: Int64 {
|
||||
return tasks.reduce(0) {
|
||||
switch $1 {
|
||||
case let task as URLSessionUploadTask:
|
||||
return $0 + task.countOfBytesSent
|
||||
case let task as FileProviderStreamTask:
|
||||
return $0 + task.countOfBytesSent + task.countOfBytesReceived
|
||||
default:
|
||||
return $0 + $1.countOfBytesReceived
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
open var totalBytes: Int64 {
|
||||
return tasks.reduce(0) {
|
||||
switch $1 {
|
||||
case let task as URLSessionUploadTask:
|
||||
return $0 + task.countOfBytesExpectedToSend
|
||||
case let task as FileProviderStreamTask:
|
||||
return $0 + task.countOfBytesExpectedToSend + task.countOfBytesExpectedToReceive
|
||||
default:
|
||||
return $0 + $1.countOfBytesExpectedToReceive
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
open func cancel() -> Bool {
|
||||
var canceled = false
|
||||
for taskbox in tasks {
|
||||
taskbox.cancel()
|
||||
canceled = true
|
||||
}
|
||||
return canceled
|
||||
}
|
||||
|
||||
open var inProgress: Bool {
|
||||
return tasks.reduce(false) { $0 || $1.state == .running }
|
||||
}
|
||||
}
|
||||
|
||||
/// A protocol defines properties for errors returned by HTTP/S based providers.
|
||||
/// Including Dropbox, OneDrive and WebDAV.
|
||||
public protocol FileProviderHTTPError: Error, CustomStringConvertible {
|
||||
public protocol FileProviderHTTPError: LocalizedError, CustomStringConvertible {
|
||||
/// HTTP status codes as an enum.
|
||||
typealias Code = FileProviderHTTPErrorCode
|
||||
/// HTTP status code returned for error by server.
|
||||
var code: FileProviderHTTPErrorCode { get }
|
||||
/// Path of file/folder casued that error
|
||||
var path: String { get }
|
||||
/// Contents returned by server as error description
|
||||
var errorDescription: String? { get }
|
||||
var serverDescription: String? { get }
|
||||
}
|
||||
|
||||
extension FileProviderHTTPError {
|
||||
public var description: String {
|
||||
return code.description
|
||||
return "Status \(code.rawValue): \(code.description)"
|
||||
}
|
||||
|
||||
public var localizedDescription: String {
|
||||
return description
|
||||
public var errorDescription: String? {
|
||||
return "Status \(code.rawValue): \(code.description)"
|
||||
}
|
||||
}
|
||||
|
||||
internal var completionHandlersForTasks = [Int: SimpleCompletionHandler]()
|
||||
internal var downloadCompletionHandlersForTasks = [Int: (URL) -> Void]()
|
||||
internal var dataCompletionHandlersForTasks = [Int: (Data) -> Void]()
|
||||
internal var completionHandlersForTasks = [String: [Int: SimpleCompletionHandler]]()
|
||||
internal var downloadCompletionHandlersForTasks = [String: [Int: (URL) -> Void]]()
|
||||
internal var dataCompletionHandlersForTasks = [String: [Int: (Data) -> Void]]()
|
||||
internal var responseCompletionHandlersForTasks = [String: [Int: (URLResponse) -> Void]]()
|
||||
|
||||
class SessionDelegate: NSObject, URLSessionDataDelegate, URLSessionDownloadDelegate, URLSessionStreamDelegate {
|
||||
internal func initEmptySessionHandler(_ uuid: String) {
|
||||
completionHandlersForTasks[uuid] = [:]
|
||||
downloadCompletionHandlersForTasks[uuid] = [:]
|
||||
dataCompletionHandlersForTasks[uuid] = [:]
|
||||
responseCompletionHandlersForTasks[uuid] = [:]
|
||||
}
|
||||
|
||||
internal func removeSessionHandler(for uuid: String) {
|
||||
_ = completionHandlersForTasks.removeValue(forKey: uuid)
|
||||
_ = downloadCompletionHandlersForTasks.removeValue(forKey: uuid)
|
||||
_ = dataCompletionHandlersForTasks.removeValue(forKey: uuid)
|
||||
_ = responseCompletionHandlersForTasks.removeValue(forKey: uuid)
|
||||
}
|
||||
|
||||
/// All objects set to `FileProviderRemote.session` must be an instance of this class
|
||||
final public class SessionDelegate: NSObject, URLSessionDataDelegate, URLSessionDownloadDelegate, URLSessionStreamDelegate {
|
||||
|
||||
weak var fileProvider: (FileProviderBasicRemote & FileProviderOperations)?
|
||||
var credential: URLCredential?
|
||||
|
||||
var finishDownloadHandler: ((_ session: URLSession, _ downloadTask: URLSessionDownloadTask, _ didFinishDownloadingToURL: URL) -> Void)?
|
||||
var didSendDataHandler: ((_ session: URLSession, _ task: URLSessionTask, _ bytesSent: Int64, _ totalBytesSent: Int64, _ totalBytesExpectedToSend: Int64) -> Void)?
|
||||
var didReceivedData: ((_ session: URLSession, _ downloadTask: URLSessionDownloadTask, _ bytesWritten: Int64, _ totalBytesWritten: Int64, _ totalBytesExpectedToWrite: Int64) -> Void)?
|
||||
var didBecomeStream :((_ session: URLSession, _ taskId: Int, _ didBecome: InputStream, _ outputStream: OutputStream) -> Void)?
|
||||
|
||||
init(fileProvider: FileProviderBasicRemote & FileProviderOperations, credential: URLCredential?) {
|
||||
public init(fileProvider: FileProviderBasicRemote & FileProviderOperations) {
|
||||
self.fileProvider = fileProvider
|
||||
self.credential = credential
|
||||
self.credential = fileProvider.credential
|
||||
}
|
||||
|
||||
open override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
|
||||
if let progress = context?.load(as: Progress.self), let newVal = change?[.newKey] as? Int64 {
|
||||
switch keyPath ?? "" {
|
||||
case #keyPath(URLSessionTask.countOfBytesReceived):
|
||||
progress.completedUnitCount = newVal
|
||||
if let startTime = progress.userInfo[ProgressUserInfoKey.startingTimeKey] as? Date, let task = object as? URLSessionTask {
|
||||
let elapsed = Date().timeIntervalSince(startTime)
|
||||
let throughput = Double(newVal) / elapsed
|
||||
progress.setUserInfoObject(NSNumber(value: throughput), forKey: .throughputKey)
|
||||
if task.countOfBytesExpectedToReceive > 0 {
|
||||
let remain = task.countOfBytesExpectedToReceive - task.countOfBytesReceived
|
||||
let estimatedTimeRemaining = Double(remain) / elapsed
|
||||
progress.setUserInfoObject(NSNumber(value: estimatedTimeRemaining), forKey: .estimatedTimeRemainingKey)
|
||||
}
|
||||
}
|
||||
case #keyPath(URLSessionTask.countOfBytesSent):
|
||||
if let startTime = progress.userInfo[ProgressUserInfoKey.startingTimeKey] as? Date, let task = object as? URLSessionTask {
|
||||
let elapsed = Date().timeIntervalSince(startTime)
|
||||
let throughput = Double(newVal) / elapsed
|
||||
progress.setUserInfoObject(NSNumber(value: throughput), forKey: .throughputKey)
|
||||
|
||||
// wokaround for multipart uploading
|
||||
let json = task.taskDescription?.deserializeJSON()
|
||||
let uploadedBytes = ((json?["uploadedBytes"] as? Int64) ?? 0) + newVal
|
||||
let totalBytes = (json?["totalBytes"] as? Int64) ?? task.countOfBytesExpectedToSend
|
||||
progress.completedUnitCount = uploadedBytes
|
||||
if totalBytes > 0 {
|
||||
let remain = totalBytes - uploadedBytes
|
||||
let estimatedTimeRemaining = Double(remain) / elapsed
|
||||
progress.setUserInfoObject(NSNumber(value: estimatedTimeRemaining), forKey: .estimatedTimeRemainingKey)
|
||||
}
|
||||
} else {
|
||||
progress.completedUnitCount = newVal
|
||||
}
|
||||
case #keyPath(URLSessionTask.countOfBytesExpectedToReceive), #keyPath(URLSessionTask.countOfBytesExpectedToSend):
|
||||
progress.totalUnitCount = newVal
|
||||
default:
|
||||
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// codebeat:disable[ARITY]
|
||||
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
|
||||
let completionHandler = completionHandlersForTasks[task.taskIdentifier] ?? nil
|
||||
completionHandler?(error)
|
||||
completionHandlersForTasks.removeValue(forKey: task.taskIdentifier)
|
||||
}
|
||||
|
||||
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
|
||||
let completionHandler = dataCompletionHandlersForTasks[dataTask.taskIdentifier] ?? nil
|
||||
completionHandler?(data)
|
||||
completionHandlersForTasks.removeValue(forKey: dataTask.taskIdentifier)
|
||||
}
|
||||
|
||||
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
|
||||
self.finishDownloadHandler?(session, downloadTask, location)
|
||||
public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
|
||||
if task is URLSessionUploadTask {
|
||||
task.removeObserver(self, forKeyPath: #keyPath(URLSessionTask.countOfBytesSent))
|
||||
//task.removeObserver(self, forKeyPath: #keyPath(URLSessionTask.countOfBytesExpectedToSend))
|
||||
} else if task is URLSessionDownloadTask {
|
||||
task.removeObserver(self, forKeyPath: #keyPath(URLSessionTask.countOfBytesReceived))
|
||||
task.removeObserver(self, forKeyPath: #keyPath(URLSessionTask.countOfBytesExpectedToReceive))
|
||||
}
|
||||
|
||||
let dcompletionHandler = downloadCompletionHandlersForTasks[downloadTask.taskIdentifier]
|
||||
dcompletionHandler?(location)
|
||||
completionHandlersForTasks.removeValue(forKey: downloadTask.taskIdentifier)
|
||||
_ = dataCompletionHandlersForTasks[session.sessionDescription!]?.removeValue(forKey: task.taskIdentifier)
|
||||
if !(error == nil && task is URLSessionDownloadTask) {
|
||||
let completionHandler = completionHandlersForTasks[session.sessionDescription!]?[task.taskIdentifier] ?? nil
|
||||
completionHandler?(error)
|
||||
_ = completionHandlersForTasks[session.sessionDescription!]?.removeValue(forKey: task.taskIdentifier)
|
||||
}
|
||||
|
||||
guard let json = downloadTask.taskDescription?.deserializeJSON(),
|
||||
guard let json = task.taskDescription?.deserializeJSON(),
|
||||
let op = FileOperationType(json: json), let fileProvider = fileProvider else {
|
||||
return
|
||||
}
|
||||
|
||||
DispatchQueue.main.async {
|
||||
fileProvider.delegate?.fileproviderSucceed(fileProvider, operation: op)
|
||||
switch op {
|
||||
case .fetch:
|
||||
if task is URLSessionDataTask {
|
||||
task.removeObserver(self, forKeyPath: #keyPath(URLSessionTask.countOfBytesReceived))
|
||||
task.removeObserver(self, forKeyPath: #keyPath(URLSessionTask.countOfBytesExpectedToReceive))
|
||||
}
|
||||
default:
|
||||
break
|
||||
}
|
||||
|
||||
if !(task is URLSessionDownloadTask), case FileOperationType.fetch = op {
|
||||
return
|
||||
}
|
||||
if #available(iOS 9.0, macOS 10.11, *) {
|
||||
if task is URLSessionStreamTask {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
fileProvider.delegateNotify(op, error: error)
|
||||
}
|
||||
|
||||
func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
|
||||
self.didSendDataHandler?(session, task, bytesSent, totalBytesSent, totalBytesExpectedToSend)
|
||||
public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
|
||||
let dcompletionHandler = downloadCompletionHandlersForTasks[session.sessionDescription!]?[downloadTask.taskIdentifier]
|
||||
dcompletionHandler?(location)
|
||||
_ = downloadCompletionHandlersForTasks[session.sessionDescription!]?.removeValue(forKey: downloadTask.taskIdentifier)
|
||||
_ = completionHandlersForTasks[session.sessionDescription!]?.removeValue(forKey: downloadTask.taskIdentifier)
|
||||
}
|
||||
|
||||
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse, completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
|
||||
let handler = responseCompletionHandlersForTasks[session.sessionDescription!]?[dataTask.taskIdentifier] ?? nil
|
||||
handler?(response)
|
||||
completionHandler(.allow)
|
||||
_ = responseCompletionHandlersForTasks[session.sessionDescription!]?.removeValue(forKey: dataTask.taskIdentifier)
|
||||
}
|
||||
|
||||
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
|
||||
if let completionHandler = dataCompletionHandlersForTasks[session.sessionDescription!]?[dataTask.taskIdentifier] {
|
||||
/*if let json = dataTask.taskDescription?.deserializeJSON(),
|
||||
let op = FileOperationType(json: json), let fileProvider = fileProvider {
|
||||
fileProvider.delegateNotify(op, progress: Double(dataTask.countOfBytesReceived) / Double(dataTask.countOfBytesExpectedToReceive))
|
||||
}*/
|
||||
completionHandler(data)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public func urlSession(_ session: URLSession, task: URLSessionTask, didSendBodyData bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) {
|
||||
guard let json = task.taskDescription?.deserializeJSON(),
|
||||
let op = FileOperationType(json: json), let fileProvider = fileProvider else {
|
||||
return
|
||||
}
|
||||
|
||||
let progress = Float(totalBytesSent) / Float(totalBytesExpectedToSend)
|
||||
|
||||
DispatchQueue.main.async {
|
||||
fileProvider.delegate?.fileproviderProgress(fileProvider, operation: op, progress: progress)
|
||||
switch op {
|
||||
case .create(path: let path):
|
||||
if path.hasSuffix("/") { return }
|
||||
break
|
||||
case .modify:
|
||||
break
|
||||
case .copy(source: let source, destination: _) where source.hasPrefix("file://"):
|
||||
break
|
||||
default:
|
||||
return
|
||||
}
|
||||
|
||||
// wokaround for multipart uploading
|
||||
let uploadedBytes = (json["uploadedBytes"] as? Int64) ?? 0
|
||||
let totalBytes = (json["totalBytes"] as? Int64) ?? totalBytesExpectedToSend
|
||||
|
||||
fileProvider.delegateNotify(op, progress: Double(uploadedBytes + totalBytesSent) / Double(totalBytes))
|
||||
}
|
||||
|
||||
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
|
||||
self.didReceivedData?(session, downloadTask, bytesWritten, totalBytesWritten, totalBytesExpectedToWrite)
|
||||
public func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
|
||||
if totalBytesExpectedToWrite == NSURLSessionTransferSizeUnknown { return }
|
||||
|
||||
guard let json = downloadTask.taskDescription?.deserializeJSON(),
|
||||
let op = FileOperationType(json: json), let fileProvider = fileProvider else {
|
||||
return
|
||||
}
|
||||
|
||||
DispatchQueue.main.async {
|
||||
fileProvider.delegate?.fileproviderProgress(fileProvider, operation: op, progress: Float(totalBytesWritten) / Float(totalBytesExpectedToWrite))
|
||||
}
|
||||
fileProvider.delegateNotify(op, progress: Double(totalBytesWritten) / Double(totalBytesExpectedToWrite))
|
||||
}
|
||||
|
||||
func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
|
||||
public func urlSession(_ session: URLSession, task: URLSessionTask, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
|
||||
authenticate(didReceive: challenge, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
|
||||
public func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
|
||||
authenticate(didReceive: challenge, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
@@ -185,11 +226,6 @@ class SessionDelegate: NSObject, URLSessionDataDelegate, URLSessionDownloadDeleg
|
||||
completionHandler(.performDefaultHandling, nil)
|
||||
}
|
||||
}
|
||||
|
||||
@available(iOS 9.0, macOS 10.11, *)
|
||||
func urlSession(_ session: URLSession, streamTask: URLSessionStreamTask, didBecome inputStream: InputStream, outputStream: OutputStream) {
|
||||
self.didBecomeStream?(session, streamTask.taskIdentifier, inputStream, outputStream)
|
||||
}
|
||||
}
|
||||
|
||||
/// HTTP status codes as an enum.
|
||||
@@ -335,6 +371,10 @@ public enum FileProviderHTTPErrorCode: Int, CustomStringConvertible {
|
||||
}
|
||||
}
|
||||
|
||||
public var localizedDescription: String {
|
||||
return HTTPURLResponse.localizedString(forStatusCode: self.rawValue)
|
||||
}
|
||||
|
||||
/// Description of status based on first digit which indicated fail or success.
|
||||
public var typeDescription: String {
|
||||
switch self.rawValue {
|
||||
|
||||
+202
-122
@@ -11,151 +11,200 @@ import Foundation
|
||||
// This client implementation is for little-endian platform, namely x86, x64 & arm
|
||||
// For big-endian platforms like PowerPC, there must be a huge overhaul
|
||||
|
||||
protocol SMBProtocolClientDelegate: class {
|
||||
func receivedResponse(client: SMB2ProtocolClient, response: SMBResponse, for: SMBRequest)
|
||||
enum SMBClientError: Error {
|
||||
case streamNotOpened
|
||||
case timedOut
|
||||
}
|
||||
|
||||
class SMB2ProtocolClient: FileProviderStreamTask {
|
||||
var timeout: TimeInterval = 30
|
||||
@objcMembers
|
||||
class SMBClient: NSObject, StreamDelegate {
|
||||
fileprivate var inputStream: InputStream?
|
||||
fileprivate var outputStream: OutputStream?
|
||||
fileprivate var operation_queue: OperationQueue!
|
||||
|
||||
private(set) var lastMessageID: UInt64 = 0
|
||||
private(set) var sessionId: UInt64 = 0
|
||||
private func messageId() -> UInt64 {
|
||||
fileprivate var host: (hostname: String, port: Int)?
|
||||
fileprivate var service: NetService?
|
||||
|
||||
public var timeout: TimeInterval = 30
|
||||
|
||||
internal private(set) var messageId: UInt64 = 0
|
||||
fileprivate func createMessageId() -> UInt64 {
|
||||
defer {
|
||||
lastMessageID += 1
|
||||
messageId += 1
|
||||
}
|
||||
return lastMessageID
|
||||
return messageId
|
||||
}
|
||||
|
||||
internal private(set) var credit: UInt16 = 0
|
||||
fileprivate func consumeCredit() -> UInt16 {
|
||||
if credit > 0 {
|
||||
credit -= 1
|
||||
return credit
|
||||
} else {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
private(set) var sessionId: UInt64 = 0
|
||||
|
||||
private(set) var establishedTrees = Array<SMB2.TreeConnectResponse>()
|
||||
private(set) var requestStack = [Int: SMBRequest]()
|
||||
private(set) var responseStack = [Int: SMBResponse]()
|
||||
|
||||
weak var delegate: SMBProtocolClientDelegate?
|
||||
|
||||
func sendNegotiate(completionHandler: SimpleCompletionHandler) -> UInt64 {
|
||||
let mId = messageId()
|
||||
let smbHeader = SMB2.Header(command: .NEGOTIATE, creditRequestResponse: UInt16(126), messageId: mId, treeId: UInt32(0), sessionId: UInt64(0))
|
||||
let msg = SMB2.NegotiateRequest()
|
||||
let data = createSMB2Message(header: smbHeader, message: msg)
|
||||
self.write(data, timeout: 0, completionHandler: { (e) in
|
||||
completionHandler?(e)
|
||||
})
|
||||
return mId
|
||||
init(host: String, port: Int) {
|
||||
self.host = (host, port)
|
||||
self.operation_queue = OperationQueue()
|
||||
self.operation_queue.name = "FileProviderStreamTask"
|
||||
self.operation_queue.maxConcurrentOperationCount = 1
|
||||
super.init()
|
||||
}
|
||||
|
||||
func sendSessionSetup(completionHandler: SimpleCompletionHandler) -> UInt64 {
|
||||
let mId = messageId()
|
||||
let credit = UInt16(sessionId > 0 ? 124 : 125)
|
||||
let smbHeader = SMB2.Header(command: SMB2.Command.SESSION_SETUP, creditRequestResponse: credit, messageId: mId, treeId: UInt32(0), sessionId: sessionId)
|
||||
let msg = SMB2.SessionSetupRequest(singing: [])
|
||||
let data = createSMB2Message(header: smbHeader, message: msg)
|
||||
self.write(data, timeout: 0, completionHandler: { (e) in
|
||||
if self.sessionId == 0 {
|
||||
self.readData(ofMinLength: 64, maxLength: 65536, timeout: self.timeout, completionHandler: { (data, eof, e2) in
|
||||
// TODO: set session id
|
||||
completionHandler?(e2 ?? e)
|
||||
})
|
||||
}
|
||||
})
|
||||
return mId
|
||||
deinit {
|
||||
close()
|
||||
}
|
||||
|
||||
func sendTreeConnect(completionHandler: SimpleCompletionHandler) -> UInt64 {
|
||||
let req = self.currentRequest ?? self.originalRequest
|
||||
guard let url = req?.url, let host = url.host else {
|
||||
return 0
|
||||
}
|
||||
let mId = messageId()
|
||||
let smbHeader = SMB2.Header(command: .TREE_CONNECT, creditRequestResponse: 123, messageId: mId, treeId: 0, sessionId: sessionId)
|
||||
let cmp = url.pathComponents
|
||||
let share = cmp.first ?? ""
|
||||
let tcHeader = SMB2.TreeConnectRequest.Header(flags: [])
|
||||
let msg = SMB2.TreeConnectRequest(header: tcHeader, host: host, share: share)
|
||||
let data = createSMB2Message(header: smbHeader, message: msg!)
|
||||
self.write(data, timeout: 0, completionHandler: { (e) in
|
||||
completionHandler?(e)
|
||||
|
||||
})
|
||||
return mId
|
||||
}
|
||||
|
||||
func sendTreeDisconnect(id treeId: UInt32, completionHandler: SimpleCompletionHandler) -> UInt64 {
|
||||
let mId = messageId()
|
||||
let smbHeader = SMB2.Header(command: .TREE_DISCONNECT, creditRequestResponse: 111, messageId: mId, treeId: treeId, sessionId: sessionId)
|
||||
let msg = SMB2.TreeDisconnect()
|
||||
let data = createSMB2Message(header: smbHeader, message: msg)
|
||||
self.write(data, timeout: 0, completionHandler: { (e) in
|
||||
completionHandler?(e)
|
||||
})
|
||||
return mId
|
||||
}
|
||||
|
||||
func sendLogoff(id treeId: UInt32, completionHandler: SimpleCompletionHandler) -> UInt64 {
|
||||
let mId = messageId()
|
||||
let smbHeader = SMB2.Header(command: .LOGOFF, creditRequestResponse: 0, messageId: mId, treeId: 0, sessionId: sessionId)
|
||||
let msg = SMB2.LogOff()
|
||||
let data = createSMB2Message(header: smbHeader, message: msg)
|
||||
self.write(data, timeout: 0, completionHandler: { (e) in
|
||||
completionHandler?(e)
|
||||
})
|
||||
return mId
|
||||
}
|
||||
|
||||
func reset() {
|
||||
fileprivate func open(secure: Bool = false) {
|
||||
var readStream : Unmanaged<CFReadStream>?
|
||||
var writeStream : Unmanaged<CFWriteStream>?
|
||||
|
||||
if inputStream == nil || outputStream == nil {
|
||||
if let host = host {
|
||||
CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, host.hostname as CFString, UInt32(host.port), &readStream, &writeStream)
|
||||
} else if let service = service {
|
||||
let cfnetService = CFNetServiceCreate(kCFAllocatorDefault, service.domain as CFString, service.type as CFString, service.name as CFString, Int32(service.port))
|
||||
CFStreamCreatePairWithSocketToNetService(kCFAllocatorDefault, cfnetService.takeRetainedValue(), &readStream, &writeStream)
|
||||
}
|
||||
|
||||
inputStream = readStream?.takeRetainedValue()
|
||||
outputStream = writeStream?.takeRetainedValue()
|
||||
}
|
||||
|
||||
guard let inputStream = inputStream, let outputStream = outputStream else {
|
||||
return
|
||||
}
|
||||
|
||||
if secure {
|
||||
inputStream.setProperty(StreamSocketSecurityLevel.negotiatedSSL.rawValue, forKey: .socketSecurityLevelKey)
|
||||
outputStream.setProperty(StreamSocketSecurityLevel.negotiatedSSL.rawValue, forKey: .socketSecurityLevelKey)
|
||||
}
|
||||
|
||||
inputStream.delegate = self
|
||||
outputStream.delegate = self
|
||||
inputStream.schedule(in: RunLoop.main, forMode: .defaultRunLoopMode)
|
||||
outputStream.schedule(in: RunLoop.main, forMode: .defaultRunLoopMode)
|
||||
inputStream.open()
|
||||
outputStream.open()
|
||||
|
||||
operation_queue.isSuspended = false
|
||||
}
|
||||
|
||||
fileprivate func close() {
|
||||
self.inputStream?.close()
|
||||
self.outputStream?.close()
|
||||
self.inputStream?.remove(from: RunLoop.main, forMode: .defaultRunLoopMode)
|
||||
self.outputStream?.remove(from: RunLoop.main, forMode: .defaultRunLoopMode)
|
||||
self.inputStream?.delegate = nil
|
||||
self.outputStream?.delegate = nil
|
||||
|
||||
self.inputStream = nil
|
||||
self.outputStream = nil
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
fileprivate func write(data: Data) throws -> Int {
|
||||
guard let outputStream = self.outputStream else {
|
||||
throw SMBClientError.streamNotOpened
|
||||
}
|
||||
let expireDate = Date(timeIntervalSinceNow: timeout)
|
||||
var data = data
|
||||
var byteSent: Int = 0
|
||||
while data.count > 0 {
|
||||
let bytesWritten = data.withUnsafeBytes {
|
||||
outputStream.write($0, maxLength: data.count)
|
||||
}
|
||||
|
||||
if bytesWritten > 0 {
|
||||
let range = 0..<bytesWritten
|
||||
data.replaceSubrange(range, with: Data())
|
||||
byteSent += bytesWritten
|
||||
} else if bytesWritten < 0 {
|
||||
if let error = outputStream.streamError {
|
||||
throw error
|
||||
}
|
||||
return bytesWritten
|
||||
}
|
||||
if data.count == 0 {
|
||||
break
|
||||
}
|
||||
if expireDate < Date() {
|
||||
throw SMBClientError.timedOut
|
||||
}
|
||||
|
||||
}
|
||||
return byteSent
|
||||
}
|
||||
|
||||
var currentHandlingData: Data = Data()
|
||||
var expectedBytes = 0
|
||||
|
||||
open func stream(_ aStream: Stream, handle eventCode: Stream.Event) {
|
||||
if eventCode.contains(.errorOccurred) {
|
||||
/*self._error = aStream.streamError
|
||||
streamDelegate?.urlSession?(_underlyingSession, task: self, didCompleteWithError: error)*/
|
||||
}
|
||||
|
||||
if aStream == inputStream && eventCode.contains(.hasBytesAvailable) {
|
||||
while (inputStream!.hasBytesAvailable) {
|
||||
var buffer = [UInt8](repeating: 0, count: 65536)
|
||||
let len = inputStream!.read(&buffer, maxLength: buffer.count)
|
||||
if len > 0 {
|
||||
|
||||
/*dataReceived.append(&buffer, count: len)
|
||||
self._countOfBytesRecieved += Int64(len)*/
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: create and analyse messages
|
||||
extension SMB2ProtocolClient {
|
||||
extension SMBClient {
|
||||
internal func sendMessage(_ message: SMBRequestBody, toTree treeId: UInt32, completionHandler: SimpleCompletionHandler) -> UInt64 {
|
||||
let mId = createMessageId()
|
||||
let credit = consumeCredit()
|
||||
let smbHeader = SMB2.Header(command: message.command, creditRequestResponse: credit, messageId: mId, treeId: treeId, sessionId: sessionId)
|
||||
let data = createRequest(header: smbHeader, message: message)
|
||||
operation_queue.addOperation {
|
||||
do {
|
||||
try self.write(data: data)
|
||||
completionHandler?(nil)
|
||||
} catch {
|
||||
completionHandler?(error)
|
||||
}
|
||||
}
|
||||
return mId
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fileprivate extension SMBClient {
|
||||
func determineSMBVersion(_ data: Data) -> Float {
|
||||
let smbverChar: Int8 = Int8(bitPattern: data.first ?? 0)
|
||||
let version = 0 - smbverChar
|
||||
return Float(version)
|
||||
}
|
||||
|
||||
func digestSMBMessage(_ data: Data) throws -> (header: SMB1.Header, blocks: [(params: [UInt16], message: Data?)]) {
|
||||
guard data.count > 30 else {
|
||||
throw URLError(.badServerResponse)
|
||||
}
|
||||
var buffer = [UInt8](repeating: 0, count: data.count)
|
||||
guard determineSMBVersion(data) == 1 else {
|
||||
throw SMBFileProviderError.incompatibleHeader
|
||||
}
|
||||
let headersize = MemoryLayout<SMB1.Header>.size
|
||||
let header: SMB1.Header = data.scanValue()!
|
||||
var blocks = [(params: [UInt16], message: Data?)]()
|
||||
var offset = headersize
|
||||
while offset < data.count {
|
||||
let paramWords: [UInt16]
|
||||
let paramWordsCount = Int(buffer[offset])
|
||||
guard data.count > (paramWordsCount * 2 + offset) else {
|
||||
throw SMBFileProviderError.incorrectParamsLength
|
||||
}
|
||||
offset += MemoryLayout<UInt8>.size
|
||||
var rawParamWords = [UInt8](buffer[offset..<(offset + paramWordsCount * 2)])
|
||||
let paramData = Data(bytesNoCopy: UnsafeMutablePointer<UInt8>(&rawParamWords), count: rawParamWords.count, deallocator: .free)
|
||||
paramWords = paramData.scanValue()!
|
||||
offset += paramWordsCount * 2
|
||||
let messageBytesCount = Int(UInt16(buffer[0]) + UInt16(buffer[1]) << 8)
|
||||
offset += MemoryLayout<UInt16>.size
|
||||
guard data.count >= (offset + messageBytesCount) else {
|
||||
throw SMBFileProviderError.incorrectMessageLength
|
||||
}
|
||||
let rawMessage = [UInt8](buffer[offset..<(offset + messageBytesCount)])
|
||||
offset += messageBytesCount
|
||||
let message = Data(bytes: rawMessage)
|
||||
blocks.append((params: paramWords, message: message))
|
||||
}
|
||||
return (header, blocks)
|
||||
func createRequest(header: SMB2.Header, message: SMBRequestBody) -> Data {
|
||||
var result = Data(value: header)
|
||||
result.append(message.data())
|
||||
return result
|
||||
}
|
||||
|
||||
func digestSMB2Message(_ data: Data) throws -> SMBResponse? {
|
||||
func responseOf(_ data: Data) throws -> SMBResponse? {
|
||||
guard data.count > 65 else {
|
||||
throw URLError(.badServerResponse)
|
||||
}
|
||||
guard determineSMBVersion(data) == 2 else {
|
||||
guard determineSMBVersion(data) >= 2 else {
|
||||
throw SMBFileProviderError.incompatibleHeader
|
||||
}
|
||||
let headersize = MemoryLayout<SMB2.Header>.size
|
||||
@@ -202,12 +251,12 @@ extension SMB2ProtocolClient {
|
||||
return (header, SMB2.SetInfoResponse(data: messageData))
|
||||
case .OPLOCK_BREAK:
|
||||
return (header, nil) // FIXME:
|
||||
case .INVALID:
|
||||
default:
|
||||
throw SMBFileProviderError.invalidCommand
|
||||
}
|
||||
}
|
||||
|
||||
func createSMBMessage(header: SMB1.Header, blocks: [(params: Data?, message: Data?)]) -> Data {
|
||||
/*func createSMBMessage(header: SMB1.Header, blocks: [(params: Data?, message: Data?)]) -> Data {
|
||||
var result = Data(value: header)
|
||||
for block in blocks {
|
||||
var paramWordsCount = UInt8(block.params?.count ?? 0)
|
||||
@@ -223,11 +272,42 @@ extension SMB2ProtocolClient {
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
}*/
|
||||
|
||||
func createSMB2Message(header: SMB2.Header, message: SMBRequestBody) -> Data {
|
||||
var result = Data(value: header)
|
||||
result.append(message.data())
|
||||
return result
|
||||
}
|
||||
/*func digestSMBMessage(_ data: Data) throws -> (header: SMB1.Header, blocks: [(params: [UInt16], message: Data?)]) {
|
||||
guard data.count > 30 else {
|
||||
throw URLError(.badServerResponse)
|
||||
}
|
||||
var buffer = [UInt8](repeating: 0, count: data.count)
|
||||
guard determineSMBVersion(data) == 1 else {
|
||||
throw SMBFileProviderError.incompatibleHeader
|
||||
}
|
||||
let headersize = MemoryLayout<SMB1.Header>.size
|
||||
let header: SMB1.Header = data.scanValue()!
|
||||
var blocks = [(params: [UInt16], message: Data?)]()
|
||||
var offset = headersize
|
||||
while offset < data.count {
|
||||
let paramWords: [UInt16]
|
||||
let paramWordsCount = Int(buffer[offset])
|
||||
guard data.count > (paramWordsCount * 2 + offset) else {
|
||||
throw SMBFileProviderError.incorrectParamsLength
|
||||
}
|
||||
offset += MemoryLayout<UInt8>.size
|
||||
var rawParamWords = [UInt8](buffer[offset..<(offset + paramWordsCount * 2)])
|
||||
let paramData = Data(bytesNoCopy: UnsafeMutablePointer<UInt8>(&rawParamWords), count: rawParamWords.count, deallocator: .free)
|
||||
paramWords = paramData.scanValue()!
|
||||
offset += paramWordsCount * 2
|
||||
let messageBytesCountHi = Int(buffer[1]) << 8
|
||||
let messageBytesCount = Int(buffer[0]) + messageBytesCountHi
|
||||
offset += MemoryLayout<UInt16>.size
|
||||
guard data.count >= (offset + messageBytesCount) else {
|
||||
throw SMBFileProviderError.incorrectMessageLength
|
||||
}
|
||||
let rawMessage = [UInt8](buffer[offset..<(offset + messageBytesCount)])
|
||||
offset += messageBytesCount
|
||||
let message = Data(bytes: rawMessage)
|
||||
blocks.append((params: paramWords, message: message))
|
||||
}
|
||||
return (header, blocks)
|
||||
}*/
|
||||
}
|
||||
|
||||
@@ -24,9 +24,15 @@ class SMBFileProvider: FileProvider, FileProviderMonitor {
|
||||
return nil
|
||||
}
|
||||
self.baseURL = baseURL.appendingPathComponent("")
|
||||
dispatch_queue = DispatchQueue(label: "FileProvider.\(type(of: self).type)", attributes: .concurrent)
|
||||
|
||||
#if swift(>=3.1)
|
||||
let queueLabel = "FileProvider.\(Swift.type(of: self).type)"
|
||||
#else
|
||||
let queueLabel = "FileProvider.\(type(of: self).type)"
|
||||
#endif
|
||||
dispatch_queue = DispatchQueue(label: queueLabel, attributes: .concurrent)
|
||||
operation_queue = OperationQueue()
|
||||
operation_queue.name = "FileProvider.\(type(of: self).type).Operation"
|
||||
operation_queue.name = "\(queueLabel).Operation"
|
||||
|
||||
self.credential = credential
|
||||
}
|
||||
@@ -50,71 +56,72 @@ class SMBFileProvider: FileProvider, FileProviderMonitor {
|
||||
return true
|
||||
}
|
||||
|
||||
open func contentsOfDirectory(path: String, completionHandler: @escaping ((_ contents: [FileObjectClass], _ error: Error?) -> Void)) {
|
||||
open func contentsOfDirectory(path: String, completionHandler: @escaping (_ contents: [FileObjectClass], _ error: Error?) -> Void) {
|
||||
NotImplemented()
|
||||
}
|
||||
|
||||
open func attributesOfItem(path: String, completionHandler: @escaping ((_ attributes: FileObjectClass?, _ error: Error?) -> Void)) {
|
||||
open func attributesOfItem(path: String, completionHandler: @escaping (_ attributes: FileObjectClass?, _ error: Error?) -> Void) {
|
||||
NotImplemented()
|
||||
}
|
||||
|
||||
open func storageProperties(completionHandler: @escaping ((_ total: Int64, _ used: Int64) -> Void)) {
|
||||
open func storageProperties(completionHandler: @escaping (_ volume: VolumeObject?) -> Void) {
|
||||
NotImplemented()
|
||||
}
|
||||
|
||||
func isReachable(completionHandler: @escaping (Bool) -> Void) {
|
||||
func isReachable(completionHandler: @escaping (_ success: Bool, _ error: Error?) -> Void) {
|
||||
NotImplemented()
|
||||
}
|
||||
|
||||
open weak var fileOperationDelegate: FileOperationDelegate?
|
||||
|
||||
open func create(folder folderName: String, at atPath: String, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
open func create(folder folderName: String, at atPath: String, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
NotImplemented()
|
||||
return nil
|
||||
}
|
||||
|
||||
open func moveItem(path: String, to toPath: String, overwrite: Bool = false, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
open func moveItem(path: String, to toPath: String, overwrite: Bool = false, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
NotImplemented()
|
||||
return nil
|
||||
}
|
||||
|
||||
open func copyItem(path: String, to toPath: String, overwrite: Bool = false, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
open func copyItem(path: String, to toPath: String, overwrite: Bool = false, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
NotImplemented()
|
||||
return nil
|
||||
}
|
||||
|
||||
open func removeItem(path: String, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
open func removeItem(path: String, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
NotImplemented()
|
||||
return nil
|
||||
}
|
||||
|
||||
open func copyItem(localFile: URL, to toPath: String, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
open func copyItem(localFile: URL, to toPath: String, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
NotImplemented()
|
||||
return nil
|
||||
}
|
||||
|
||||
open func copyItem(path: String, toLocalURL: URL, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
open func copyItem(path: String, toLocalURL: URL, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
NotImplemented()
|
||||
return nil
|
||||
}
|
||||
|
||||
open func contents(path: String, completionHandler: @escaping ((_ contents: Data?, _ error: Error?) -> Void)) -> OperationHandle? {
|
||||
open func contents(path: String, completionHandler: @escaping ((_ contents: Data?, _ error: Error?) -> Void)) -> Progress? {
|
||||
NotImplemented()
|
||||
return nil
|
||||
}
|
||||
|
||||
open func contents(path: String, offset: Int64, length: Int, completionHandler: @escaping ((_ contents: Data?, _ error: Error?) -> Void)) -> OperationHandle? {
|
||||
open func contents(path: String, offset: Int64, length: Int, completionHandler: @escaping ((_ contents: Data?, _ error: Error?) -> Void)) -> Progress? {
|
||||
NotImplemented()
|
||||
return nil
|
||||
}
|
||||
|
||||
open func writeContents(path: String, contents data: Data?, atomically: Bool, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
open func writeContents(path: String, contents data: Data?, atomically: Bool, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> Progress? {
|
||||
NotImplemented()
|
||||
return nil
|
||||
}
|
||||
|
||||
open func searchFiles(path: String, recursive: Bool, query: NSPredicate, foundItemHandler:((FileObjectClass) -> Void)?, completionHandler: @escaping ((_ files: [FileObjectClass], _ error: Error?) -> Void)) {
|
||||
open func searchFiles(path: String, recursive: Bool, query: NSPredicate, foundItemHandler:((FileObjectClass) -> Void)?, completionHandler: @escaping ((_ files: [FileObjectClass], _ error: Error?) -> Void)) -> Progress? {
|
||||
NotImplemented()
|
||||
return nil
|
||||
}
|
||||
|
||||
open func registerNotifcation(path: String, eventHandler: @escaping (() -> Void)) {
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
// codebeat:disable[TOO_MANY_IVARS]
|
||||
// SMB/CIFS Types
|
||||
struct SMB1 {
|
||||
struct Header { // 32 bytes
|
||||
@@ -75,6 +76,7 @@ struct SMB1 {
|
||||
}
|
||||
}
|
||||
|
||||
// codebeat:disable[ARITY]
|
||||
init(command: Command, treeId: UInt16, pid: UInt32, userId: UInt16, multiplexId: UInt16, flags: Flags, flags2: Flags2 = [.LONG_NAMES, .ERR_STATUS, .UNICODE], ntStatus: UInt32 = 0, securityKey: UInt32 = 0, securityCID: UInt16 = 0, securitySequenceNumber: UInt16 = 0) {
|
||||
self.protocolID = Header.protocolConst
|
||||
self._command = command.rawValue
|
||||
@@ -91,6 +93,7 @@ struct SMB1 {
|
||||
self.userId = userId
|
||||
self.multiplexId = multiplexId
|
||||
}
|
||||
// codebeat:enable[ARITY]
|
||||
}
|
||||
|
||||
struct Flags: OptionSet {
|
||||
@@ -215,3 +218,4 @@ struct SMB1 {
|
||||
case INVALID = 0xFE
|
||||
}
|
||||
}
|
||||
// codebeat:enable[TOO_MANY_IVARS]
|
||||
|
||||
@@ -8,11 +8,36 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
protocol Option: RawRepresentable, Hashable {
|
||||
|
||||
}
|
||||
|
||||
extension Option where RawValue: Hashable {
|
||||
var hashValue: Int {
|
||||
return rawValue.hashValue
|
||||
}
|
||||
}
|
||||
|
||||
extension Option where RawValue: Equatable {
|
||||
static func ==(lhs: Self, rhs: Self) -> Bool {
|
||||
return lhs.rawValue == rhs.rawValue
|
||||
}
|
||||
}
|
||||
|
||||
protocol SMBRequestBody {
|
||||
static var command: SMB2.Command { get }
|
||||
func data() -> Data
|
||||
}
|
||||
|
||||
extension SMBRequestBody {
|
||||
var command: SMB2.Command {
|
||||
#if swift(>=3.1)
|
||||
return Swift.type(of: self).command
|
||||
#else
|
||||
return type(of: self).command
|
||||
#endif
|
||||
}
|
||||
|
||||
func data() -> Data {
|
||||
return Data(value: self)
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ extension SMB2 {
|
||||
// MARK: SMB2 Create
|
||||
|
||||
struct CreateRequest: SMBRequestBody {
|
||||
static var command: SMB2.Command = .CREATE
|
||||
|
||||
let header: CreateRequest.Header
|
||||
let name: String?
|
||||
let contexts: [CreateContext]
|
||||
@@ -48,38 +50,14 @@ extension SMB2 {
|
||||
struct Header {
|
||||
let size: UInt16
|
||||
fileprivate let securityFlags: UInt8
|
||||
fileprivate var _requestedOplockLevel: UInt8
|
||||
var requestedOplockLevel: OplockLevel {
|
||||
get {
|
||||
return OplockLevel(rawValue: _requestedOplockLevel)!
|
||||
}
|
||||
set {
|
||||
_requestedOplockLevel = newValue.rawValue
|
||||
}
|
||||
}
|
||||
fileprivate var _impersonationLevel: UInt32
|
||||
var impersonationLevel: ImpersonationLevel {
|
||||
get {
|
||||
return ImpersonationLevel(rawValue: _impersonationLevel)!
|
||||
}
|
||||
set {
|
||||
_impersonationLevel = newValue.rawValue
|
||||
}
|
||||
}
|
||||
var requestedOplockLevel: OplockLevel
|
||||
var impersonationLevel: ImpersonationLevel
|
||||
fileprivate let flags: UInt64
|
||||
fileprivate let reserved: UInt64
|
||||
let access: FileAccessMask
|
||||
let fileAttributes: FileAttributes
|
||||
let shareAccess: ShareAccess
|
||||
fileprivate var _desposition: UInt32
|
||||
var desposition: CreateDisposition {
|
||||
get {
|
||||
return CreateDisposition(rawValue: _desposition)!
|
||||
}
|
||||
set {
|
||||
_desposition = newValue.rawValue
|
||||
}
|
||||
}
|
||||
var desposition: CreateDisposition
|
||||
let options: CreateOptions
|
||||
var nameOffset: UInt16
|
||||
var nameLength: UInt16
|
||||
@@ -89,14 +67,14 @@ extension SMB2 {
|
||||
init(requestedOplockLevel: OplockLevel = .NONE, impersonationLevel: ImpersonationLevel = .anonymous, access: FileAccessMask = [.GENERIC_ALL], fileAttributes: FileAttributes = [], shareAccess: ShareAccess = [.READ], desposition: CreateDisposition = .OPEN_IF, options: CreateOptions = []) {
|
||||
self.size = 57
|
||||
self.securityFlags = 0
|
||||
self._requestedOplockLevel = requestedOplockLevel.rawValue
|
||||
self._impersonationLevel = impersonationLevel.rawValue
|
||||
self.requestedOplockLevel = requestedOplockLevel
|
||||
self.impersonationLevel = impersonationLevel
|
||||
self.flags = 0
|
||||
self.reserved = 0
|
||||
self.access = access
|
||||
self.fileAttributes = fileAttributes
|
||||
self.shareAccess = shareAccess
|
||||
self._desposition = desposition.rawValue
|
||||
self.desposition = desposition
|
||||
self.options = options
|
||||
self.nameOffset = 0
|
||||
self.nameLength = 0
|
||||
@@ -135,36 +113,44 @@ extension SMB2 {
|
||||
fileprivate static let RESERVE_OPFILTER = CreateOptions(rawValue: 0x00100000)
|
||||
}
|
||||
|
||||
enum CreateDisposition: UInt32 {
|
||||
struct CreateDisposition: Option {
|
||||
init(rawValue: UInt32) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
var rawValue: UInt32
|
||||
/// If the file already exists, supersede it. Otherwise, create the file.
|
||||
case SUPERSEDE = 0x00000000
|
||||
public static let SUPERSEDE = CreateDisposition(rawValue: 0x00000000)
|
||||
/// If the file already exists, return success; otherwise, fail the operation.
|
||||
case OPEN = 0x00000001
|
||||
public static let OPEN = CreateDisposition(rawValue: 0x00000001)
|
||||
/// If the file already exists, fail the operation; otherwise, create the file.
|
||||
case CREATE = 0x00000002
|
||||
public static let CREATE = CreateDisposition(rawValue: 0x00000002)
|
||||
/// Open the file if it already exists; otherwise, create the file.
|
||||
case OPEN_IF = 0x00000003
|
||||
public static let OPEN_IF = CreateDisposition(rawValue: 0x00000003)
|
||||
/// Overwrite the file if it already exists; otherwise, fail the operation.
|
||||
case OVERWRITE = 0x00000004
|
||||
public static let OVERWRITE = CreateDisposition(rawValue: 0x00000004)
|
||||
/// Overwrite the file if it already exists; otherwise, create the file.
|
||||
case OVERWRITE_IF = 0x00000005
|
||||
public static let OVERWRITE_IF = CreateDisposition(rawValue: 0x00000005)
|
||||
}
|
||||
|
||||
enum ImpersonationLevel: UInt32 {
|
||||
case anonymous = 0x00000000
|
||||
case identification = 0x00000001
|
||||
case impersonation = 0x00000002
|
||||
case delegate = 0x00000003
|
||||
struct ImpersonationLevel {
|
||||
init(rawValue: UInt32) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
var rawValue: UInt32
|
||||
|
||||
public static let anonymous = ImpersonationLevel(rawValue: 0x00000000)
|
||||
public static let identification = ImpersonationLevel(rawValue: 0x00000001)
|
||||
public static let impersonation = ImpersonationLevel(rawValue: 0x00000002)
|
||||
public static let delegate = ImpersonationLevel(rawValue: 0x00000003)
|
||||
}
|
||||
}
|
||||
|
||||
struct CreateResponse: SMBResponseBody {
|
||||
struct Header {
|
||||
let size: UInt16
|
||||
fileprivate let _oplockLevel: UInt8
|
||||
var oplockLevel: OplockLevel {
|
||||
return OplockLevel(rawValue: _oplockLevel)!
|
||||
}
|
||||
let oplockLevel: OplockLevel
|
||||
fileprivate let reserved: UInt32
|
||||
let creationTime: SMBTime
|
||||
let lastAccessTime: SMBTime
|
||||
@@ -248,34 +234,47 @@ extension SMB2 {
|
||||
return result
|
||||
}
|
||||
|
||||
enum ContextNames: String {
|
||||
struct ContextNames: Option {
|
||||
init(rawValue: String) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
let rawValue: String
|
||||
|
||||
/// Request Create Context: Extended attributes
|
||||
case EA_BUFFER = "ExtA"
|
||||
public static let EA_BUFFER = ContextNames(rawValue: "ExtA")
|
||||
/// Request Create Context: Security descriptor
|
||||
case SD_BUFFER = "SecD"
|
||||
public static let SD_BUFFER = ContextNames(rawValue: "SecD")
|
||||
/// Request & Response Create Context: Open to be durable
|
||||
case DURABLE_HANDLE = "DHnQ"
|
||||
case DURABLE_HANDLE_RESPONSE_V2 = "DH2Q"
|
||||
public static let DURABLE_HANDLE = ContextNames(rawValue: "DHnQ")
|
||||
/// Request & Response Create Context: Open to be durable
|
||||
public static let DURABLE_HANDLE_RESPONSE_V2 = ContextNames(rawValue: "DH2Q")
|
||||
/// Request Create Context: Reconnect to a durable open after being disconnected
|
||||
case DURABLE_HANDLE_RECONNECT = "DHnC"
|
||||
public static let DURABLE_HANDLE_RECONNECT = ContextNames(rawValue: "DHnC")
|
||||
/// Request Create Context: Required allocation size of the newly created file
|
||||
case ALLOCATION_SIZE = "AISi"
|
||||
public static let ALLOCATION_SIZE = ContextNames(rawValue: "AISi")
|
||||
/// Request & Response Create Context: Maximal access information
|
||||
case QUERY_MAXIMAL_ACCESS = "MxAc"
|
||||
case TIMEWARP_TOKEN = "TWrp"
|
||||
public static let QUERY_MAXIMAL_ACCESS = ContextNames(rawValue: "MxAc")
|
||||
public static let TIMEWARP_TOKEN = ContextNames(rawValue: "TWrp")
|
||||
/// Response Create Context: DiskID of the open file in a volume.
|
||||
case QUERY_ON_DISK_ID = "QFid"
|
||||
public static let QUERY_ON_DISK_ID = ContextNames(rawValue: "QFid")
|
||||
/// Response Create Context: A lease. This value is only supported for the SMB 2.1 and 3.x dialect family.
|
||||
case LEASE = "RqLs"
|
||||
public static let LEASE = ContextNames(rawValue: "RqLs")
|
||||
}
|
||||
}
|
||||
|
||||
enum OplockLevel: UInt8 {
|
||||
case NONE = 0x00
|
||||
case LEVEL_II = 0x01
|
||||
case EXCLUSIVE = 0x08
|
||||
case BATCH = 0x09
|
||||
case LEASE = 0xFF
|
||||
struct OplockLevel {
|
||||
let rawValue: UInt8
|
||||
|
||||
init(rawValue: UInt8) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
public static let NONE = OplockLevel(rawValue: 0x00)
|
||||
public static let LEVEL_II = OplockLevel(rawValue: 0x01)
|
||||
public static let EXCLUSIVE = OplockLevel(rawValue: 0x08)
|
||||
public static let BATCH = OplockLevel(rawValue: 0x09)
|
||||
public static let LEASE = OplockLevel(rawValue: 0xFF)
|
||||
}
|
||||
|
||||
struct ShareAccess: OptionSet {
|
||||
@@ -358,6 +357,8 @@ extension SMB2 {
|
||||
// MARK: SMB2 Close
|
||||
|
||||
struct CloseRequest: SMBRequestBody {
|
||||
static var command: SMB2.Command = .CLOSE
|
||||
|
||||
let size: UInt16
|
||||
let flags: CloseFlags
|
||||
fileprivate let reserved2: UInt32
|
||||
@@ -399,6 +400,8 @@ extension SMB2 {
|
||||
// MARK: SMB2 Flush
|
||||
|
||||
struct FlushRequest: SMBRequestBody {
|
||||
static var command: SMB2.Command = .FLUSH
|
||||
|
||||
let size: UInt16
|
||||
fileprivate let reserved: UInt16
|
||||
fileprivate let reserved2: UInt32
|
||||
|
||||
@@ -12,6 +12,8 @@ extension SMB2 {
|
||||
// MARK: SMB2 Read
|
||||
|
||||
struct ReadRequest: SMBRequestBody {
|
||||
static var command: SMB2.Command = .READ
|
||||
|
||||
let size: UInt16
|
||||
fileprivate let padding: UInt8
|
||||
let flags: ReadRequest.Flags
|
||||
@@ -19,10 +21,7 @@ extension SMB2 {
|
||||
let offset: UInt64
|
||||
let fileId: FileId
|
||||
let minimumLength: UInt32
|
||||
fileprivate let _channel: UInt32
|
||||
var channel: Channel {
|
||||
return Channel(rawValue: _channel) ?? .NONE
|
||||
}
|
||||
let channel: Channel
|
||||
let remainingBytes: UInt32
|
||||
fileprivate let channelInfoOffset: UInt16
|
||||
fileprivate let channelInfoLength: UInt16
|
||||
@@ -36,7 +35,7 @@ extension SMB2 {
|
||||
self.offset = offset
|
||||
self.fileId = fileId
|
||||
self.minimumLength = minimumLength
|
||||
self._channel = channel.rawValue
|
||||
self.channel = channel
|
||||
self.remainingBytes = remainingBytes
|
||||
self.channelInfoOffset = 0
|
||||
self.channelInfoLength = 0
|
||||
@@ -77,15 +76,23 @@ extension SMB2 {
|
||||
}
|
||||
}
|
||||
|
||||
enum Channel: UInt32 {
|
||||
case NONE = 0x00000000
|
||||
case RDMA_V1 = 0x00000001
|
||||
case RDMA_V1_INVALIDATE = 0x00000002
|
||||
struct Channel: Option {
|
||||
init(rawValue: UInt32) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
let rawValue: UInt32
|
||||
|
||||
public static let NONE = Channel(rawValue: 0x00000000)
|
||||
public static let RDMA_V1 = Channel(rawValue: 0x00000001)
|
||||
public static let RDMA_V1_INVALIDATE = Channel(rawValue: 0x00000002)
|
||||
}
|
||||
|
||||
// MARK: SMB2 Write
|
||||
|
||||
struct WriteRequest: SMBRequestBody {
|
||||
static var command: SMB2.Command = .WRITE
|
||||
|
||||
let header: WriteRequest.Header
|
||||
let channelInfo: ChannelInfo?
|
||||
let fileData: Data
|
||||
@@ -96,10 +103,7 @@ extension SMB2 {
|
||||
let length: UInt32
|
||||
let offset: UInt64
|
||||
let fileId: FileId
|
||||
fileprivate let _channel: UInt32
|
||||
var channel: Channel {
|
||||
return Channel(rawValue: _channel) ?? .NONE
|
||||
}
|
||||
let channel: Channel
|
||||
let remainingBytes: UInt32
|
||||
let channelInfoOffset: UInt16
|
||||
let channelInfoLength: UInt16
|
||||
@@ -115,7 +119,7 @@ extension SMB2 {
|
||||
channelInfoLength = UInt16(MemoryLayout<SMB2.ChannelInfo>.size)
|
||||
}
|
||||
let dataOffset = UInt16(MemoryLayout<SMB2.Header>.size + MemoryLayout<WriteRequest.Header>.size) + channelInfoLength
|
||||
self.header = WriteRequest.Header(size: UInt16(49), dataOffset: dataOffset, length: UInt32(data.count), offset: offset, fileId: fileId, _channel: channel.rawValue, remainingBytes: remainingBytes, channelInfoOffset: channelInfoOffset, channelInfoLength: channelInfoLength, flags: flags)
|
||||
self.header = WriteRequest.Header(size: UInt16(49), dataOffset: dataOffset, length: UInt32(data.count), offset: offset, fileId: fileId, channel: channel, remainingBytes: remainingBytes, channelInfoOffset: channelInfoOffset, channelInfoLength: channelInfoLength, flags: flags)
|
||||
self.channelInfo = channelInfo
|
||||
self.fileData = data
|
||||
}
|
||||
@@ -152,6 +156,8 @@ extension SMB2 {
|
||||
}
|
||||
|
||||
struct ChannelInfo: SMBRequestBody {
|
||||
static var command: SMB2.Command = .WRITE
|
||||
|
||||
let offset: UInt64
|
||||
let token: UInt32
|
||||
let length: UInt32
|
||||
@@ -160,6 +166,8 @@ extension SMB2 {
|
||||
// MARK: SMB2 Lock
|
||||
|
||||
struct LockElement: SMBRequestBody {
|
||||
static var command: SMB2.Command = .LOCK
|
||||
|
||||
let offset: UInt64
|
||||
let length: UInt64
|
||||
let flags: LockElement.Flags
|
||||
@@ -180,6 +188,8 @@ extension SMB2 {
|
||||
}
|
||||
|
||||
struct LockRequest: SMBRequestBody {
|
||||
static var command: SMB2.Command = .LOCK
|
||||
|
||||
let header: LockRequest.Header
|
||||
let locks: [LockElement]
|
||||
|
||||
@@ -217,6 +227,8 @@ extension SMB2 {
|
||||
// MARK: SMB2 Cancel
|
||||
|
||||
struct CancelRequest: SMBRequestBody {
|
||||
static var command: SMB2.Command = .CANCEL
|
||||
|
||||
let size: UInt16
|
||||
let reserved: UInt16
|
||||
|
||||
|
||||
@@ -16,12 +16,14 @@ extension SMB2 {
|
||||
*/
|
||||
|
||||
struct IOCtlRequest: SMBRequestBody {
|
||||
static var command: SMB2.Command = .IOCTL
|
||||
|
||||
let header: Header
|
||||
let requestData: IOCtlRequestProtocol?
|
||||
|
||||
init(fileId: FileId ,ctlCode: IOCtlCode, requestData: IOCtlRequestProtocol?, flags: IOCtlRequest.Flags = []) {
|
||||
let offset = requestData != nil ? UInt32(MemoryLayout<SMB2.Header>.size + MemoryLayout<IOCtlRequest.Header>.size) : 0
|
||||
self.header = Header(size: 57, reserved: 0, _ctlCode: ctlCode.rawValue, fileId: fileId, inputOffset: offset, inputCount: UInt32((requestData?.data().count ?? 0)), maxInputResponse: 0, outputOffset: offset, outputCount: 0, maxOutputResponse: UInt32(Int32.max), flags: flags, reserved2: 0)
|
||||
self.header = Header(size: 57, reserved: 0, ctlCode: ctlCode, fileId: fileId, inputOffset: offset, inputCount: UInt32((requestData?.data().count ?? 0)), maxInputResponse: 0, outputOffset: offset, outputCount: 0, maxOutputResponse: UInt32(Int32.max), flags: flags, reserved2: 0)
|
||||
self.requestData = requestData
|
||||
}
|
||||
|
||||
@@ -36,10 +38,7 @@ extension SMB2 {
|
||||
struct Header {
|
||||
let size: UInt16
|
||||
fileprivate let reserved: UInt16
|
||||
fileprivate let _ctlCode: UInt32
|
||||
var ctlCode: IOCtlCode {
|
||||
return IOCtlCode(rawValue: _ctlCode)!
|
||||
}
|
||||
let ctlCode: IOCtlCode
|
||||
let fileId: FileId
|
||||
let inputOffset: UInt32
|
||||
let inputCount: UInt32
|
||||
@@ -92,10 +91,7 @@ extension SMB2 {
|
||||
struct Header {
|
||||
let size: UInt16
|
||||
fileprivate let reserved: UInt16
|
||||
fileprivate let _ctlCode: UInt32
|
||||
var ctlCode: IOCtlCode {
|
||||
return IOCtlCode(rawValue: _ctlCode)!
|
||||
}
|
||||
let ctlCode: IOCtlCode
|
||||
let fileId: FileId
|
||||
let inputOffset: UInt32
|
||||
let inputCount: UInt32
|
||||
@@ -106,35 +102,43 @@ extension SMB2 {
|
||||
}
|
||||
}
|
||||
|
||||
enum IOCtlCode: UInt32 {
|
||||
case DFS_GET_REFERRALS = 0x00060194
|
||||
case DFS_GET_REFERRALS_EX = 0x000601B0
|
||||
case SET_REPARSE_POINT = 0x000900A4
|
||||
case FILE_LEVEL_TRIM = 0x00098208
|
||||
case PIPE_PEEK = 0x0011400C
|
||||
case PIPE_WAIT = 0x00110018
|
||||
struct IOCtlCode: Option {
|
||||
let rawValue: UInt32
|
||||
|
||||
init(rawValue: UInt32) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
public static let DFS_GET_REFERRALS = IOCtlCode(rawValue: 0x00060194)
|
||||
public static let DFS_GET_REFERRALS_EX = IOCtlCode(rawValue: 0x000601B0)
|
||||
public static let SET_REPARSE_POINT = IOCtlCode(rawValue: 0x000900A4)
|
||||
public static let FILE_LEVEL_TRIM = IOCtlCode(rawValue: 0x00098208)
|
||||
public static let PIPE_PEEK = IOCtlCode(rawValue: 0x0011400C)
|
||||
public static let PIPE_WAIT = IOCtlCode(rawValue: 0x00110018)
|
||||
/// PIPE_TRANSCEIVE is valid only on a named pipe with mode set to FILE_PIPE_MESSAGE_MODE.
|
||||
case PIPE_TRANSCEIVE = 0x0011C017
|
||||
public static let PIPE_TRANSCEIVE = IOCtlCode(rawValue: 0x0011C017)
|
||||
/// Get ResumeKey used by the client to uniquely identify the source file in an FSCTL_SRV_COPYCHUNK or FSCTL_SRV_COPYCHUNK_WRITE request.
|
||||
case SRV_REQUEST_RESUME_KEY = 0x00140078
|
||||
public static let SRV_REQUEST_RESUME_KEY = IOCtlCode(rawValue: 0x00140078)
|
||||
/// Get all the revision time-stamps that are associated with the Tree Connect share in which the open resides
|
||||
case SRV_ENUMERATE_SNAPSHOTS = 0x00144064
|
||||
public static let SRV_ENUMERATE_SNAPSHOTS = IOCtlCode(rawValue: 0x00144064)
|
||||
/// Reads a chunk of file for performing server side copy operations.
|
||||
case SRV_COPYCHUNK = 0x001440F2
|
||||
public static let SRV_COPYCHUNK = IOCtlCode(rawValue: 0x001440F2)
|
||||
/// Retrieve data from the Content Information File associated with a specified file, not valid for the SMB 2.0.2 dialect.
|
||||
case SRV_READ_HASH = 0x001441BB
|
||||
public static let SRV_READ_HASH = IOCtlCode(rawValue: 0x001441BB)
|
||||
/// Writes the chunk of file for performing server side copy operations.
|
||||
case SRV_COPYCHUNK_WRITE = 0x001480F2
|
||||
public static let SRV_COPYCHUNK_WRITE = IOCtlCode(rawValue: 0x001480F2)
|
||||
/// Request resiliency for a specified open file, not valid for the SMB 2.0.2 dialect.
|
||||
case LMR_REQUEST_RESILIENCY = 0x001401D4
|
||||
public static let LMR_REQUEST_RESILIENCY = IOCtlCode(rawValue: 0x001401D4)
|
||||
/// Get server network interface info e.g. link speed and socket address information
|
||||
case QUERY_NETWORK_INTERFACE_INFO = 0x001401FC
|
||||
public static let QUERY_NETWORK_INTERFACE_INFO = IOCtlCode(rawValue: 0x001401FC)
|
||||
/// Request validation of a previous SMB 2 NEGOTIATE, valid for SMB 3.0 and SMB 3.0.2 dialects.
|
||||
case VALIDATE_NEGOTIATE_INFO = 0x00140204
|
||||
public static let VALIDATE_NEGOTIATE_INFO = IOCtlCode(rawValue: 0x00140204)
|
||||
}
|
||||
|
||||
struct IOCtlRequestData {
|
||||
struct CopyChunk: IOCtlRequestProtocol {
|
||||
static var command: SMB2.Command = .IOCTL
|
||||
|
||||
let sourceKey: (UInt64, UInt64, UInt64)
|
||||
let chunkCount: UInt32
|
||||
let chunks: [Chunk]
|
||||
@@ -156,31 +160,26 @@ extension SMB2 {
|
||||
}
|
||||
|
||||
struct ReadHash: IOCtlRequestProtocol {
|
||||
let _hashType: UInt32
|
||||
var hashType: IOCtlHashType {
|
||||
return IOCtlHashType(rawValue: _hashType) ?? .PEER_DIST
|
||||
}
|
||||
let _hashVersion: UInt32
|
||||
var hashVersion: IOCtlHashVersion {
|
||||
return IOCtlHashVersion(rawValue: _hashVersion) ?? .VER_1
|
||||
}
|
||||
let _hashRetrievalType: UInt32
|
||||
var hashRetrievalType: IOCtlHashRetrievalType {
|
||||
return IOCtlHashRetrievalType(rawValue: _hashRetrievalType) ?? .FILE_BASED
|
||||
}
|
||||
static var command: SMB2.Command = .IOCTL
|
||||
|
||||
let _hashType: IOCtlHashType
|
||||
let _hashVersion: IOCtlHashVersion
|
||||
let _hashRetrievalType: IOCtlHashRetrievalType
|
||||
let length: UInt32
|
||||
let offset: UInt64
|
||||
|
||||
init(offset: UInt64, length: UInt32, hashType: IOCtlHashType = .PEER_DIST, hashVersion: IOCtlHashVersion = .VER_1, hashRetrievalType: IOCtlHashRetrievalType = .FILE_BASED) {
|
||||
self._hashType = hashType.rawValue
|
||||
self._hashVersion = hashVersion.rawValue
|
||||
self._hashRetrievalType = hashRetrievalType.rawValue
|
||||
self._hashType = hashType
|
||||
self._hashVersion = hashVersion
|
||||
self._hashRetrievalType = hashRetrievalType
|
||||
self.length = length
|
||||
self.offset = offset
|
||||
}
|
||||
}
|
||||
|
||||
struct ResilencyRequest: IOCtlRequestProtocol {
|
||||
static var command: SMB2.Command = .IOCTL
|
||||
|
||||
let timeout: UInt32
|
||||
fileprivate let reserved: UInt32
|
||||
|
||||
@@ -192,6 +191,8 @@ extension SMB2 {
|
||||
}
|
||||
|
||||
struct ValidateNegotiateInfo: IOCtlRequestProtocol {
|
||||
static var command: SMB2.Command = .IOCTL
|
||||
|
||||
let header: ValidateNegotiateInfo.Header
|
||||
let dialects: [UInt16]
|
||||
|
||||
@@ -309,11 +310,11 @@ extension SMB2 {
|
||||
static let ipv6: sa_family_t = 0x17
|
||||
|
||||
var sockaddr: sockaddr_in {
|
||||
return Data.mapMemory(from: self.sockaddrStorage)!
|
||||
return unsafeBitCast(self.sockaddrStorage, to: sockaddr_in.self)
|
||||
}
|
||||
|
||||
var sockaddr6: sockaddr_in6 {
|
||||
return Data.mapMemory(from: self.sockaddrStorage)!
|
||||
return unsafeBitCast(self.sockaddrStorage, to: sockaddr_in6.self)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -340,17 +341,35 @@ extension SMB2 {
|
||||
static let RDMA_CAPABLE = IOCtlCapabilities(rawValue: 0x00000002)
|
||||
}
|
||||
|
||||
enum IOCtlHashType: UInt32 {
|
||||
case PEER_DIST = 0x00000001
|
||||
struct IOCtlHashType: Option {
|
||||
let rawValue: UInt32
|
||||
|
||||
init(rawValue: UInt32) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
public static let PEER_DIST = IOCtlHashType(rawValue: 0x00000001)
|
||||
}
|
||||
|
||||
enum IOCtlHashVersion: UInt32 {
|
||||
case VER_1 = 0x00000001
|
||||
case VER_2 = 0x00000002
|
||||
struct IOCtlHashVersion: Option {
|
||||
let rawValue: UInt32
|
||||
|
||||
init(rawValue: UInt32) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
public static let VER_1 = IOCtlHashVersion(rawValue: 0x00000001)
|
||||
public static let VER_2 = IOCtlHashVersion(rawValue: 0x00000002)
|
||||
}
|
||||
|
||||
enum IOCtlHashRetrievalType: UInt32 {
|
||||
case HASH_BASED = 0x00000001
|
||||
case FILE_BASED = 0x00000002
|
||||
struct IOCtlHashRetrievalType: Option {
|
||||
let rawValue: UInt32
|
||||
|
||||
init(rawValue: UInt32) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
public static let HASH_BASED = IOCtlHashRetrievalType(rawValue: 0x00000001)
|
||||
public static let FILE_BASED = IOCtlHashRetrievalType(rawValue: 0x00000002)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ extension SMB2 {
|
||||
// MARK: SMB2 Change Notify
|
||||
|
||||
struct ChangeNotifyRequest: SMBRequestBody {
|
||||
static var command: SMB2.Command = .CHANGE_NOTIFY
|
||||
|
||||
let size: UInt16
|
||||
let flags: ChangeNotifyRequest.Flags
|
||||
let outputBufferLength: UInt32
|
||||
@@ -87,9 +89,7 @@ extension SMB2 {
|
||||
while i < maxLoop {
|
||||
let nextOffset: UInt32 = data.scanValue(start: offset) ?? 0
|
||||
let actionValue: UInt32 = data.scanValue(start: offset + 4) ?? 0
|
||||
guard let action = FileNotifyAction(rawValue: actionValue) else {
|
||||
continue
|
||||
}
|
||||
let action = FileNotifyAction(rawValue: actionValue)
|
||||
|
||||
let fileNameLen = Int(data.scanValue(start: offset + 8) as UInt32? ?? 0)
|
||||
let fileName = data.scanString(start: offset + 12, length: fileNameLen, using: .utf16) ?? ""
|
||||
@@ -106,28 +106,38 @@ extension SMB2 {
|
||||
}
|
||||
}
|
||||
|
||||
enum FileNotifyAction: UInt32 {
|
||||
struct FileNotifyAction: Option {
|
||||
init(rawValue: UInt32) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
init(_ rawValue: UInt32) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
let rawValue: UInt32
|
||||
|
||||
/// The file was added to the directory.
|
||||
case ADDED = 0x00000001
|
||||
public static let ADDED = FileNotifyAction(0x00000001)
|
||||
/// The file was removed from the directory.
|
||||
case REMOVED = 0x00000002
|
||||
public static let REMOVED = FileNotifyAction(0x00000002)
|
||||
/// The file was modified. This can be a change to the data or attributes of the file.
|
||||
case MODIFIED = 0x00000003
|
||||
public static let MODIFIED = FileNotifyAction(0x00000003)
|
||||
/// The file was renamed, and this is the old name. If the new name resides outside of the directory being monitored, the client will not receive the FILE_ACTION_RENAMED_NEW_NAME bit value.
|
||||
case RENAMED_OLD_NAME = 0x00000004
|
||||
public static let RENAMED_OLD_NAME = FileNotifyAction(0x00000004)
|
||||
/// The file was renamed, and this is the new name. If the old name resides outside of the directory being monitored, the client will not receive the FILE_ACTION_RENAME_OLD_NAME bit value.
|
||||
case RENAMED_NEW_NAME = 0x00000005
|
||||
public static let RENAMED_NEW_NAME = FileNotifyAction(0x00000005)
|
||||
/// The file was added to a named stream.
|
||||
case ADDED_STREAM = 0x00000006
|
||||
public static let ADDED_STREAM = FileNotifyAction(0x00000006)
|
||||
/// The file was removed from the named stream.
|
||||
case REMOVED_STREAM = 0x00000007
|
||||
public static let REMOVED_STREAM = FileNotifyAction(0x00000007)
|
||||
/// The file was modified. This can be a change to the data or attributes of the file.
|
||||
case MODIFIED_STREAM = 0x00000008
|
||||
public static let MODIFIED_STREAM = FileNotifyAction(0x00000008)
|
||||
/// An object ID was removed because the file the object ID referred to was deleted. This notification is only sent when the directory being monitored is the special directory "\$Extend\$ObjId:$O:$INDEX_ALLOCATION".
|
||||
case REMOVED_BY_DELETE = 0x00000009
|
||||
public static let REMOVED_BY_DELETE = FileNotifyAction(0x00000009)
|
||||
/// An attempt to tunnel object ID information to a file being created or renamed failed because the object ID is in use by another file on the same volume. This notification is only sent when the directory being monitored is the special directory "\$Extend\$ObjId:$O:$INDEX_ALLOCATION".
|
||||
case NOT_TUNNELLED = 0x0000000A
|
||||
public static let NOT_TUNNELLED = FileNotifyAction(0x0000000A)
|
||||
/// An attempt to tunnel object ID information to a file being renamed failed because the file already has an object ID. This notification is only sent when the directory being monitored is the special directory "\$Extend\$ObjId:$O:$INDEX_ALLOCATION".
|
||||
case TUNNELLED_ID_COLLISION = 0x0000000B
|
||||
public static let TUNNELLED_ID_COLLISION = FileNotifyAction(0x0000000B)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ extension SMB2 {
|
||||
// MARK: SMB2 Query Directory
|
||||
|
||||
struct QueryDirectoryRequest: SMBRequestBody {
|
||||
static let command: SMB2.Command = .QUERY_DIRECTORY
|
||||
|
||||
let header: QueryDirectoryRequest.Header
|
||||
let searchPattern: String?
|
||||
|
||||
@@ -70,17 +72,23 @@ extension SMB2 {
|
||||
let header: SMB2FilesInformationHeader
|
||||
switch type {
|
||||
case .fileDirectoryInformation:
|
||||
header = buffer.scanValue(start: offset) as FileDirectoryInformationHeader!
|
||||
let tHeader: FileDirectoryInformationHeader = buffer.scanValue(start: offset)!
|
||||
header = tHeader
|
||||
case .fileFullDirectoryInformation:
|
||||
header = buffer.scanValue(start: offset) as FileFullDirectoryInformationHeader!
|
||||
let tHeader: FileFullDirectoryInformationHeader = buffer.scanValue(start: offset)!
|
||||
header = tHeader
|
||||
case .fileIdFullDirectoryInformation:
|
||||
header = buffer.scanValue(start: offset) as FileIdFullDirectoryInformationHeader!
|
||||
let tHeader: FileIdFullDirectoryInformationHeader = buffer.scanValue(start: offset)!
|
||||
header = tHeader
|
||||
case .fileBothDirectoryInformation:
|
||||
header = buffer.scanValue(start: offset) as FileBothDirectoryInformationHeader!
|
||||
let tHeader: FileBothDirectoryInformationHeader = buffer.scanValue(start: offset)!
|
||||
header = tHeader
|
||||
case .fileIdBothDirectoryInformation:
|
||||
header = buffer.scanValue(start: offset) as FileIdBothDirectoryInformationHeader!
|
||||
let tHeader: FileIdBothDirectoryInformationHeader = buffer.scanValue(start: offset)!
|
||||
header = tHeader
|
||||
case .fileNamesInformation:
|
||||
header = buffer.scanValue(start: offset) as FileNamesInformationHeader!
|
||||
let tHeader: FileNamesInformationHeader = buffer.scanValue(start: offset)!
|
||||
header = tHeader
|
||||
default:
|
||||
return []
|
||||
}
|
||||
@@ -96,8 +104,10 @@ extension SMB2 {
|
||||
}
|
||||
|
||||
init? (data: Data) {
|
||||
let offset = Int(data.scanValue(start: 2) as UInt16!)
|
||||
let length = Int(data.scanValue(start: 4) as UInt32!)
|
||||
let tOffset: UInt16 = data.scanValue(start: 2)!
|
||||
let offset = Int(tOffset)
|
||||
let tLength: UInt32 = data.scanValue(start: 4)!
|
||||
let length = Int(tLength)
|
||||
guard data.count > offset + length else {
|
||||
return nil
|
||||
}
|
||||
@@ -108,6 +118,8 @@ extension SMB2 {
|
||||
// MARK: SMB2 Query Info
|
||||
|
||||
struct QueryInfoRequest: SMBRequestBody {
|
||||
static var command: SMB2.Command = .QUERY_INFO
|
||||
|
||||
let header: Header
|
||||
let buffer: Data?
|
||||
|
||||
@@ -196,7 +208,8 @@ extension SMB2 {
|
||||
/*let offsetData = data.subdataWithRange(NSRange(location: 2, length: 2))
|
||||
let offset: UInt16 = decode(offsetData)*/
|
||||
|
||||
let length = Int(data.scanValue(start: 4) as UInt32!)
|
||||
let tLength: UInt32 = data.scanValue(start: 4)!
|
||||
let length = Int(tLength)
|
||||
|
||||
guard data.count >= 8 + length else {
|
||||
return nil
|
||||
|
||||
@@ -15,88 +15,100 @@ protocol SMB2FilesInformationHeader: SMBResponseBody {
|
||||
}
|
||||
|
||||
extension SMB2 {
|
||||
enum FileInformationEnum: UInt8 {
|
||||
case none = 0x00
|
||||
case fileDirectoryInformation = 0x01
|
||||
case fileFullDirectoryInformation = 0x02
|
||||
case fileBothDirectoryInformation = 0x03
|
||||
case fileBasicInformation = 0x04
|
||||
case fileStandardInformation = 0x05
|
||||
case fileInternalInformation = 0x06
|
||||
case fileEaInformation = 0x07
|
||||
case fileAccessInformation = 0x08
|
||||
case fileNameInformation = 0x09
|
||||
case fileRenameInformation = 0x0A
|
||||
case fileLinkInformation = 0x0B
|
||||
case fileNamesInformation = 0x0C
|
||||
case fileDispositionInformation = 0x0D
|
||||
case filePositionInformation = 0x0E
|
||||
case fileFullEaInformation = 0x0F
|
||||
case fileModeInformation = 0x10
|
||||
case fileAlignmentInformation = 0x11
|
||||
case fileAllInformation = 0x12
|
||||
case fileAllocationInformation = 0x13
|
||||
case fileEndOfFileInformation = 0x14
|
||||
case fileAlternateNameInformation = 0x15
|
||||
case fileStreamInformation = 0x16
|
||||
case filePipeInformation = 0x17
|
||||
case filePipeLocalInformation = 0x18
|
||||
case filePipeRemoteInformation = 0x19
|
||||
case fileMailslotQueryInformation = 0x1A
|
||||
case fileMailslotSetInformation = 0x1B
|
||||
case fileCompressionInformation = 0x1C
|
||||
case fileObjectIdInformation = 0x1D
|
||||
case fileCompletionInformation = 0x1E
|
||||
case fileMoveClusterInformation = 0x1F
|
||||
case fileQuotaInformation = 0x20
|
||||
case fileReparsePointInformation = 0x21
|
||||
case fileNetworkOpenInformation = 0x22
|
||||
case fileAttributeTagInformation = 0x23
|
||||
case fileTrackingInformation = 0x24
|
||||
case fileIdBothDirectoryInformation = 0x25
|
||||
case fileIdFullDirectoryInformation = 0x26
|
||||
case fileValidDataLengthInformation = 0x27
|
||||
case fileShortNameInformation = 0x28
|
||||
case fileIoCompletionNotificationInformation = 0x29
|
||||
case fileIoStatusBlockRangeInformation = 0x2A
|
||||
case fileIoPriorityHintInformation = 0x2B
|
||||
case fileSfioReserveInformation = 0x2C
|
||||
case fileSfioVolumeInformation = 0x2D
|
||||
case fileHardLinkInformation = 0x2E
|
||||
case fileProcessIdsUsingFileInformation = 0x2F
|
||||
case fileNormalizedNameInformation = 0x30
|
||||
case fileNetworkPhysicalNameInformation = 0x31
|
||||
case fileIdGlobalTxDirectoryInformation = 0x32
|
||||
case fileIsRemoteDeviceInformation = 0x33
|
||||
case fileUnusedInformation = 0x34
|
||||
case fileNumaNodeInformation = 0x35
|
||||
case fileStandardLinkInformation = 0x36
|
||||
case fileRemoteProtocolInformation = 0x37
|
||||
case fileRenameInformationBypassAccessCheck = 0x38
|
||||
case fileLinkInformationBypassAccessCheck = 0x39
|
||||
case fileVolumeNameInformation = 0x3A
|
||||
case fileIdInformation = 0x3B
|
||||
case fileIdExtdDirectoryInformation = 0x3C
|
||||
case fileReplaceCompletionInformation = 0x3D
|
||||
case fileHardLinkFullIdInformation = 0x3E
|
||||
case fileIdExtdBothDirectoryInformation = 0x3F
|
||||
case fileMaximumInformation = 0x40
|
||||
struct FileInformationEnum: Option {
|
||||
let rawValue: UInt8
|
||||
|
||||
init(rawValue: UInt8) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
public static let none = 0x00
|
||||
public static let fileDirectoryInformation = FileInformationEnum(rawValue: 0x01)
|
||||
public static let fileFullDirectoryInformation = FileInformationEnum(rawValue: 0x02)
|
||||
public static let fileBothDirectoryInformation = FileInformationEnum(rawValue: 0x03)
|
||||
public static let fileBasicInformation = FileInformationEnum(rawValue: 0x04)
|
||||
public static let fileStandardInformation = FileInformationEnum(rawValue: 0x05)
|
||||
public static let fileInternalInformation = FileInformationEnum(rawValue: 0x06)
|
||||
public static let fileEaInformation = FileInformationEnum(rawValue: 0x07)
|
||||
public static let fileAccessInformation = FileInformationEnum(rawValue: 0x08)
|
||||
public static let fileNameInformation = FileInformationEnum(rawValue: 0x09)
|
||||
public static let fileRenameInformation = FileInformationEnum(rawValue: 0x0A)
|
||||
public static let fileLinkInformation = FileInformationEnum(rawValue: 0x0B)
|
||||
public static let fileNamesInformation = FileInformationEnum(rawValue: 0x0C)
|
||||
public static let fileDispositionInformation = FileInformationEnum(rawValue: 0x0D)
|
||||
public static let filePositionInformation = FileInformationEnum(rawValue: 0x0E)
|
||||
public static let fileFullEaInformation = FileInformationEnum(rawValue: 0x0F)
|
||||
public static let fileModeInformation = FileInformationEnum(rawValue: 0x10)
|
||||
public static let fileAlignmentInformation = FileInformationEnum(rawValue: 0x11)
|
||||
public static let fileAllInformation = FileInformationEnum(rawValue: 0x12)
|
||||
public static let fileAllocationInformation = FileInformationEnum(rawValue: 0x13)
|
||||
public static let fileEndOfFileInformation = FileInformationEnum(rawValue: 0x14)
|
||||
public static let fileAlternateNameInformation = FileInformationEnum(rawValue: 0x15)
|
||||
public static let fileStreamInformation = FileInformationEnum(rawValue: 0x16)
|
||||
public static let filePipeInformation = FileInformationEnum(rawValue: 0x17)
|
||||
public static let filePipeLocalInformation = FileInformationEnum(rawValue: 0x18)
|
||||
public static let filePipeRemoteInformation = FileInformationEnum(rawValue: 0x19)
|
||||
public static let fileMailslotQueryInformation = FileInformationEnum(rawValue: 0x1A)
|
||||
public static let fileMailslotSetInformation = FileInformationEnum(rawValue: 0x1B)
|
||||
public static let fileCompressionInformation = FileInformationEnum(rawValue: 0x1C)
|
||||
public static let fileObjectIdInformation = FileInformationEnum(rawValue: 0x1D)
|
||||
public static let fileCompletionInformation = FileInformationEnum(rawValue: 0x1E)
|
||||
public static let fileMoveClusterInformation = FileInformationEnum(rawValue: 0x1F)
|
||||
public static let fileQuotaInformation = FileInformationEnum(rawValue: 0x20)
|
||||
public static let fileReparsePointInformation = FileInformationEnum(rawValue: 0x21)
|
||||
public static let fileNetworkOpenInformation = FileInformationEnum(rawValue: 0x22)
|
||||
public static let fileAttributeTagInformation = FileInformationEnum(rawValue: 0x23)
|
||||
public static let fileTrackingInformation = FileInformationEnum(rawValue: 0x24)
|
||||
public static let fileIdBothDirectoryInformation = FileInformationEnum(rawValue: 0x25)
|
||||
public static let fileIdFullDirectoryInformation = FileInformationEnum(rawValue: 0x26)
|
||||
public static let fileValidDataLengthInformation = FileInformationEnum(rawValue: 0x27)
|
||||
public static let fileShortNameInformation = FileInformationEnum(rawValue: 0x28)
|
||||
public static let fileIoCompletionNotificationInformation = FileInformationEnum(rawValue: 0x29)
|
||||
public static let fileIoStatusBlockRangeInformation = FileInformationEnum(rawValue: 0x2A)
|
||||
public static let fileIoPriorityHintInformation = FileInformationEnum(rawValue: 0x2B)
|
||||
public static let fileSfioReserveInformation = FileInformationEnum(rawValue: 0x2C)
|
||||
public static let fileSfioVolumeInformation = FileInformationEnum(rawValue: 0x2D)
|
||||
public static let fileHardLinkInformation = FileInformationEnum(rawValue: 0x2E)
|
||||
public static let fileProcessIdsUsingFileInformation = FileInformationEnum(rawValue: 0x2F)
|
||||
public static let fileNormalizedNameInformation = FileInformationEnum(rawValue: 0x30)
|
||||
public static let fileNetworkPhysicalNameInformation = FileInformationEnum(rawValue: 0x31)
|
||||
public static let fileIdGlobalTxDirectoryInformation = FileInformationEnum(rawValue: 0x32)
|
||||
public static let fileIsRemoteDeviceInformation = FileInformationEnum(rawValue: 0x33)
|
||||
public static let fileUnusedInformation = FileInformationEnum(rawValue: 0x34)
|
||||
public static let fileNumaNodeInformation = FileInformationEnum(rawValue: 0x35)
|
||||
public static let fileStandardLinkInformation = FileInformationEnum(rawValue: 0x36)
|
||||
public static let fileRemoteProtocolInformation = FileInformationEnum(rawValue: 0x37)
|
||||
public static let fileRenameInformationBypassAccessCheck = FileInformationEnum(rawValue: 0x38)
|
||||
public static let fileLinkInformationBypassAccessCheck = FileInformationEnum(rawValue: 0x39)
|
||||
public static let fileVolumeNameInformation = FileInformationEnum(rawValue: 0x3A)
|
||||
public static let fileIdInformation = FileInformationEnum(rawValue: 0x3B)
|
||||
public static let fileIdExtdDirectoryInformation = FileInformationEnum(rawValue: 0x3C)
|
||||
public static let fileReplaceCompletionInformation = FileInformationEnum(rawValue: 0x3D)
|
||||
public static let fileHardLinkFullIdInformation = FileInformationEnum(rawValue: 0x3E)
|
||||
public static let fileIdExtdBothDirectoryInformation = FileInformationEnum(rawValue: 0x3F)
|
||||
public static let fileMaximumInformation = FileInformationEnum(rawValue: 0x40)
|
||||
|
||||
static let queryDirectory: [FileInformationEnum] = [.fileDirectoryInformation, .fileFullDirectoryInformation, .fileIdFullDirectoryInformation, .fileBothDirectoryInformation, .fileIdBothDirectoryInformation, .fileNamesInformation]
|
||||
|
||||
static let queryInfoFile: [FileInformationEnum] = [.fileAccessInformation, .fileAlignmentInformation, .fileAllInformation, .fileAlternateNameInformation, .fileAttributeTagInformation, .fileBasicInformation, .fileCompressionInformation, fileEaInformation, .fileFullEaInformation, .fileInternalInformation, .fileModeInformation, .fileNetworkOpenInformation, .filePipeInformation, .filePipeLocalInformation, .filePipeRemoteInformation, .filePositionInformation, .fileStandardInformation, .fileStreamInformation]
|
||||
}
|
||||
|
||||
enum FileSystemInformationEnum: UInt8 {
|
||||
case none = 0
|
||||
case fileFsAttributeInformation
|
||||
case fileFsControlInformation
|
||||
case fileFsDeviceInformation
|
||||
case fileFsFullSizeInformation
|
||||
case fileFsObjectIdInformation
|
||||
case fileFsSectorSizeInformation
|
||||
case fileFsSizeInformation
|
||||
case fileFsVolumeInformation
|
||||
struct FileSystemInformationEnum: Option {
|
||||
let rawValue: UInt8
|
||||
|
||||
init(rawValue: UInt8) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
public static let none = FileSystemInformationEnum(rawValue: 0x00)
|
||||
public static let fileFsAttributeInformation = FileSystemInformationEnum(rawValue: 0x01)
|
||||
public static let fileFsControlInformation = FileSystemInformationEnum(rawValue: 0x02)
|
||||
public static let fileFsDeviceInformation = FileSystemInformationEnum(rawValue: 0x03)
|
||||
public static let fileFsFullSizeInformation = FileSystemInformationEnum(rawValue: 0x04)
|
||||
public static let fileFsObjectIdInformation = FileSystemInformationEnum(rawValue: 0x05)
|
||||
public static let fileFsSectorSizeInformation = FileSystemInformationEnum(rawValue: 0x06)
|
||||
public static let fileFsSizeInformation = FileSystemInformationEnum(rawValue: 0x07)
|
||||
public static let fileFsVolumeInformation = FileSystemInformationEnum(rawValue: 0x08)
|
||||
}
|
||||
|
||||
struct FileSecurityInfo: OptionSet {
|
||||
@@ -303,71 +315,89 @@ extension SMB2 {
|
||||
}
|
||||
|
||||
struct FilePipeInformation {
|
||||
fileprivate let _readMode: UInt32
|
||||
var readMode: ReadMode {
|
||||
return ReadMode(rawValue: _readMode) ?? .BYTE_STREAM_MODE
|
||||
}
|
||||
fileprivate let _completionMode: UInt32
|
||||
var completionMode: CompletionMode {
|
||||
return CompletionMode(rawValue: _completionMode) ?? .QUEUE_OPERATION
|
||||
let readMode: ReadMode
|
||||
fileprivate let completionMode: CompletionMode
|
||||
|
||||
struct ReadMode: Option {
|
||||
let rawValue: UInt32
|
||||
|
||||
init(rawValue: UInt32) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
public static let BYTE_STREAM_MODE = ReadMode(rawValue: 0x00000000)
|
||||
public static let MESSAGE_MODE = ReadMode(rawValue: 0x00000001)
|
||||
}
|
||||
|
||||
enum ReadMode: UInt32 {
|
||||
case BYTE_STREAM_MODE = 0x00000000
|
||||
case MESSAGE_MODE = 0x00000001
|
||||
}
|
||||
|
||||
enum CompletionMode: UInt32 {
|
||||
case QUEUE_OPERATION = 0x00000000
|
||||
case COMPLETE_OPERATION = 0x00000001
|
||||
struct CompletionMode: Option {
|
||||
let rawValue: UInt32
|
||||
|
||||
init(rawValue: UInt32) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
public static let QUEUE_OPERATION = CompletionMode(rawValue: 0x00000000)
|
||||
public static let COMPLETE_OPERATION = CompletionMode(rawValue: 0x00000001)
|
||||
}
|
||||
}
|
||||
|
||||
struct FilePipeLocalInformation {
|
||||
fileprivate let _namedPipeType: UInt32
|
||||
var namedPipeType: Type {
|
||||
return Type(rawValue: _namedPipeType) ?? .BYTE_STREAM_TYPE
|
||||
}
|
||||
fileprivate let _namedPipeConfiguration: UInt32
|
||||
var namedPipeConfiguration: Configuration {
|
||||
return Configuration(rawValue: _namedPipeConfiguration) ?? .INBOUND
|
||||
}
|
||||
let namedPipeType: Type
|
||||
let namedPipeConfiguration: Configuration
|
||||
let maximumInstances: UInt32
|
||||
let currentInstances: UInt32
|
||||
let inboundQuota: UInt32
|
||||
let readDataAvailable: UInt32
|
||||
let outboundQuota: UInt32
|
||||
let writeQuotaAvailable: UInt32
|
||||
fileprivate let _namedPipeState: UInt32
|
||||
var namedPipeState: State {
|
||||
return State(rawValue: _namedPipeState) ?? .DISCONNECTED_STATE
|
||||
}
|
||||
fileprivate let _namedPipeEnd: UInt32
|
||||
var namedPipeEnd: End {
|
||||
return End(rawValue: _namedPipeEnd) ?? .CLIENT_END
|
||||
let namedPipeState: State
|
||||
let namedPipeEnd: End
|
||||
|
||||
struct `Type`: Option {
|
||||
let rawValue: UInt32
|
||||
|
||||
init(rawValue: UInt32) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
public static let BYTE_STREAM_TYPE = `Type`(rawValue: 0x00000000)
|
||||
public static let MESSAGE_TYPE = `Type`(rawValue: 0x00000001)
|
||||
}
|
||||
|
||||
enum `Type`: UInt32 {
|
||||
case BYTE_STREAM_TYPE = 0x00000000
|
||||
case MESSAGE_TYPE = 0x00000001
|
||||
struct Configuration: Option {
|
||||
let rawValue: UInt32
|
||||
|
||||
init(rawValue: UInt32) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
public static let INBOUND = Configuration(rawValue: 0x00000000)
|
||||
public static let OUTBOUND = Configuration(rawValue: 0x00000001)
|
||||
public static let FULL_DUPLEX = Configuration(rawValue: 0x00000002)
|
||||
}
|
||||
|
||||
enum Configuration: UInt32 {
|
||||
case INBOUND = 0x00000000
|
||||
case OUTBOUND = 0x00000001
|
||||
case FULL_DUPLEX = 0x00000002
|
||||
struct State: Option {
|
||||
let rawValue: UInt32
|
||||
|
||||
init(rawValue: UInt32) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
public static let DISCONNECTED_STATE = State(rawValue: 0x00000001)
|
||||
public static let LISTENING_STATE = State(rawValue: 0x00000002)
|
||||
public static let CONNECTED_STATE = State(rawValue: 0x00000003)
|
||||
public static let CLOSING_STATE = State(rawValue: 0x00000004)
|
||||
}
|
||||
|
||||
enum State: UInt32 {
|
||||
case DISCONNECTED_STATE = 0x00000001
|
||||
case LISTENING_STATE = 0x00000002
|
||||
case CONNECTED_STATE = 0x00000003
|
||||
case CLOSING_STATE = 0x00000004
|
||||
}
|
||||
|
||||
enum End: UInt32 {
|
||||
case CLIENT_END = 0x00000000
|
||||
case SERVER_END = 0x00000001
|
||||
struct End: Option {
|
||||
let rawValue: UInt32
|
||||
|
||||
init(rawValue: UInt32) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
public static let CLIENT_END = End(rawValue: 0x00000000)
|
||||
public static let SERVER_END = End(rawValue: 0x00000001)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -412,15 +442,18 @@ extension SMB2 {
|
||||
}
|
||||
|
||||
struct FileFsDeviceInformation {
|
||||
fileprivate let _deviceType: UInt32
|
||||
var deviceType: DeviceType {
|
||||
return DeviceType(rawValue: _deviceType) ?? .DISK
|
||||
}
|
||||
let deviceType: DeviceType
|
||||
let charactristics: Charactristics
|
||||
|
||||
enum DeviceType: UInt32 {
|
||||
case CD_ROM = 0x00000002
|
||||
case DISK = 0x00000007
|
||||
struct DeviceType: Option {
|
||||
let rawValue: UInt32
|
||||
|
||||
init(rawValue: UInt32) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
public static let CD_ROM = DeviceType(rawValue: 0x00000002)
|
||||
public static let DISK = DeviceType(rawValue: 0x00000007)
|
||||
}
|
||||
|
||||
struct Charactristics: OptionSet {
|
||||
@@ -471,7 +504,7 @@ extension SMB2 {
|
||||
|
||||
/// The file system supports case-sensitive file names when looking up (searching for) file names in a directory.
|
||||
static let CASE_SENSITIVE_SEARCH = Attributes(rawValue: 0x00000001)
|
||||
/// The file system preserves the case of file names when it places a name on disk.
|
||||
/// The file system preserves the public static let of file names when it places a name on disk.
|
||||
static let CASE_PRESERVED_NAMES = Attributes(rawValue: 0x00000002)
|
||||
/// The file system supports Unicode in file and directory names. This flag applies only to file and directory names; the file system neither restricts nor interprets the bytes of data within a file.
|
||||
static let UNICODE_ON_DISK = Attributes(rawValue: 0x00000004)
|
||||
|
||||
@@ -12,6 +12,8 @@ extension SMB2 {
|
||||
// MARK: SMB2 Negotiating
|
||||
|
||||
struct NegotiateRequest: SMBRequestBody {
|
||||
static var command: SMB2.Command = .NEGOTIATE
|
||||
|
||||
let header: NegotiateRequest.Header
|
||||
let dialects: [UInt16]
|
||||
let contexts: [(type: NegotiateContextType, data: Data)]
|
||||
@@ -64,7 +66,10 @@ extension SMB2 {
|
||||
var contextCount: UInt16
|
||||
fileprivate let reserved2: UInt16
|
||||
var clientStartTime: SMBTime {
|
||||
let time = Int64(contextOffset) + (Int64(contextCount) << 32) + (Int64(contextCount) << 48)
|
||||
let lo = Int64(contextOffset)
|
||||
let hi1 = Int64(contextCount) << 32
|
||||
let hi2 = Int64(contextCount) << 48
|
||||
let time: Int64 = lo + hi1 + hi2
|
||||
return SMBTime(time: time)
|
||||
}
|
||||
|
||||
@@ -175,6 +180,8 @@ extension SMB2 {
|
||||
// MARK: SMB2 Session Setup
|
||||
|
||||
struct SessionSetupRequest: SMBRequestBody {
|
||||
static var command: SMB2.Command = .SESSION_SETUP
|
||||
|
||||
let header: SessionSetupRequest.Header
|
||||
let buffer: Data?
|
||||
|
||||
@@ -288,6 +295,8 @@ extension SMB2 {
|
||||
// MARK: SMB2 Log off
|
||||
|
||||
struct LogOff: SMBRequestBody, SMBResponseBody {
|
||||
static var command: SMB2.Command = .LOGOFF
|
||||
|
||||
let size: UInt16
|
||||
let reserved: UInt16
|
||||
|
||||
@@ -300,6 +309,8 @@ extension SMB2 {
|
||||
// MARK: SMB2 Echo
|
||||
|
||||
struct Echo: SMBRequestBody, SMBResponseBody {
|
||||
static var command: SMB2.Command = .ECHO
|
||||
|
||||
let size: UInt16
|
||||
let reserved: UInt16
|
||||
|
||||
|
||||
@@ -11,6 +11,8 @@ import Foundation
|
||||
extension SMB2 {
|
||||
// MARK: SMB2 Set Info
|
||||
struct SetInfoRequest: SMBRequestBody {
|
||||
static var command: SMB2.Command = .SET_INFO
|
||||
|
||||
let header: Header
|
||||
let buffer: Data?
|
||||
|
||||
|
||||
@@ -12,13 +12,15 @@ extension SMB2 {
|
||||
// MARK: SMB2 Tree Connect
|
||||
|
||||
struct TreeConnectRequest: SMBRequestBody {
|
||||
static var command: SMB2.Command = .TREE_CONNECT
|
||||
|
||||
let header: TreeConnectRequest.Header
|
||||
let buffer: Data?
|
||||
var path: String {
|
||||
return ""
|
||||
return buffer.flatMap({ String.init(data: $0, encoding: .utf16) }) ?? ""
|
||||
}
|
||||
var share: String {
|
||||
return ""
|
||||
return path.split(separator: "/", omittingEmptySubsequences: true).first.map(String.init) ?? ""
|
||||
}
|
||||
|
||||
init? (header: TreeConnectRequest.Header, host: String, share: String) {
|
||||
@@ -26,7 +28,7 @@ extension SMB2 {
|
||||
return nil
|
||||
}
|
||||
self.header = header
|
||||
let path = "\\\\\(host)\\\(share)"
|
||||
let path = "\\\\" + host + "\\" + share
|
||||
self.buffer = path.data(using: .utf16)
|
||||
}
|
||||
|
||||
@@ -125,6 +127,8 @@ extension SMB2 {
|
||||
// MARK: SMB2 Tree Disconnect
|
||||
|
||||
struct TreeDisconnect: SMBRequestBody, SMBResponseBody {
|
||||
static var command: SMB2.Command = .TREE_DISCONNECT
|
||||
|
||||
let size: UInt16
|
||||
let reserved: UInt16
|
||||
|
||||
|
||||
@@ -22,20 +22,17 @@ struct SMB2 {
|
||||
let size: UInt16
|
||||
let creditCharge: UInt16
|
||||
// error messages from the server to the client
|
||||
let status: UInt32
|
||||
let status: NTStatus
|
||||
enum StatusSeverity: UInt8 {
|
||||
case success = 0, information, warning, error
|
||||
}
|
||||
var statusDetails: (severity: StatusSeverity, customer: Bool, facility: UInt16, code: UInt16) {
|
||||
let severity = StatusSeverity(rawValue: UInt8(status >> 30))!
|
||||
return (severity, status & 0x20000000 != 0, UInt16((status & 0x0FFF0000) >> 16), UInt16(status & 0x0000FFFF))
|
||||
}
|
||||
fileprivate let _command: UInt16
|
||||
var command: Command {
|
||||
get {
|
||||
return Command(rawValue: _command) ?? .INVALID
|
||||
}
|
||||
let severity = StatusSeverity(rawValue: UInt8(status.rawValue >> 30))!
|
||||
return (severity, status.rawValue & 0x20000000 != 0,
|
||||
UInt16((status.rawValue & 0x0FFF0000) >> 16),
|
||||
UInt16(status.rawValue & 0x0000FFFF))
|
||||
}
|
||||
let command: Command
|
||||
let creditRequestResponse: UInt16
|
||||
let flags: Flags
|
||||
var nextCommand: UInt32
|
||||
@@ -54,8 +51,8 @@ struct SMB2 {
|
||||
init(command: Command, status: NTStatus = .SUCCESS, creditCharge: UInt16 = 0, creditRequestResponse: UInt16, flags: Flags = [], nextCommand: UInt32 = 0, messageId: UInt64, treeId: UInt32, sessionId: UInt64, signature: (UInt64, UInt64) = (0, 0)) {
|
||||
self.protocolID = type(of: self).protocolConst
|
||||
self.size = 64
|
||||
self.status = status.rawValue
|
||||
self._command = command.rawValue
|
||||
self.status = status
|
||||
self.command = command
|
||||
self.creditCharge = creditCharge
|
||||
self.creditRequestResponse = creditRequestResponse
|
||||
self.flags = flags
|
||||
@@ -70,8 +67,8 @@ struct SMB2 {
|
||||
init(asyncCommand: Command, status: NTStatus = .SUCCESS, creditCharge: UInt16 = 0, creditRequestResponse: UInt16, flags: Flags = [.ASYNC_COMMAND], nextCommand: UInt32 = 0, messageId: UInt64, asyncId: UInt64, sessionId: UInt64, signature: (UInt64, UInt64) = (0, 0)) {
|
||||
self.protocolID = type(of: self).protocolConst
|
||||
self.size = 64
|
||||
self.status = status.rawValue
|
||||
self._command = asyncCommand.rawValue
|
||||
self.status = status
|
||||
self.command = asyncCommand
|
||||
self.creditCharge = creditCharge
|
||||
self.creditRequestResponse = creditRequestResponse
|
||||
self.flags = flags.union([Flags.ASYNC_COMMAND])
|
||||
@@ -110,27 +107,33 @@ struct SMB2 {
|
||||
static let REPLAY_OPERATION = Flags(rawValue: 0x20000000)
|
||||
}
|
||||
|
||||
enum Command: UInt16 {
|
||||
case NEGOTIATE = 0x0000
|
||||
case SESSION_SETUP = 0x0001
|
||||
case LOGOFF = 0x0002
|
||||
case TREE_CONNECT = 0x0003
|
||||
case TREE_DISCONNECT = 0x0004
|
||||
case CREATE = 0x0005
|
||||
case CLOSE = 0x0006
|
||||
case FLUSH = 0x0007
|
||||
case READ = 0x0008
|
||||
case WRITE = 0x0009
|
||||
case LOCK = 0x000A
|
||||
case IOCTL = 0x000B
|
||||
case CANCEL = 0x000C
|
||||
case ECHO = 0x000D
|
||||
case QUERY_DIRECTORY = 0x000E
|
||||
case CHANGE_NOTIFY = 0x000F
|
||||
case QUERY_INFO = 0x0010
|
||||
case SET_INFO = 0x0011
|
||||
case OPLOCK_BREAK = 0x0012
|
||||
case INVALID = 0xFFFF
|
||||
struct Command: Option {
|
||||
init(rawValue: UInt16) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
let rawValue: UInt16
|
||||
|
||||
public static let NEGOTIATE = Command(rawValue: 0x0000)
|
||||
public static let SESSION_SETUP = Command(rawValue: 0x0001)
|
||||
public static let LOGOFF = Command(rawValue: 0x0002)
|
||||
public static let TREE_CONNECT = Command(rawValue: 0x0003)
|
||||
public static let TREE_DISCONNECT = Command(rawValue: 0x0004)
|
||||
public static let CREATE = Command(rawValue: 0x0005)
|
||||
public static let CLOSE = Command(rawValue: 0x0006)
|
||||
public static let FLUSH = Command(rawValue: 0x0007)
|
||||
public static let READ = Command(rawValue: 0x0008)
|
||||
public static let WRITE = Command(rawValue: 0x0009)
|
||||
public static let LOCK = Command(rawValue: 0x000A)
|
||||
public static let IOCTL = Command(rawValue: 0x000B)
|
||||
public static let CANCEL = Command(rawValue: 0x000C)
|
||||
public static let ECHO = Command(rawValue: 0x000D)
|
||||
public static let QUERY_DIRECTORY = Command(rawValue: 0x000E)
|
||||
public static let CHANGE_NOTIFY = Command(rawValue: 0x000F)
|
||||
public static let QUERY_INFO = Command(rawValue: 0x0010)
|
||||
public static let SET_INFO = Command(rawValue: 0x0011)
|
||||
public static let OPLOCK_BREAK = Command(rawValue: 0x0012)
|
||||
public static let INVALID = Command(rawValue: 0xFFFF)
|
||||
}
|
||||
|
||||
// MARK: SMB2 Oplock Break
|
||||
|
||||
+126
-120
@@ -10,126 +10,132 @@ import Foundation
|
||||
|
||||
/// Error Types and Description
|
||||
|
||||
enum NTStatus: UInt32, Error, CustomStringConvertible {
|
||||
case SUCCESS = 0x00000000
|
||||
case NOT_IMPLEMENTED = 0xC0000002
|
||||
case INVALID_DEVICE_REQUEST = 0xC0000010
|
||||
case ILLEGAL_FUNCTION = 0xC00000AF
|
||||
case NO_SUCH_FILE = 0xC000000F
|
||||
case NO_SUCH_DEVICE = 0xC000000E
|
||||
case OBJECT_NAME_NOT_FOUND = 0xC0000034
|
||||
case OBJECT_PATH_INVALID = 0xC0000039
|
||||
case OBJECT_PATH_NOT_FOUND = 0xC000003A
|
||||
case OBJECT_PATH_SYNTAX_BAD = 0xC000003B
|
||||
case DFS_EXIT_PATH_FOUND = 0xC000009B
|
||||
case REDIRECTOR_NOT_STARTED = 0xC00000FB
|
||||
case TOO_MANY_OPENED_FILES = 0xC000011F
|
||||
case ACCESS_DENIED = 0xC0000022
|
||||
case INVALID_LOCK_SEQUENCE = 0xC000001E
|
||||
case INVALID_VIEW_SIZE = 0xC000001F
|
||||
case ALREADY_COMMITTED = 0xC0000021
|
||||
case PORT_CONNECTION_REFUSED = 0xC0000041
|
||||
case THREAD_IS_TERMINATING = 0xC000004B
|
||||
case DELETE_PENDING = 0xC0000056
|
||||
case PRIVILEGE_NOT_HELD = 0xC0000061
|
||||
case LOGON_FAILURE = 0xC000006D
|
||||
case FILE_IS_A_DIRECTORY = 0xC00000BA
|
||||
case FILE_RENAMED = 0xC00000D5
|
||||
case PROCESS_IS_TERMINATING = 0xC000010A
|
||||
case DIRECTORY_NOT_EMPTY = 0xC0000101
|
||||
case CANNOT_DELETE = 0xC0000121
|
||||
case FILE_NOT_AVAILABLE = 0xC0000467
|
||||
case FILE_DELETED = 0xC0000123
|
||||
case SMB_BAD_FID = 0x00060001
|
||||
case INVALID_HANDLE = 0xC0000008
|
||||
case OBJECT_TYPE_MISMATCH = 0xC0000024
|
||||
case PORT_DISCONNECTED = 0xC0000037
|
||||
case INVALID_PORT_HANDLE = 0xC0000042
|
||||
case FILE_CLOSED = 0xC0000128
|
||||
case HANDLE_NOT_CLOSABLE = 0xC0000235
|
||||
case SECTION_TOO_BIG = 0xC0000040
|
||||
case TOO_MANY_PAGING_FILES = 0xC0000097
|
||||
case INSUFF_SERVER_RESOURCES = 0xC0000205
|
||||
case OS2_INVALID_ACCESS = 0x000C0001
|
||||
case ACCESS_DENIED_2 = 0xC00000CA
|
||||
case DATA_ERROR = 0xC000009C
|
||||
case NOT_SAME_DEVICE = 0xC00000D4
|
||||
case NO_MORE_FILES = 0x80000006
|
||||
case NO_MORE_ENTRIES = 0x8000001A
|
||||
case UNSUCCESSFUL = 0xC0000001
|
||||
case SHARING_VIOLATION = 0xC0000043
|
||||
case FILE_LOCK_CONFLICT = 0xC0000054
|
||||
case LOCK_NOT_GRANTED = 0xC0000055
|
||||
case END_OF_FILE = 0xC0000011
|
||||
case NOT_SUPPORTED = 0xC00000BB
|
||||
case OBJECT_NAME_COLLISION = 0xC0000035
|
||||
case INVALID_PARAMETER = 0xC000000D
|
||||
case OS2_INVALID_LEVEL = 0x007C0001
|
||||
case OS2_NEGATIVE_SEEK = 0x00830001
|
||||
case RANGE_NOT_LOCKED = 0xC000007E
|
||||
case OS2_NO_MORE_SIDS = 0x00710001
|
||||
case OS2_CANCEL_VIOLATION = 0x00AD0001
|
||||
case OS2_ATOMIC_LOCKS_NOT_SUPPORTED = 0x00AE0001
|
||||
case INVALID_INFO_CLASS = 0xC0000003
|
||||
case INVALID_PIPE_STATE = 0xC00000AD
|
||||
case INVALID_READ_MODE = 0xC00000B4
|
||||
case OS2_CANNOT_COPY = 0x010A0001
|
||||
case STOPPED_ON_SYMLINK = 0x8000002D
|
||||
case INSTANCE_NOT_AVAILABLE = 0xC00000AB
|
||||
case PIPE_NOT_AVAILABLE = 0xC00000AC
|
||||
case PIPE_BUSY = 0xC00000AE
|
||||
case PIPE_CLOSING = 0xC00000B1
|
||||
case PIPE_EMPTY = 0xC00000D9
|
||||
case PIPE_DISCONNECTED = 0xC00000B0
|
||||
case BUFFER_OVERFLOW = 0x80000005
|
||||
case MORE_PROCESSING_REQUIRED = 0xC0000016
|
||||
case EA_TOO_LARGE = 0xC0000050
|
||||
case OS2_EAS_DIDNT_FIT = 0x01130001
|
||||
case EAS_NOT_SUPPORTED = 0xC000004F
|
||||
case EA_LIST_INCONSISTENT = 0x80000014
|
||||
case OS2_EA_ACCESS_DENIED = 0x03E20001
|
||||
case NOTIFY_ENUM_DIR = 0x0000010C
|
||||
case INVALID_SMB = 0x00010002
|
||||
case WRONG_PASSWORD = 0xC000006A
|
||||
case PATH_NOT_COVERED = 0xC0000257
|
||||
case NETWORK_NAME_DELETED = 0xC00000C9
|
||||
case SMB_BAD_TID = 0x00050002
|
||||
case BAD_NETWORK_NAME = 0xC00000CC
|
||||
case BAD_DEVICE_TYPE = 0xC00000CB
|
||||
case SMB_BAD_COMMAND = 0x00160002
|
||||
case PRINT_QUEUE_FULL = 0xC00000C6
|
||||
case NO_SPOOL_SPACE = 0xC00000C7
|
||||
case PRINT_CANCELLED = 0xC00000C8
|
||||
case UNEXPECTED_NETWORK_ERROR = 0xC00000C4
|
||||
case IO_TIMEOUT = 0xC00000B5
|
||||
case REQUEST_NOT_ACCEPTED = 0xC00000D0
|
||||
case TOO_MANY_SESSIONS = 0xC00000CE
|
||||
case SMB_BAD_UID = 0x005B0002
|
||||
case SMB_USE_MPX = 0x00FA0002
|
||||
case SMB_USE_STANDARD = 0x00FB0002
|
||||
case SMB_CONTINUE_MPX = 0x00FC0002
|
||||
case ACCOUNT_DISABLED = 0xC0000072
|
||||
case ACCOUNT_EXPIRED = 0xC0000193
|
||||
case INVALID_WORKSTATION = 0xC0000070
|
||||
case INVALID_LOGON_HOURS = 0xC000006F
|
||||
case PASSWORD_EXPIRED = 0xC0000071
|
||||
case PASSWORD_MUST_CHANGE = 0xC0000224
|
||||
case SMB_NO_SUPPORT = 0xFFFF0002
|
||||
case MEDIA_WRITE_PROTECTED = 0xC00000A2
|
||||
case NO_MEDIA_IN_DEVICE = 0xC0000013
|
||||
case INVALID_DEVICE_STATE = 0xC0000184
|
||||
case DATA_ERROR_2 = 0xC000003E
|
||||
case CRC_ERROR = 0xC000003F
|
||||
case DISK_CORRUPT_ERROR = 0xC0000032
|
||||
case NONEXISTENT_SECTOR = 0xC0000015
|
||||
case DEVICE_PAPER_EMPTY = 0x8000000E
|
||||
case WRONG_VOLUME = 0xC0000012
|
||||
case DISK_FULL = 0xC000007F
|
||||
case BUFFER_TOO_SMALL = 0xC0000023
|
||||
case BAD_IMPERSONATION_LEVEL = 0xC00000A5
|
||||
case USER_SESSION_DELETED = 0xC0000203
|
||||
case NETWORK_SESSION_EXPIRED = 0xC000035C
|
||||
case SMB_TOO_MANY_UIDS = 0xC000205A
|
||||
struct NTStatus: Option, Error, CustomStringConvertible {
|
||||
init(rawValue: UInt32) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
var rawValue: UInt32
|
||||
|
||||
public static let SUCCESS = NTStatus(rawValue: 0x00000000)
|
||||
public static let NOT_IMPLEMENTED = NTStatus(rawValue: 0xC0000002)
|
||||
public static let INVALID_DEVICE_REQUEST = NTStatus(rawValue: 0xC0000010)
|
||||
public static let ILLEGAL_FUNCTION = NTStatus(rawValue: 0xC00000AF)
|
||||
public static let NO_SUCH_FILE = NTStatus(rawValue: 0xC000000F)
|
||||
public static let NO_SUCH_DEVICE = NTStatus(rawValue: 0xC000000E)
|
||||
public static let OBJECT_NAME_NOT_FOUND = NTStatus(rawValue: 0xC0000034)
|
||||
public static let OBJECT_PATH_INVALID = NTStatus(rawValue: 0xC0000039)
|
||||
public static let OBJECT_PATH_NOT_FOUND = NTStatus(rawValue: 0xC000003A)
|
||||
public static let OBJECT_PATH_SYNTAX_BAD = NTStatus(rawValue: 0xC000003B)
|
||||
public static let DFS_EXIT_PATH_FOUND = NTStatus(rawValue: 0xC000009B)
|
||||
public static let REDIRECTOR_NOT_STARTED = NTStatus(rawValue: 0xC00000FB)
|
||||
public static let TOO_MANY_OPENED_FILES = NTStatus(rawValue: 0xC000011F)
|
||||
public static let ACCESS_DENIED = NTStatus(rawValue: 0xC0000022)
|
||||
public static let INVALID_LOCK_SEQUENCE = NTStatus(rawValue: 0xC000001E)
|
||||
public static let INVALID_VIEW_SIZE = NTStatus(rawValue: 0xC000001F)
|
||||
public static let ALREADY_COMMITTED = NTStatus(rawValue: 0xC0000021)
|
||||
public static let PORT_CONNECTION_REFUSED = NTStatus(rawValue: 0xC0000041)
|
||||
public static let THREAD_IS_TERMINATING = NTStatus(rawValue: 0xC000004B)
|
||||
public static let DELETE_PENDING = NTStatus(rawValue: 0xC0000056)
|
||||
public static let PRIVILEGE_NOT_HELD = NTStatus(rawValue: 0xC0000061)
|
||||
public static let LOGON_FAILURE = NTStatus(rawValue: 0xC000006D)
|
||||
public static let FILE_IS_A_DIRECTORY = NTStatus(rawValue: 0xC00000BA)
|
||||
public static let FILE_RENAMED = NTStatus(rawValue: 0xC00000D5)
|
||||
public static let PROCESS_IS_TERMINATING = NTStatus(rawValue: 0xC000010A)
|
||||
public static let DIRECTORY_NOT_EMPTY = NTStatus(rawValue: 0xC0000101)
|
||||
public static let CANNOT_DELETE = NTStatus(rawValue: 0xC0000121)
|
||||
public static let FILE_NOT_AVAILABLE = NTStatus(rawValue: 0xC0000467)
|
||||
public static let FILE_DELETED = NTStatus(rawValue: 0xC0000123)
|
||||
public static let SMB_BAD_FID = NTStatus(rawValue: 0x00060001)
|
||||
public static let INVALID_HANDLE = NTStatus(rawValue: 0xC0000008)
|
||||
public static let OBJECT_TYPE_MISMATCH = NTStatus(rawValue: 0xC0000024)
|
||||
public static let PORT_DISCONNECTED = NTStatus(rawValue: 0xC0000037)
|
||||
public static let INVALID_PORT_HANDLE = NTStatus(rawValue: 0xC0000042)
|
||||
public static let FILE_CLOSED = NTStatus(rawValue: 0xC0000128)
|
||||
public static let HANDLE_NOT_CLOSABLE = NTStatus(rawValue: 0xC0000235)
|
||||
public static let SECTION_TOO_BIG = NTStatus(rawValue: 0xC0000040)
|
||||
public static let TOO_MANY_PAGING_FILES = NTStatus(rawValue: 0xC0000097)
|
||||
public static let INSUFF_SERVER_RESOURCES = NTStatus(rawValue: 0xC0000205)
|
||||
public static let OS2_INVALID_ACCESS = NTStatus(rawValue: 0x000C0001)
|
||||
public static let ACCESS_DENIED_2 = NTStatus(rawValue: 0xC00000CA)
|
||||
public static let DATA_ERROR = NTStatus(rawValue: 0xC000009C)
|
||||
public static let NOT_SAME_DEVICE = NTStatus(rawValue: 0xC00000D4)
|
||||
public static let NO_MORE_FILES = NTStatus(rawValue: 0x80000006)
|
||||
public static let NO_MORE_ENTRIES = NTStatus(rawValue: 0x8000001A)
|
||||
public static let UNSUCCESSFUL = NTStatus(rawValue: 0xC0000001)
|
||||
public static let SHARING_VIOLATION = NTStatus(rawValue: 0xC0000043)
|
||||
public static let FILE_LOCK_CONFLICT = NTStatus(rawValue: 0xC0000054)
|
||||
public static let LOCK_NOT_GRANTED = NTStatus(rawValue: 0xC0000055)
|
||||
public static let END_OF_FILE = NTStatus(rawValue: 0xC0000011)
|
||||
public static let NOT_SUPPORTED = NTStatus(rawValue: 0xC00000BB)
|
||||
public static let OBJECT_NAME_COLLISION = NTStatus(rawValue: 0xC0000035)
|
||||
public static let INVALID_PARAMETER = NTStatus(rawValue: 0xC000000D)
|
||||
public static let OS2_INVALID_LEVEL = NTStatus(rawValue: 0x007C0001)
|
||||
public static let OS2_NEGATIVE_SEEK = NTStatus(rawValue: 0x00830001)
|
||||
public static let RANGE_NOT_LOCKED = NTStatus(rawValue: 0xC000007E)
|
||||
public static let OS2_NO_MORE_SIDS = NTStatus(rawValue: 0x00710001)
|
||||
public static let OS2_CANCEL_VIOLATION = NTStatus(rawValue: 0x00AD0001)
|
||||
public static let OS2_ATOMIC_LOCKS_NOT_SUPPORTED = NTStatus(rawValue: 0x00AE0001)
|
||||
public static let INVALID_INFO_CLASS = NTStatus(rawValue: 0xC0000003)
|
||||
public static let INVALID_PIPE_STATE = NTStatus(rawValue: 0xC00000AD)
|
||||
public static let INVALID_READ_MODE = NTStatus(rawValue: 0xC00000B4)
|
||||
public static let OS2_CANNOT_COPY = NTStatus(rawValue: 0x010A0001)
|
||||
public static let STOPPED_ON_SYMLINK = NTStatus(rawValue: 0x8000002D)
|
||||
public static let INSTANCE_NOT_AVAILABLE = NTStatus(rawValue: 0xC00000AB)
|
||||
public static let PIPE_NOT_AVAILABLE = NTStatus(rawValue: 0xC00000AC)
|
||||
public static let PIPE_BUSY = NTStatus(rawValue: 0xC00000AE)
|
||||
public static let PIPE_CLOSING = NTStatus(rawValue: 0xC00000B1)
|
||||
public static let PIPE_EMPTY = NTStatus(rawValue: 0xC00000D9)
|
||||
public static let PIPE_DISCONNECTED = NTStatus(rawValue: 0xC00000B0)
|
||||
public static let BUFFER_OVERFLOW = NTStatus(rawValue: 0x80000005)
|
||||
public static let MORE_PROCESSING_REQUIRED = NTStatus(rawValue: 0xC0000016)
|
||||
public static let EA_TOO_LARGE = NTStatus(rawValue: 0xC0000050)
|
||||
public static let OS2_EAS_DIDNT_FIT = NTStatus(rawValue: 0x01130001)
|
||||
public static let EAS_NOT_SUPPORTED = NTStatus(rawValue: 0xC000004F)
|
||||
public static let EA_LIST_INCONSISTENT = NTStatus(rawValue: 0x80000014)
|
||||
public static let OS2_EA_ACCESS_DENIED = NTStatus(rawValue: 0x03E20001)
|
||||
public static let NOTIFY_ENUM_DIR = NTStatus(rawValue: 0x0000010C)
|
||||
public static let INVALID_SMB = NTStatus(rawValue: 0x00010002)
|
||||
public static let WRONG_PASSWORD = NTStatus(rawValue: 0xC000006A)
|
||||
public static let PATH_NOT_COVERED = NTStatus(rawValue: 0xC0000257)
|
||||
public static let NETWORK_NAME_DELETED = NTStatus(rawValue: 0xC00000C9)
|
||||
public static let SMB_BAD_TID = NTStatus(rawValue: 0x00050002)
|
||||
public static let BAD_NETWORK_NAME = NTStatus(rawValue: 0xC00000CC)
|
||||
public static let BAD_DEVICE_TYPE = NTStatus(rawValue: 0xC00000CB)
|
||||
public static let SMB_BAD_COMMAND = NTStatus(rawValue: 0x00160002)
|
||||
public static let PRINT_QUEUE_FULL = NTStatus(rawValue: 0xC00000C6)
|
||||
public static let NO_SPOOL_SPACE = NTStatus(rawValue: 0xC00000C7)
|
||||
public static let PRINT_CANCELLED = NTStatus(rawValue: 0xC00000C8)
|
||||
public static let UNEXPECTED_NETWORK_ERROR = NTStatus(rawValue: 0xC00000C4)
|
||||
public static let IO_TIMEOUT = NTStatus(rawValue: 0xC00000B5)
|
||||
public static let REQUEST_NOT_ACCEPTED = NTStatus(rawValue: 0xC00000D0)
|
||||
public static let TOO_MANY_SESSIONS = NTStatus(rawValue: 0xC00000CE)
|
||||
public static let SMB_BAD_UID = NTStatus(rawValue: 0x005B0002)
|
||||
public static let SMB_USE_MPX = NTStatus(rawValue: 0x00FA0002)
|
||||
public static let SMB_USE_STANDARD = NTStatus(rawValue: 0x00FB0002)
|
||||
public static let SMB_CONTINUE_MPX = NTStatus(rawValue: 0x00FC0002)
|
||||
public static let ACCOUNT_DISABLED = NTStatus(rawValue: 0xC0000072)
|
||||
public static let ACCOUNT_EXPIRED = NTStatus(rawValue: 0xC0000193)
|
||||
public static let INVALID_WORKSTATION = NTStatus(rawValue: 0xC0000070)
|
||||
public static let INVALID_LOGON_HOURS = NTStatus(rawValue: 0xC000006F)
|
||||
public static let PASSWORD_EXPIRED = NTStatus(rawValue: 0xC0000071)
|
||||
public static let PASSWORD_MUST_CHANGE = NTStatus(rawValue: 0xC0000224)
|
||||
public static let SMB_NO_SUPPORT = NTStatus(rawValue: 0xFFFF0002)
|
||||
public static let MEDIA_WRITE_PROTECTED = NTStatus(rawValue: 0xC00000A2)
|
||||
public static let NO_MEDIA_IN_DEVICE = NTStatus(rawValue: 0xC0000013)
|
||||
public static let INVALID_DEVICE_STATE = NTStatus(rawValue: 0xC0000184)
|
||||
public static let DATA_ERROR_2 = NTStatus(rawValue: 0xC000003E)
|
||||
public static let CRC_ERROR = NTStatus(rawValue: 0xC000003F)
|
||||
public static let DISK_CORRUPT_ERROR = NTStatus(rawValue: 0xC0000032)
|
||||
public static let NONEXISTENT_SECTOR = NTStatus(rawValue: 0xC0000015)
|
||||
public static let DEVICE_PAPER_EMPTY = NTStatus(rawValue: 0x8000000E)
|
||||
public static let WRONG_VOLUME = NTStatus(rawValue: 0xC0000012)
|
||||
public static let DISK_FULL = NTStatus(rawValue: 0xC000007F)
|
||||
public static let BUFFER_TOO_SMALL = NTStatus(rawValue: 0xC0000023)
|
||||
public static let BAD_IMPERSONATION_LEVEL = NTStatus(rawValue: 0xC00000A5)
|
||||
public static let USER_SESSION_DELETED = NTStatus(rawValue: 0xC0000203)
|
||||
public static let NETWORK_SESSION_EXPIRED = NTStatus(rawValue: 0xC000035C)
|
||||
public static let SMB_TOO_MANY_UIDS = NTStatus(rawValue: 0xC000205A)
|
||||
|
||||
public var description: String {
|
||||
switch self {
|
||||
|
||||
+340
-386
@@ -7,54 +7,27 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
#if os(macOS) || os(iOS) || os(tvOS)
|
||||
import CoreGraphics
|
||||
#endif
|
||||
|
||||
/**
|
||||
Allows accessing to WebDAV server files. This provider doesn't cache or save files internally, however you can
|
||||
set `useCache` and `cache` properties to use Foundation `NSURLCache` system.
|
||||
|
||||
WebDAV system supported by many cloud services including [Box.net](https://www.box.com/home)
|
||||
WebDAV system supported by many cloud services including [Box.com](https://www.box.com/home)
|
||||
and [Yandex disk](https://disk.yandex.com) and [ownCloud](https://owncloud.org).
|
||||
|
||||
- Important: Because this class uses `URLSession`, it's necessary to disable App Transport Security
|
||||
in case of using this class with unencrypted HTTP connection.
|
||||
[Read this to know how](http://iosdevtips.co/post/121756573323/ios-9-xcode-7-http-connect-server-error).
|
||||
*/
|
||||
open class WebDAVFileProvider: FileProviderBasicRemote {
|
||||
open class var type: String { return "WebDAV" }
|
||||
open let baseURL: URL?
|
||||
open var currentPath: String
|
||||
open class WebDAVFileProvider: HTTPFileProvider, FileProviderSharing {
|
||||
override open class var type: String { return "WebDAV" }
|
||||
|
||||
open var dispatch_queue: DispatchQueue
|
||||
open var operation_queue: OperationQueue {
|
||||
willSet {
|
||||
assert(_session == nil, "It's not effective to change dispatch_queue property after session is initialized.")
|
||||
}
|
||||
}
|
||||
|
||||
public weak var delegate: FileProviderDelegate?
|
||||
open var credential: URLCredential? {
|
||||
didSet {
|
||||
sessionDelegate?.credential = credential
|
||||
}
|
||||
}
|
||||
open private(set) var cache: URLCache?
|
||||
public var useCache: Bool
|
||||
public var validatingCache: Bool
|
||||
|
||||
fileprivate var _session: URLSession?
|
||||
fileprivate var sessionDelegate: SessionDelegate?
|
||||
public var session: URLSession {
|
||||
if _session == nil {
|
||||
self.sessionDelegate = SessionDelegate(fileProvider: self, credential: credential)
|
||||
let queue = OperationQueue()
|
||||
//queue.underlyingQueue = dispatch_queue
|
||||
let config = URLSessionConfiguration.default
|
||||
config.urlCache = cache
|
||||
config.requestCachePolicy = .returnCacheDataElseLoad
|
||||
_session = URLSession(configuration: config, delegate: sessionDelegate as URLSessionDownloadDelegate?, delegateQueue: queue)
|
||||
}
|
||||
return _session!
|
||||
}
|
||||
/// An enum which defines HTTP Authentication method, usually you should it default `.digest`.
|
||||
/// If the server uses OAuth authentication, credential must be set with token as `password`, like Dropbox.
|
||||
public var credentialType: URLRequest.AuthenticationType = .digest
|
||||
|
||||
/**
|
||||
Initializes WebDAV provider.
|
||||
@@ -68,43 +41,22 @@ open class WebDAVFileProvider: FileProviderBasicRemote {
|
||||
if !["http", "https"].contains(baseURL.uw_scheme.lowercased()) {
|
||||
return nil
|
||||
}
|
||||
self.baseURL = (baseURL.path.hasSuffix("/") ? baseURL : baseURL.appendingPathComponent("")).absoluteURL
|
||||
self.currentPath = ""
|
||||
self.useCache = false
|
||||
self.validatingCache = true
|
||||
self.cache = cache
|
||||
self.credential = credential
|
||||
dispatch_queue = DispatchQueue(label: "FileProvider.\(type(of: self).type)", attributes: .concurrent)
|
||||
operation_queue = OperationQueue()
|
||||
operation_queue.name = "FileProvider.\(type(of: self).type).Operation"
|
||||
let refinedBaseURL = (baseURL.absoluteString.hasSuffix("/") ? baseURL : baseURL.appendingPathComponent(""))
|
||||
super.init(baseURL: refinedBaseURL.absoluteURL, credential: credential, cache: cache)
|
||||
}
|
||||
|
||||
public required convenience init?(coder aDecoder: NSCoder) {
|
||||
guard let baseURL = aDecoder.decodeObject(forKey: "baseURL") as? URL else {
|
||||
guard let baseURL = aDecoder.decodeObject(of: NSURL.self, forKey: "baseURL") as URL? else {
|
||||
return nil
|
||||
}
|
||||
self.init(baseURL: baseURL,
|
||||
credential: aDecoder.decodeObject(forKey: "credential") as? URLCredential)
|
||||
self.currentPath = aDecoder.decodeObject(forKey: "currentPath") as? String ?? ""
|
||||
credential: aDecoder.decodeObject(of: URLCredential.self, forKey: "credential"))
|
||||
self.useCache = aDecoder.decodeBool(forKey: "useCache")
|
||||
self.validatingCache = aDecoder.decodeBool(forKey: "validatingCache")
|
||||
}
|
||||
|
||||
open func encode(with aCoder: NSCoder) {
|
||||
aCoder.encode(self.baseURL, forKey: "baseURL")
|
||||
aCoder.encode(self.credential, forKey: "credential")
|
||||
aCoder.encode(self.currentPath, forKey: "currentPath")
|
||||
aCoder.encode(self.useCache, forKey: "isCoorinating")
|
||||
aCoder.encode(self.validatingCache, forKey: "undoManager")
|
||||
}
|
||||
|
||||
public static var supportsSecureCoding: Bool {
|
||||
return true
|
||||
}
|
||||
|
||||
open func copy(with zone: NSZone? = nil) -> Any {
|
||||
override open func copy(with zone: NSZone? = nil) -> Any {
|
||||
let copy = WebDAVFileProvider(baseURL: self.baseURL!, credential: self.credential, cache: self.cache)!
|
||||
copy.currentPath = self.currentPath
|
||||
copy.delegate = self.delegate
|
||||
copy.fileOperationDelegate = self.fileOperationDelegate
|
||||
copy.useCache = self.useCache
|
||||
@@ -112,16 +64,20 @@ open class WebDAVFileProvider: FileProviderBasicRemote {
|
||||
return copy
|
||||
}
|
||||
|
||||
deinit {
|
||||
if fileProviderCancelTasksOnInvalidating {
|
||||
_session?.invalidateAndCancel()
|
||||
} else {
|
||||
_session?.finishTasksAndInvalidate()
|
||||
}
|
||||
}
|
||||
|
||||
public func contentsOfDirectory(path: String, completionHandler: @escaping (([FileObject], Error?) -> Void)) {
|
||||
self.contentsOfDirectory(path: path, including: [], completionHandler: completionHandler)
|
||||
/**
|
||||
Returns an Array of `FileObject`s identifying the the directory entries via asynchronous completion handler.
|
||||
|
||||
If the directory contains no entries or an error is occured, this method will return the empty array.
|
||||
|
||||
- Parameters:
|
||||
- path: path to target directory. If empty, root will be iterated.
|
||||
- completionHandler: a closure with result of directory entries or error.
|
||||
- contents: An array of `FileObject` identifying the the directory entries.
|
||||
- error: Error returned by system.
|
||||
*/
|
||||
override open func contentsOfDirectory(path: String, completionHandler: @escaping (([FileObject], Error?) -> Void)) {
|
||||
let query = NSPredicate(format: "TRUEPREDICATE")
|
||||
_ = searchFiles(path: path, recursive: false, query: query, including: [], foundItemHandler: nil, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -129,41 +85,18 @@ open class WebDAVFileProvider: FileProviderBasicRemote {
|
||||
|
||||
If the directory contains no entries or an error is occured, this method will return the empty array.
|
||||
|
||||
- Parameter path: path to target directory. If empty, `currentPath` value will be used.
|
||||
- Parameter path: path to target directory. If empty, root will be iterated.
|
||||
- Parameter including: An array which determines which file properties should be considered to fetch.
|
||||
- Parameter completionHandler: a closure with result of directory entries or error.
|
||||
- `contents`: An array of `FileObject` identifying the the directory entries.
|
||||
- `error`: Error returned by system.
|
||||
- Parameter contents: An array of `FileObject` identifying the the directory entries.
|
||||
- Parameter error: Error returned by system.
|
||||
*/
|
||||
open func contentsOfDirectory(path: String, including: [URLResourceKey], completionHandler: @escaping ((_ contents: [FileObject], _ error: Error?) -> Void)) {
|
||||
let opType = FileOperationType.fetch(path: path)
|
||||
let url = self.url(of: path).appendingPathComponent("")
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "PROPFIND"
|
||||
request.setValue("1", forHTTPHeaderField: "Depth")
|
||||
request.setValue("text/xml; charset=\"utf-8\"", forHTTPHeaderField: "Content-Type")
|
||||
request.httpBody = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n\(WebDavFileObject.propString(including))\n</D:propfind>".data(using: .utf8)
|
||||
request.setValue(String(request.httpBody!.count), forHTTPHeaderField: "Content-Length")
|
||||
runDataTask(with: request, operationHandle: RemoteOperationHandle(operationType: opType, tasks: []), completionHandler: { (data, response, error) in
|
||||
var responseError: FileProviderWebDavError?
|
||||
if let code = (response as? HTTPURLResponse)?.statusCode , code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) {
|
||||
responseError = FileProviderWebDavError(code: rCode, path: path, errorDescription: String(data: data ?? Data(), encoding: .utf8), url: url)
|
||||
}
|
||||
var fileObjects = [WebDavFileObject]()
|
||||
if let data = data {
|
||||
let xresponse = DavResponse.parse(xmlResponse: data, baseURL: self.baseURL)
|
||||
for attr in xresponse where attr.href != url {
|
||||
if attr.href.path == url.path {
|
||||
continue
|
||||
}
|
||||
fileObjects.append(WebDavFileObject(attr))
|
||||
}
|
||||
}
|
||||
completionHandler(fileObjects, responseError ?? error)
|
||||
})
|
||||
open func contentsOfDirectory(path: String, including: [URLResourceKey], completionHandler: @escaping (_ contents: [FileObject], _ error: Error?) -> Void) {
|
||||
let query = NSPredicate(format: "TRUEPREDICATE")
|
||||
_ = searchFiles(path: path, recursive: false, query: query, including: including, foundItemHandler: nil, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
open func attributesOfItem(path: String, completionHandler: @escaping ((_ attributes: FileObject?, _ error: Error?) -> Void)) {
|
||||
override open func attributesOfItem(path: String, completionHandler: @escaping (_ attributes: FileObject?, _ error: Error?) -> Void) {
|
||||
self.attributesOfItem(path: path, including: [], completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
@@ -172,24 +105,24 @@ open class WebDAVFileProvider: FileProviderBasicRemote {
|
||||
|
||||
If the directory contains no entries or an error is occured, this method will return the empty `FileObject`.
|
||||
|
||||
- Parameter path: path to target directory. If empty, `currentPath` value will be used.
|
||||
- Parameter including: An array which determines which file properties should be considered to fetch.
|
||||
- Parameter completionHandler: a closure with result of directory entries or error.
|
||||
- `attributes`: A `FileObject` containing the attributes of the item.
|
||||
- `error`: Error returned by system.
|
||||
- Parameters:
|
||||
- path: path to target directory. If empty, attributes of root will be returned.
|
||||
- completionHandler: a closure with result of directory entries or error.
|
||||
- attributes: A `FileObject` containing the attributes of the item.
|
||||
- error: Error returned by system.
|
||||
*/
|
||||
open func attributesOfItem(path: String, including: [URLResourceKey], completionHandler: @escaping ((_ attributes: FileObject?, _ error: Error?) -> Void)) {
|
||||
open func attributesOfItem(path: String, including: [URLResourceKey], completionHandler: @escaping (_ attributes: FileObject?, _ error: Error?) -> Void) {
|
||||
let url = self.url(of: path)
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "PROPFIND"
|
||||
request.setValue("1", forHTTPHeaderField: "Depth")
|
||||
request.setValue("text/xml; charset=\"utf-8\"", forHTTPHeaderField: "Content-Type")
|
||||
request.httpBody = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n\(WebDavFileObject.propString(including))\n</D:propfind>".data(using: .utf8)
|
||||
request.setValue(String(request.httpBody!.count), forHTTPHeaderField: "Content-Length")
|
||||
request.setValue("0", forHTTPHeaderField: "Depth")
|
||||
request.setValue(authentication: credential, with: credentialType)
|
||||
request.setValue(contentType: .xml, charset: .utf8)
|
||||
request.httpBody = WebDavFileObject.xmlProp(including)
|
||||
runDataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var responseError: FileProviderWebDavError?
|
||||
var responseError: FileProviderHTTPError?
|
||||
if let code = (response as? HTTPURLResponse)?.statusCode, code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) {
|
||||
responseError = FileProviderWebDavError(code: rCode, path: path, errorDescription: String(data: data ?? Data(), encoding: .utf8), url: url)
|
||||
responseError = self.serverError(with: rCode, path: path, data: data)
|
||||
}
|
||||
if let data = data {
|
||||
let xresponse = DavResponse.parse(xmlResponse: data, baseURL: self.baseURL)
|
||||
@@ -202,7 +135,9 @@ open class WebDAVFileProvider: FileProviderBasicRemote {
|
||||
})
|
||||
}
|
||||
|
||||
open func storageProperties(completionHandler: @escaping ((_ total: Int64, _ used: Int64) -> Void)) {
|
||||
/// Returns volume/provider information asynchronously.
|
||||
/// - Parameter volumeInfo: Information of filesystem/Provider returned by system/server.
|
||||
override open func storageProperties(completionHandler: @escaping (_ volumeInfo: VolumeObject?) -> Void) {
|
||||
// Not all WebDAV clients implements RFC2518 which allows geting storage quota.
|
||||
// In this case you won't get error. totalSize is NSURLSessionTransferSizeUnknown
|
||||
// and used space is zero.
|
||||
@@ -212,280 +147,239 @@ open class WebDAVFileProvider: FileProviderBasicRemote {
|
||||
var request = URLRequest(url: baseURL)
|
||||
request.httpMethod = "PROPFIND"
|
||||
request.setValue("0", forHTTPHeaderField: "Depth")
|
||||
request.setValue("text/xml; charset=\"utf-8\"", forHTTPHeaderField: "Content-Type")
|
||||
request.httpBody = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:prop><D:quota-available-bytes/><D:quota-used-bytes/></D:prop>\n</D:propfind>".data(using: .utf8)
|
||||
request.setValue(String(request.httpBody!.count), forHTTPHeaderField: "Content-Length")
|
||||
request.setValue(authentication: credential, with: credentialType)
|
||||
request.setValue(contentType: .xml, charset: .utf8)
|
||||
request.httpBody = WebDavFileObject.xmlProp([.volumeTotalCapacityKey, .volumeAvailableCapacityKey, .creationDateKey])
|
||||
runDataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var totalSize: Int64 = -1
|
||||
var usedSize: Int64 = 0
|
||||
if let data = data {
|
||||
let xresponse = DavResponse.parse(xmlResponse: data, baseURL: self.baseURL)
|
||||
if let attr = xresponse.first {
|
||||
totalSize = Int64(attr.prop["quota-available-bytes"] ?? "") ?? -1
|
||||
usedSize = Int64(attr.prop["quota-used-bytes"] ?? "") ?? 0
|
||||
}
|
||||
guard let data = data, let attr = DavResponse.parse(xmlResponse: data, baseURL: self.baseURL).first else {
|
||||
completionHandler(nil)
|
||||
return
|
||||
}
|
||||
completionHandler(totalSize, usedSize)
|
||||
|
||||
let volume = VolumeObject(allValues: [:])
|
||||
volume.creationDate = attr.prop["creationdate"].flatMap { Date(rfcString: $0) }
|
||||
volume.availableCapacity = attr.prop["quota-available-bytes"].flatMap({ Int64($0) }) ?? 0
|
||||
if let usage = attr.prop["quota-used-bytes"].flatMap({ Int64($0) }) {
|
||||
volume.totalCapacity = volume.availableCapacity + usage
|
||||
}
|
||||
completionHandler(volume)
|
||||
})
|
||||
}
|
||||
|
||||
open func searchFiles(path: String, recursive: Bool, query: NSPredicate, foundItemHandler: ((FileObject) -> Void)?, completionHandler: @escaping ((_ files: [FileObject], _ error: Error?) -> Void)) {
|
||||
/**
|
||||
Search files inside directory using query asynchronously.
|
||||
|
||||
Sample predicates:
|
||||
```
|
||||
NSPredicate(format: "(name CONTAINS[c] 'hello') && (fileSize >= 10000)")
|
||||
NSPredicate(format: "(modifiedDate >= %@)", Date())
|
||||
NSPredicate(format: "(path BEGINSWITH %@)", "folder/child folder")
|
||||
```
|
||||
|
||||
- Note: Don't pass Spotlight predicates to this method directly, use `FileProvider.convertSpotlightPredicateTo()` method to get usable predicate.
|
||||
|
||||
- Important: A file name criteria should be provided for Dropbox.
|
||||
|
||||
- Parameters:
|
||||
- path: location of directory to start search
|
||||
- recursive: Searching subdirectories of path
|
||||
- query: An `NSPredicate` object with keys like `FileObject` members, except `size` which becomes `filesize`.
|
||||
- foundItemHandler: Closure which is called when a file is found
|
||||
- completionHandler: Closure which will be called after finishing search. Returns an arry of `FileObject` or error if occured.
|
||||
- files: all files meat the `query` criteria.
|
||||
- error: `Error` returned by server if occured.
|
||||
- Returns: An `Progress` to get progress or cancel progress. Use `completedUnitCount` to iterate count of found items.
|
||||
*/
|
||||
@discardableResult
|
||||
open override func searchFiles(path: String, recursive: Bool, query: NSPredicate, foundItemHandler: ((FileObject) -> Void)?, completionHandler: @escaping ([FileObject], Error?) -> Void) -> Progress? {
|
||||
return searchFiles(path: path, recursive: recursive, query: query, including: [], foundItemHandler: foundItemHandler, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
/**
|
||||
Search files inside directory using query asynchronously.
|
||||
|
||||
Sample predicates:
|
||||
```
|
||||
NSPredicate(format: "(name CONTAINS[c] 'hello') && (fileSize >= 10000)")
|
||||
NSPredicate(format: "(modifiedDate >= %@)", Date())
|
||||
NSPredicate(format: "(path BEGINSWITH %@)", "folder/child folder")
|
||||
```
|
||||
|
||||
- Note: Don't pass Spotlight predicates to this method directly, use `FileProvider.convertSpotlightPredicateTo()` method to get usable predicate.
|
||||
|
||||
- Important: A file name criteria should be provided for Dropbox.
|
||||
|
||||
- Parameters:
|
||||
- path: location of directory to start search
|
||||
- recursive: Searching subdirectories of path
|
||||
- query: An `NSPredicate` object with keys like `FileObject` members, except `size` which becomes `filesize`.
|
||||
- including: An array which determines which file properties should be considered to fetch.
|
||||
- foundItemHandler: Closure which is called when a file is found
|
||||
- completionHandler: Closure which will be called after finishing search. Returns an arry of `FileObject` or error if occured.
|
||||
- files: all files meat the `query` criteria.
|
||||
- error: `Error` returned by server if occured.
|
||||
- Returns: An `Progress` to get progress or cancel progress. Use `completedUnitCount` to iterate count of found items.
|
||||
*/
|
||||
@discardableResult
|
||||
open func searchFiles(path: String, recursive: Bool, query: NSPredicate, including: [URLResourceKey], foundItemHandler: ((FileObject) -> Void)?, completionHandler: @escaping (_ files: [FileObject], _ error: Error?) -> Void) -> Progress? {
|
||||
let url = self.url(of: path)
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "PROPFIND"
|
||||
//request.setValue("1", forHTTPHeaderField: "Depth")
|
||||
request.setValue("text/xml; charset=\"utf-8\"", forHTTPHeaderField: "Content-Type")
|
||||
request.httpBody = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:allprop/></D:propfind>".data(using: .utf8)
|
||||
runDataTask(with: request, completionHandler: { (data, response, error) in
|
||||
// Depth infinity is disabled on some servers. Implement workaround?!
|
||||
request.setValue(recursive ? "infinity" : "1", forHTTPHeaderField: "Depth")
|
||||
request.setValue(authentication: credential, with: credentialType)
|
||||
request.setValue(contentType: .xml, charset: .utf8)
|
||||
request.httpBody = WebDavFileObject.xmlProp(including)
|
||||
let progress = Progress(totalUnitCount: -1)
|
||||
progress.setUserInfoObject(url, forKey: .fileURLKey)
|
||||
|
||||
let queryIsTruePredicate = query.predicateFormat == "TRUEPREDICATE"
|
||||
let task = session.dataTask(with: request) { (data, response, error) in
|
||||
// FIXME: paginating results
|
||||
var responseError: FileProviderWebDavError?
|
||||
var responseError: FileProviderHTTPError?
|
||||
if let code = (response as? HTTPURLResponse)?.statusCode , code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) {
|
||||
responseError = FileProviderWebDavError(code: rCode, path: path, errorDescription: String(data: data ?? Data(), encoding: .utf8), url: url)
|
||||
responseError = self.serverError(with: rCode, path: path, data: data)
|
||||
}
|
||||
if let data = data {
|
||||
let xresponse = DavResponse.parse(xmlResponse: data, baseURL: self.baseURL)
|
||||
var fileObjects = [WebDavFileObject]()
|
||||
for attr in xresponse {
|
||||
let fileObject = WebDavFileObject(attr)
|
||||
if !query.evaluate(with: fileObject.mapPredicate()) {
|
||||
continue
|
||||
}
|
||||
|
||||
fileObjects.append(fileObject)
|
||||
foundItemHandler?(fileObject)
|
||||
}
|
||||
completionHandler(fileObjects, responseError ?? error)
|
||||
guard let data = data else {
|
||||
completionHandler([], responseError ?? error)
|
||||
return
|
||||
}
|
||||
completionHandler([], responseError ?? error)
|
||||
})
|
||||
|
||||
let xresponse = DavResponse.parse(xmlResponse: data, baseURL: self.baseURL)
|
||||
var fileObjects = [WebDavFileObject]()
|
||||
for attr in xresponse where attr.href.path != url.path {
|
||||
let fileObject = WebDavFileObject(attr)
|
||||
if !queryIsTruePredicate && !query.evaluate(with: fileObject.mapPredicate()) {
|
||||
continue
|
||||
}
|
||||
|
||||
fileObjects.append(fileObject)
|
||||
progress.completedUnitCount = Int64(fileObjects.count)
|
||||
foundItemHandler?(fileObject)
|
||||
}
|
||||
completionHandler(fileObjects, responseError ?? error)
|
||||
}
|
||||
progress.cancellationHandler = { [weak task] in
|
||||
task?.cancel()
|
||||
}
|
||||
progress.setUserInfoObject(Date(), forKey: .startingTimeKey)
|
||||
task.resume()
|
||||
return progress
|
||||
}
|
||||
|
||||
open func isReachable(completionHandler: @escaping (Bool) -> Void) {
|
||||
override open func isReachable(completionHandler: @escaping (_ success: Bool, _ error: Error?) -> Void) {
|
||||
var request = URLRequest(url: baseURL!)
|
||||
request.httpMethod = "PROPFIND"
|
||||
request.setValue("0", forHTTPHeaderField: "Depth")
|
||||
request.setValue("text/xml; charset=\"utf-8\"", forHTTPHeaderField: "Content-Type")
|
||||
request.httpBody = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:prop><D:quota-available-bytes/><D:quota-used-bytes/></D:prop>\n</D:propfind>".data(using: .utf8)
|
||||
request.setValue(String(request.httpBody!.count), forHTTPHeaderField: "Content-Length")
|
||||
request.setValue(authentication: credential, with: credentialType)
|
||||
request.setValue(contentType: .xml, charset: .utf8)
|
||||
request.httpBody = WebDavFileObject.xmlProp([.volumeTotalCapacityKey, .volumeAvailableCapacityKey])
|
||||
runDataTask(with: request, completionHandler: { (data, response, error) in
|
||||
let status = (response as? HTTPURLResponse)?.statusCode ?? 400
|
||||
completionHandler(status < 300)
|
||||
})
|
||||
}
|
||||
|
||||
open weak var fileOperationDelegate: FileOperationDelegate?
|
||||
}
|
||||
|
||||
extension WebDAVFileProvider: FileProviderOperations {
|
||||
@discardableResult
|
||||
open func create(folder folderName: String, at atPath: String, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
let opType = FileOperationType.create(path: (atPath as NSString).appendingPathComponent(folderName) + "/")
|
||||
guard fileOperationDelegate?.fileProvider(self, shouldDoOperation: opType) ?? true == true else {
|
||||
return nil
|
||||
}
|
||||
let url = self.url(of: atPath).appendingPathComponent(folderName.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? folderName, isDirectory: true)
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "MKCOL"
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var responseError: FileProviderWebDavError?
|
||||
if let code = (response as? HTTPURLResponse)?.statusCode , code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) {
|
||||
responseError = FileProviderWebDavError(code: rCode, path: url.relativePath, errorDescription: String(data: data ?? Data(), encoding: .utf8), url: url)
|
||||
}
|
||||
completionHandler?(responseError ?? error)
|
||||
self.delegateNotify(opType, error: responseError ?? error)
|
||||
})
|
||||
task.taskDescription = opType.json
|
||||
task.resume()
|
||||
return RemoteOperationHandle(operationType: opType, tasks: [task])
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func moveItem(path: String, to toPath: String, overwrite: Bool = false, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
let opType = FileOperationType.move(source: path, destination: toPath)
|
||||
guard fileOperationDelegate?.fileProvider(self, shouldDoOperation: opType) ?? true == true else {
|
||||
return nil
|
||||
}
|
||||
return self.doOperation(operation: opType, overwrite: overwrite, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func copyItem(path: String, to toPath: String, overwrite: Bool = false, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
let opType = FileOperationType.copy(source: path, destination: toPath)
|
||||
guard fileOperationDelegate?.fileProvider(self, shouldDoOperation: opType) ?? true == true else {
|
||||
return nil
|
||||
}
|
||||
return self.doOperation(operation: opType, overwrite: overwrite, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func removeItem(path: String, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
let opType = FileOperationType.remove(path: path)
|
||||
guard fileOperationDelegate?.fileProvider(self, shouldDoOperation: opType) ?? true == true else {
|
||||
return nil
|
||||
}
|
||||
return self.doOperation(operation: opType, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
func doOperation(operation opType: FileOperationType, overwrite: Bool? = nil, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
let source = opType.source!
|
||||
let sourceURL = self.url(of: source)
|
||||
var request = URLRequest(url: sourceURL)
|
||||
if let dest = opType.destination {
|
||||
request.setValue(url(of:dest).absoluteString, forHTTPHeaderField: "Destination")
|
||||
}
|
||||
switch opType {
|
||||
case .copy:
|
||||
request.httpMethod = "COPY"
|
||||
case .move:
|
||||
request.httpMethod = "MOVE"
|
||||
case .remove:
|
||||
request.httpMethod = "DELETE"
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
if let overwrite = overwrite, !overwrite {
|
||||
request.setValue("F", forHTTPHeaderField: "Overwrite")
|
||||
}
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var responseError: FileProviderWebDavError?
|
||||
if let response = response as? HTTPURLResponse, let code = FileProviderHTTPErrorCode(rawValue: response.statusCode) {
|
||||
if response.statusCode >= 300 {
|
||||
responseError = FileProviderWebDavError(code: code, path: source, errorDescription: String(data: data ?? Data(), encoding: .utf8), url: sourceURL)
|
||||
}
|
||||
if code == .multiStatus, let data = data {
|
||||
let xresponses = DavResponse.parse(xmlResponse: data, baseURL: self.baseURL)
|
||||
for xresponse in xresponses where (xresponse.status ?? 0) >= 300 {
|
||||
let error = FileProviderWebDavError(code: code, path: source, errorDescription: String(data: data, encoding: .utf8), url: sourceURL)
|
||||
completionHandler?(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
if (response as? HTTPURLResponse)?.statusCode ?? 0 != FileProviderHTTPErrorCode.multiStatus.rawValue {
|
||||
completionHandler?(responseError ?? error)
|
||||
}
|
||||
|
||||
self.delegateNotify(opType, error: responseError ?? error)
|
||||
})
|
||||
task.taskDescription = opType.json
|
||||
task.resume()
|
||||
return RemoteOperationHandle(operationType: opType, tasks: [task])
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func copyItem(localFile: URL, to toPath: String, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
let opType = FileOperationType.copy(source: localFile.absoluteString, destination: toPath)
|
||||
guard fileOperationDelegate?.fileProvider(self, shouldDoOperation: opType) ?? true == true else {
|
||||
return nil
|
||||
}
|
||||
let url = self.url(of:toPath)
|
||||
var request = URLRequest(url: url)
|
||||
if !overwrite {
|
||||
request.setValue("F", forHTTPHeaderField: "Overwrite")
|
||||
}
|
||||
request.httpMethod = "PUT"
|
||||
let task = session.uploadTask(with: request, fromFile: localFile)
|
||||
completionHandlersForTasks[task.taskIdentifier] = { [weak self] error in
|
||||
var responseError: FileProviderWebDavError?
|
||||
if let code = (task.response as? HTTPURLResponse)?.statusCode , code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) {
|
||||
// We can't fetch server result from delegate!
|
||||
responseError = FileProviderWebDavError(code: rCode, path: toPath, errorDescription: nil, url: url)
|
||||
}
|
||||
completionHandler?(responseError ?? error)
|
||||
self?.delegateNotify(.create(path: toPath), error: responseError ?? error)
|
||||
}
|
||||
task.taskDescription = opType.json
|
||||
task.resume()
|
||||
return RemoteOperationHandle(operationType: opType, tasks: [task])
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func copyItem(path: String, toLocalURL destURL: URL, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
let opType = FileOperationType.copy(source: path, destination: destURL.absoluteString)
|
||||
guard fileOperationDelegate?.fileProvider(self, shouldDoOperation: opType) ?? true == true else {
|
||||
return nil
|
||||
}
|
||||
let url = self.url(of:path)
|
||||
let request = URLRequest(url: url)
|
||||
let task = session.downloadTask(with: request)
|
||||
completionHandlersForTasks[task.taskIdentifier] = completionHandler
|
||||
downloadCompletionHandlersForTasks[task.taskIdentifier] = { tempURL in
|
||||
guard let httpResponse = task.response as? HTTPURLResponse , httpResponse.statusCode < 300 else {
|
||||
let code = FileProviderHTTPErrorCode(rawValue: (task.response as? HTTPURLResponse)?.statusCode ?? -1)
|
||||
let serverError : FileProviderWebDavError? = code != nil ? FileProviderWebDavError(code: code!, path: path, errorDescription: code?.description, url: url) : nil
|
||||
completionHandler?(serverError)
|
||||
if status >= 400, let code = FileProviderHTTPErrorCode(rawValue: status) {
|
||||
let errorDesc = data.flatMap({ String(data: $0, encoding: .utf8) })
|
||||
let error = FileProviderWebDavError(code: code, path: "", serverDescription: errorDesc, url: self.baseURL!)
|
||||
completionHandler(false, error)
|
||||
return
|
||||
}
|
||||
do {
|
||||
try FileManager.default.moveItem(at: tempURL, to: destURL)
|
||||
completionHandler?(nil)
|
||||
} catch let e {
|
||||
completionHandler?(e)
|
||||
}
|
||||
}
|
||||
task.taskDescription = opType.json
|
||||
task.resume()
|
||||
return RemoteOperationHandle(operationType: opType, tasks: [task])
|
||||
}
|
||||
}
|
||||
|
||||
extension WebDAVFileProvider: FileProviderReadWrite {
|
||||
@discardableResult
|
||||
open func contents(path: String, offset: Int64, length: Int, completionHandler: @escaping ((_ contents: Data?, _ error: Error?) -> Void)) -> OperationHandle? {
|
||||
if length == 0 || offset < 0 {
|
||||
dispatch_queue.async {
|
||||
completionHandler(Data(), nil)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
let opType = FileOperationType.fetch(path: path)
|
||||
let url = self.url(of: path)
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "GET"
|
||||
if length > 0 {
|
||||
request.setValue("bytes=\(offset)-\(offset + Int64(length) - 1)", forHTTPHeaderField: "Range")
|
||||
} else if offset > 0 && length < 0 {
|
||||
request.setValue("bytes=\(offset)-", forHTTPHeaderField: "Range")
|
||||
}
|
||||
let handle = RemoteOperationHandle(operationType: opType, tasks: [])
|
||||
runDataTask(with: request, operationHandle: handle, completionHandler: { (data, response, error) in
|
||||
var responseError: FileProviderWebDavError?
|
||||
if let code = (response as? HTTPURLResponse)?.statusCode , code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) {
|
||||
responseError = FileProviderWebDavError(code: rCode, path: path, errorDescription: String(data: data ?? Data(), encoding: .utf8), url: url)
|
||||
}
|
||||
completionHandler(data, responseError ?? error)
|
||||
completionHandler(status < 300, error)
|
||||
})
|
||||
return handle
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func writeContents(path: String, contents data: Data?, atomically: Bool, overwrite: Bool, completionHandler: SimpleCompletionHandler) -> OperationHandle? {
|
||||
let opType = FileOperationType.modify(path: path)
|
||||
guard fileOperationDelegate?.fileProvider(self, shouldDoOperation: opType) ?? true == true else {
|
||||
return nil
|
||||
}
|
||||
// FIXME: lock destination before writing process
|
||||
let url = atomically ? self.url(of: path).appendingPathExtension("tmp") : self.url(of: path)
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "PUT"
|
||||
if !overwrite {
|
||||
request.setValue("F", forHTTPHeaderField: "Overwrite")
|
||||
}
|
||||
let task = session.uploadTask(with: request, from: data ?? Data())
|
||||
completionHandlersForTasks[task.taskIdentifier] = { [weak self] error in
|
||||
var responseError: FileProviderWebDavError?
|
||||
if let code = (task.response as? HTTPURLResponse)?.statusCode , code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) {
|
||||
// We can't fetch server result from delegate!
|
||||
responseError = FileProviderWebDavError(code: rCode, path: path, errorDescription: nil, url: url)
|
||||
open func publicLink(to path: String, completionHandler: @escaping ((URL?, FileObject?, Date?, Error?) -> Void)) {
|
||||
guard self.baseURL?.host?.contains("dav.yandex.") ?? false else {
|
||||
dispatch_queue.async {
|
||||
completionHandler(nil, nil, nil, self.urlError(path, code: .resourceUnavailable))
|
||||
}
|
||||
completionHandler?(responseError ?? error)
|
||||
self?.delegateNotify(.create(path: path), error: responseError ?? error)
|
||||
return
|
||||
}
|
||||
|
||||
let url = self.url(of: path)
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "PROPPATCH"
|
||||
request.setValue(authentication: credential, with: credentialType)
|
||||
request.setValue(contentType: .xml, charset: .utf8)
|
||||
let body = "<propertyupdate xmlns=\"DAV:\">\n<set><prop>\n<public_url xmlns=\"urn:yandex:disk:meta\">true</public_url>\n</prop></set>\n</propertyupdate>"
|
||||
request.httpBody = body.data(using: .utf8)
|
||||
runDataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var responseError: FileProviderHTTPError?
|
||||
if let code = (response as? HTTPURLResponse)?.statusCode, code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) {
|
||||
responseError = self.serverError(with: rCode, path: path, data: data)
|
||||
}
|
||||
if let data = data {
|
||||
let xresponse = DavResponse.parse(xmlResponse: data, baseURL: self.baseURL)
|
||||
if let urlStr = xresponse.first?.prop["public_url"], let url = URL(string: urlStr) {
|
||||
completionHandler(url, nil, nil, nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
completionHandler(nil, nil, nil, responseError ?? error)
|
||||
})
|
||||
}
|
||||
|
||||
override func request(for operation: FileOperationType, overwrite: Bool = true, attributes: [URLResourceKey: Any] = [:]) -> URLRequest {
|
||||
let method: String
|
||||
let url: URL
|
||||
let sourceURL = self.url(of: operation.source)
|
||||
|
||||
switch operation {
|
||||
case .fetch:
|
||||
method = "GET"
|
||||
url = sourceURL
|
||||
case .create:
|
||||
if sourceURL.absoluteString.hasSuffix("/") {
|
||||
method = "MKCOL"
|
||||
url = sourceURL
|
||||
} else {
|
||||
fallthrough
|
||||
}
|
||||
case .modify:
|
||||
method = "PUT"
|
||||
url = sourceURL
|
||||
break
|
||||
case .copy(let source, let dest):
|
||||
if source.hasPrefix("file://") {
|
||||
method = "PUT"
|
||||
url = self.url(of: dest)
|
||||
} else if dest.hasPrefix("file://") {
|
||||
method = "GET"
|
||||
url = sourceURL
|
||||
} else {
|
||||
method = "COPY"
|
||||
url = sourceURL
|
||||
}
|
||||
case .move:
|
||||
method = "MOVE"
|
||||
url = sourceURL
|
||||
case .remove:
|
||||
method = "DELETE"
|
||||
url = sourceURL
|
||||
default:
|
||||
fatalError("Unimplemented operation \(operation.description) in \(#file)")
|
||||
}
|
||||
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = method
|
||||
request.setValue(authentication: credential, with: credentialType)
|
||||
request.setValue(overwrite ? "T" : "F", forHTTPHeaderField: "Overwrite")
|
||||
if let dest = operation.destination, !dest.hasPrefix("file://") {
|
||||
request.setValue(self.url(of:dest).absoluteString, forHTTPHeaderField: "Destination")
|
||||
}
|
||||
|
||||
return request
|
||||
}
|
||||
|
||||
override func serverError(with code: FileProviderHTTPErrorCode, path: String?, data: Data?) -> FileProviderHTTPError {
|
||||
return FileProviderWebDavError(code: code, path: path ?? "", serverDescription: data.flatMap({ String(data: $0, encoding: .utf8) }), url: self.url(of: path ?? ""))
|
||||
}
|
||||
|
||||
override func multiStatusHandler(source: String, data: Data, completionHandler: SimpleCompletionHandler) {
|
||||
let xresponses = DavResponse.parse(xmlResponse: data, baseURL: self.baseURL)
|
||||
for xresponse in xresponses where (xresponse.status ?? 0) >= 300 {
|
||||
let code = xresponse.status.flatMap { FileProviderHTTPErrorCode(rawValue: $0) } ?? .internalServerError
|
||||
let error = self.serverError(with: code, path: source, data: data)
|
||||
completionHandler?(error)
|
||||
}
|
||||
task.taskDescription = opType.json
|
||||
task.resume()
|
||||
return RemoteOperationHandle(operationType: opType, tasks: [task])
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -504,33 +398,77 @@ extension WebDAVFileProvider: FileProviderReadWrite {
|
||||
// TODO: implements methods for lock mechanism
|
||||
}
|
||||
|
||||
extension WebDAVFileProvider: FileProvider { }
|
||||
|
||||
// MARK: WEBDAV XML response implementation
|
||||
|
||||
internal extension WebDAVFileProvider {
|
||||
fileprivate func delegateNotify(_ operation: FileOperationType, error: Error?) {
|
||||
DispatchQueue.main.async(execute: {
|
||||
if error == nil {
|
||||
self.delegate?.fileproviderSucceed(self, operation: operation)
|
||||
} else {
|
||||
self.delegate?.fileproviderFailed(self, operation: operation)
|
||||
extension WebDAVFileProvider: ExtendedFileProvider {
|
||||
#if os(macOS) || os(iOS) || os(tvOS)
|
||||
open func thumbnailOfFileSupported(path: String) -> Bool {
|
||||
guard self.baseURL?.host?.contains("dav.yandex.") ?? false else {
|
||||
return false
|
||||
}
|
||||
let supportedExt: [String] = ["jpg", "jpeg", "png", "gif"]
|
||||
return supportedExt.contains((path as NSString).pathExtension)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func thumbnailOfFile(path: String, dimension: CGSize?, completionHandler: @escaping ((ImageClass?, Error?) -> Void)) -> Progress? {
|
||||
guard self.baseURL?.host?.contains("dav.yandex.") ?? false else {
|
||||
dispatch_queue.async {
|
||||
completionHandler(nil, self.urlError(path, code: .resourceUnavailable))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
let dimension = dimension ?? CGSize(width: 64, height: 64)
|
||||
let url = URL(string: self.url(of: path).absoluteString + "?preview&size=\(dimension.width)x\(dimension.height)")!
|
||||
var request = URLRequest(url: url)
|
||||
request.httpMethod = "GET"
|
||||
request.setValue(authentication: credential, with: credentialType)
|
||||
let task = session.dataTask(with: request, completionHandler: { (data, response, error) in
|
||||
var responseError: FileProviderHTTPError?
|
||||
if let code = (response as? HTTPURLResponse)?.statusCode , code >= 300, let rCode = FileProviderHTTPErrorCode(rawValue: code) {
|
||||
responseError = self.serverError(with: rCode, path: url.relativePath, data: data)
|
||||
completionHandler(nil, responseError ?? error)
|
||||
return
|
||||
}
|
||||
|
||||
completionHandler(data.flatMap({ ImageClass(data: $0) }), nil)
|
||||
})
|
||||
task.resume()
|
||||
return nil
|
||||
}
|
||||
#endif
|
||||
|
||||
open func propertiesOfFileSupported(path: String) -> Bool {
|
||||
return false
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
open func propertiesOfFile(path: String, completionHandler: @escaping (([String : Any], [String], Error?) -> Void)) -> Progress? {
|
||||
dispatch_queue.async {
|
||||
completionHandler([:], [], self.urlError(path, code: .resourceUnavailable))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: WEBDAV XML response implementation
|
||||
|
||||
struct DavResponse {
|
||||
let href: URL
|
||||
let hrefString: String
|
||||
let status: Int?
|
||||
let prop: [String: String]
|
||||
|
||||
static let urlAllowed = CharacterSet(charactersIn: " ").inverted
|
||||
|
||||
init? (_ node: AEXMLElement, baseURL: URL?) {
|
||||
|
||||
func standardizePath(_ str: String) -> String {
|
||||
#if swift(>=4.0)
|
||||
let trimmedStr = str.hasPrefix("/") ? String(str[str.index(after: str.startIndex)...]) : str
|
||||
#else
|
||||
let trimmedStr = str.hasPrefix("/") ? str.substring(from: str.index(after: str.startIndex)) : str
|
||||
return trimmedStr.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? str
|
||||
#endif
|
||||
return trimmedStr.addingPercentEncoding(withAllowedCharacters: .filePathAllowed) ?? str
|
||||
}
|
||||
|
||||
// find node names with namespace
|
||||
@@ -551,8 +489,10 @@ struct DavResponse {
|
||||
|
||||
guard let hrefString = node[hreftag].value else { return nil }
|
||||
|
||||
// Percent-encoding space, some servers return invalid urls which space is not encoded to %20
|
||||
let hrefStrPercented = hrefString.addingPercentEncoding(withAllowedCharacters: DavResponse.urlAllowed) ?? hrefString
|
||||
// trying to figure out relative path out of href
|
||||
let hrefAbsolute = URL(string: hrefString, relativeTo: baseURL)?.absoluteURL
|
||||
let hrefAbsolute = URL(string: hrefStrPercented, relativeTo: baseURL)?.absoluteURL
|
||||
let relativePath: String
|
||||
if hrefAbsolute?.host?.replacingOccurrences(of: "www.", with: "", options: .anchored) == baseURL?.host?.replacingOccurrences(of: "www.", with: "", options: .anchored) {
|
||||
relativePath = hrefAbsolute?.path.replacingOccurrences(of: baseURL?.absoluteURL.path ?? "", with: "", options: .anchored, range: nil) ?? hrefString
|
||||
@@ -585,9 +525,11 @@ struct DavResponse {
|
||||
break
|
||||
}
|
||||
for propItemNode in propStatNode[proptag].children {
|
||||
propDic[propItemNode.name.components(separatedBy: ":").last!.lowercased()] = propItemNode.value
|
||||
if propItemNode.name.hasSuffix("resourcetype") && propItemNode.xml.contains("collection") {
|
||||
propDic["getcontenttype"] = "httpd/unix-directory"
|
||||
let key = propItemNode.name.components(separatedBy: ":").last!.lowercased()
|
||||
guard propDic.index(forKey: key) == nil else { continue }
|
||||
propDic[key] = propItemNode.value
|
||||
if key == "resourcetype" && propItemNode.xml.contains("collection") {
|
||||
propDic["getcontenttype"] = ContentMIMEType.directory.rawValue
|
||||
}
|
||||
}
|
||||
self.href = href
|
||||
@@ -626,21 +568,22 @@ public final class WebDavFileObject: FileObject {
|
||||
let path = relativePath.hasPrefix("/") ? relativePath : ("/" + relativePath)
|
||||
super.init(url: href, name: name, path: path)
|
||||
self.size = Int64(davResponse.prop["getcontentlength"] ?? "-1") ?? NSURLSessionTransferSizeUnknown
|
||||
self.creationDate = Date(rfcString: davResponse.prop["creationdate"] ?? "")
|
||||
self.modifiedDate = Date(rfcString: davResponse.prop["getlastmodified"] ?? "")
|
||||
self.contentType = davResponse.prop["getcontenttype"] ?? "octet/stream"
|
||||
self.creationDate = davResponse.prop["creationdate"].flatMap { Date(rfcString: $0) }
|
||||
self.modifiedDate = davResponse.prop["getlastmodified"].flatMap { Date(rfcString: $0) }
|
||||
self.contentType = davResponse.prop["getcontenttype"].flatMap(ContentMIMEType.init(rawValue:)) ?? .stream
|
||||
self.isHidden = (Int(davResponse.prop["ishidden"] ?? "0") ?? 0) > 0
|
||||
self.type = self.contentType == "httpd/unix-directory" ? .directory : .regular
|
||||
self.isReadOnly = (Int(davResponse.prop["isreadonly"] ?? "0") ?? 0) > 0
|
||||
self.type = (self.contentType == .directory) ? .directory : .regular
|
||||
self.entryTag = davResponse.prop["getetag"]
|
||||
}
|
||||
|
||||
/// MIME type of the file.
|
||||
open internal(set) var contentType: String {
|
||||
open internal(set) var contentType: ContentMIMEType {
|
||||
get {
|
||||
return allValues[.mimeTypeKey] as? String ?? ""
|
||||
return (allValues[.mimeTypeKey] as? String).flatMap(ContentMIMEType.init(rawValue:)) ?? .stream
|
||||
}
|
||||
set {
|
||||
allValues[.mimeTypeKey] = newValue
|
||||
allValues[.mimeTypeKey] = newValue.rawValue
|
||||
}
|
||||
}
|
||||
|
||||
@@ -668,6 +611,11 @@ public final class WebDavFileObject: FileObject {
|
||||
return "ishidden"
|
||||
case URLResourceKey.entryTagKey:
|
||||
return "getetag"
|
||||
case URLResourceKey.volumeTotalCapacityKey:
|
||||
// WebDAV doesn't have total capacity, but it's can be calculated via used capacity
|
||||
return "quota-used-bytes"
|
||||
case URLResourceKey.volumeAvailableCapacityKey:
|
||||
return "quota-available-bytes"
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
@@ -682,16 +630,22 @@ public final class WebDavFileObject: FileObject {
|
||||
}
|
||||
if propKeys.isEmpty {
|
||||
propKeys = "<D:allprop/>"
|
||||
} else {
|
||||
propKeys += "<D:prop><D:resourcetype/></D:prop>"
|
||||
}
|
||||
return propKeys
|
||||
}
|
||||
|
||||
internal class func xmlProp(_ keys: [URLResourceKey]) -> Data {
|
||||
return "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n\(WebDavFileObject.propString(keys))\n</D:propfind>".data(using: .utf8)!
|
||||
}
|
||||
}
|
||||
|
||||
/// Error returned by WebDAV server when trying to access or do operations on a file or folder.
|
||||
public struct FileProviderWebDavError: FileProviderHTTPError {
|
||||
public let code: FileProviderHTTPErrorCode
|
||||
public let path: String
|
||||
public let errorDescription: String?
|
||||
public let serverDescription: String?
|
||||
/// URL of resource caused error.
|
||||
public let url: URL
|
||||
}
|
||||
|
||||
@@ -0,0 +1,394 @@
|
||||
//
|
||||
// FilesProviderTests.swift
|
||||
// FilesProviderTests
|
||||
//
|
||||
// Created by Amir Abbas on 8/11/1396 AP.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
import FilesProvider
|
||||
|
||||
class FilesProviderTests: XCTestCase, FileProviderDelegate {
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
// Put setup code here. This method is called before the invocation of each test method in the class.
|
||||
}
|
||||
|
||||
override func tearDown() {
|
||||
// Put teardown code here. This method is called after the invocation of each test method in the class.
|
||||
super.tearDown()
|
||||
try? FileManager.default.removeItem(at: dummyFile())
|
||||
}
|
||||
|
||||
func testLocal() {
|
||||
let provider = LocalFileProvider()
|
||||
addTeardownBlock {
|
||||
self.testRemoveFile(provider, filePath: self.testFolderName)
|
||||
}
|
||||
testBasic(provider)
|
||||
testArchiving(provider)
|
||||
testOperations(provider)
|
||||
}
|
||||
|
||||
func testWebDav() {
|
||||
guard let urlStr = ProcessInfo.processInfo.environment["webdav_url"] else { return }
|
||||
let url = URL(string: urlStr)!
|
||||
let cred: URLCredential?
|
||||
if let user = ProcessInfo.processInfo.environment["webdav_user"], let pass = ProcessInfo.processInfo.environment["webdav_password"] {
|
||||
cred = URLCredential(user: user, password: pass, persistence: .forSession)
|
||||
} else {
|
||||
cred = nil
|
||||
}
|
||||
let provider = WebDAVFileProvider(baseURL: url, credential: cred)!
|
||||
provider.delegate = self
|
||||
testArchiving(provider)
|
||||
addTeardownBlock {
|
||||
self.testRemoveFile(provider, filePath: self.testFolderName)
|
||||
}
|
||||
testOperations(provider)
|
||||
}
|
||||
|
||||
func testDropbox() {
|
||||
guard let pass = ProcessInfo.processInfo.environment["dropbox_token"] else {
|
||||
return
|
||||
}
|
||||
let cred = URLCredential(user: "testuser", password: pass, persistence: .forSession)
|
||||
let provider = DropboxFileProvider(credential: cred)
|
||||
provider.delegate = self
|
||||
testArchiving(provider)
|
||||
addTeardownBlock {
|
||||
self.testRemoveFile(provider, filePath: self.testFolderName)
|
||||
}
|
||||
testBasic(provider)
|
||||
testOperations(provider)
|
||||
}
|
||||
|
||||
func testFTPPassive() {
|
||||
guard let urlStr = ProcessInfo.processInfo.environment["ftp_url"] else { return }
|
||||
let url = URL(string: urlStr)!
|
||||
let cred: URLCredential?
|
||||
if let user = ProcessInfo.processInfo.environment["ftp_user"], let pass = ProcessInfo.processInfo.environment["ftp_password"] {
|
||||
cred = URLCredential(user: user, password: pass, persistence: .forSession)
|
||||
} else {
|
||||
cred = nil
|
||||
}
|
||||
let provider = FTPFileProvider(baseURL: url, mode: .extendedPassive, credential: cred)!
|
||||
provider.delegate = self
|
||||
testArchiving(provider)
|
||||
addTeardownBlock {
|
||||
self.testRemoveFile(provider, filePath: self.testFolderName)
|
||||
}
|
||||
testOperations(provider)
|
||||
}
|
||||
|
||||
func testOneDrive() {
|
||||
guard let pass = ProcessInfo.processInfo.environment["onedrive_token"] else {
|
||||
return
|
||||
}
|
||||
let cred = URLCredential(user: "testuser", password: pass, persistence: .forSession)
|
||||
let provider = OneDriveFileProvider(credential: cred)
|
||||
provider.delegate = self
|
||||
testBasic(provider)
|
||||
testArchiving(provider)
|
||||
addTeardownBlock {
|
||||
self.testRemoveFile(provider, filePath: self.testFolderName)
|
||||
}
|
||||
testOperations(provider)
|
||||
}
|
||||
|
||||
/*
|
||||
func testPerformanceExample() {
|
||||
// This is an example of a performance test case.
|
||||
self.measure {
|
||||
// Put the code you want to measure the time of here.
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
let timeout: Double = 60.0
|
||||
let testFolderName = "Test"
|
||||
let textFilePath = "/Test/file.txt"
|
||||
let renamedFilePath = "/Test/renamed.txt"
|
||||
let uploadFilePath = "/Test/uploaded.dat"
|
||||
let sampleText = "Hello world!"
|
||||
|
||||
fileprivate func testCreateFolder(_ provider: FileProvider, folderName: String) {
|
||||
let desc = "Creating folder at root in \(provider.type)"
|
||||
print("Test started: \(desc).")
|
||||
let expectation = XCTestExpectation(description: desc)
|
||||
provider.create(folder: folderName, at: "/") { (error) in
|
||||
XCTAssertNil(error, "\(desc) failed: \(error?.localizedDescription ?? "no error desc")")
|
||||
expectation.fulfill()
|
||||
}
|
||||
wait(for: [expectation], timeout: timeout)
|
||||
print("Test fulfilled: \(desc).")
|
||||
}
|
||||
|
||||
fileprivate func testContentsOfDirectory(_ provider: FileProvider) {
|
||||
let desc = "Enumerating files list in \(provider.type)"
|
||||
print("Test started: \(desc).")
|
||||
let expectation = XCTestExpectation(description: desc)
|
||||
provider.contentsOfDirectory(path: "/") { (files, error) in
|
||||
XCTAssertNil(error, "\(desc) failed: \(error?.localizedDescription ?? "no error desc")")
|
||||
XCTAssertGreaterThan(files.count, 0, "list is empty")
|
||||
let testFolder = files.filter({ $0.name == self.testFolderName }).first
|
||||
XCTAssertNotNil(testFolder, "Test folder didn't listed")
|
||||
guard testFolder != nil else { return }
|
||||
XCTAssertTrue(testFolder!.isDirectory, "Test entry is not a folder")
|
||||
XCTAssertLessThanOrEqual(testFolder!.size, 0, "folder size is not -1")
|
||||
expectation.fulfill()
|
||||
}
|
||||
wait(for: [expectation], timeout: timeout)
|
||||
print("Test fulfilled: \(desc).")
|
||||
}
|
||||
|
||||
fileprivate func testAttributesOfFile(_ provider: FileProvider, filePath: String) {
|
||||
let desc = "Attrubutes of file in \(provider.type)"
|
||||
print("Test started: \(desc).")
|
||||
let expectation = XCTestExpectation(description: desc)
|
||||
provider.attributesOfItem(path: filePath) { (fileObject, error) in
|
||||
XCTAssertNil(error, "\(desc) failed: \(error?.localizedDescription ?? "no error desc")")
|
||||
XCTAssertNotNil(fileObject, "file '\(filePath)' didn't exist")
|
||||
guard fileObject != nil else { return }
|
||||
XCTAssertEqual(fileObject!.path, filePath, "file path is different from '\(filePath)'")
|
||||
XCTAssertEqual(fileObject!.type, URLFileResourceType.regular, "file '\(filePath)' is not a regular file")
|
||||
XCTAssertGreaterThan(fileObject!.size, 0, "file '\(filePath)' is empty")
|
||||
XCTAssertNotNil(fileObject!.modifiedDate, "file '\(filePath)' has no modification date")
|
||||
expectation.fulfill()
|
||||
}
|
||||
wait(for: [expectation], timeout: timeout)
|
||||
print("Test fulfilled: \(desc).")
|
||||
}
|
||||
|
||||
fileprivate func testCreateFile(_ provider: FileProvider, filePath: String) {
|
||||
let desc = "Creating file in \(provider.type)"
|
||||
print("Test started: \(desc).")
|
||||
let expectation = XCTestExpectation(description: desc)
|
||||
let data = sampleText.data(using: .ascii)
|
||||
provider.writeContents(path: filePath, contents: data, overwrite: true) { (error) in
|
||||
XCTAssertNil(error, "\(desc) failed: \(error?.localizedDescription ?? "no error desc")")
|
||||
expectation.fulfill()
|
||||
}
|
||||
wait(for: [expectation], timeout: timeout * 3)
|
||||
print("Test fulfilled: \(desc).")
|
||||
}
|
||||
|
||||
fileprivate func testContentsFile(_ provider: FileProvider, filePath: String, hasSampleText: Bool = true) {
|
||||
let desc = "Reading file in \(provider.type)"
|
||||
print("Test started: \(desc).")
|
||||
let expectation = XCTestExpectation(description: desc)
|
||||
provider.contents(path: filePath) { (data, error) in
|
||||
XCTAssertNil(error, "\(desc) failed: \(error?.localizedDescription ?? "no error desc")")
|
||||
XCTAssertNotNil(data, "no data for test file")
|
||||
if data != nil && hasSampleText {
|
||||
let str = String(data: data!, encoding: .ascii)
|
||||
XCTAssertNotNil(str, "test file data not readable")
|
||||
XCTAssertEqual(str, self.sampleText, "test file data didn't matched")
|
||||
}
|
||||
expectation.fulfill()
|
||||
}
|
||||
wait(for: [expectation], timeout: timeout * 3)
|
||||
print("Test fulfilled: \(desc).")
|
||||
}
|
||||
|
||||
fileprivate func testRenameFile(_ provider: FileProvider, filePath: String, to toPath: String) {
|
||||
let desc = "Renaming file in \(provider.type)"
|
||||
print("Test started: \(desc).")
|
||||
let expectation = XCTestExpectation(description: desc)
|
||||
provider.moveItem(path: filePath, to: toPath, overwrite: true) { (error) in
|
||||
XCTAssertNil(error, "\(desc) failed: \(error?.localizedDescription ?? "no error desc")")
|
||||
expectation.fulfill()
|
||||
}
|
||||
wait(for: [expectation], timeout: timeout)
|
||||
print("Test fulfilled: \(desc).")
|
||||
}
|
||||
|
||||
fileprivate func testCopyFile(_ provider: FileProvider, filePath: String, to toPath: String) {
|
||||
let desc = "Copying file in \(provider.type)"
|
||||
print("Test started: \(desc).")
|
||||
let expectation = XCTestExpectation(description: desc)
|
||||
provider.copyItem(path: filePath, to: toPath, overwrite: true) { (error) in
|
||||
XCTAssertNil(error, "\(desc) failed: \(error?.localizedDescription ?? "no error desc")")
|
||||
expectation.fulfill()
|
||||
}
|
||||
if provider is FTPFileProvider {
|
||||
// FTP will need to download and upload file again.
|
||||
wait(for: [expectation], timeout: timeout * 6)
|
||||
} else {
|
||||
wait(for: [expectation], timeout: timeout)
|
||||
}
|
||||
print("Test fulfilled: \(desc).")
|
||||
}
|
||||
|
||||
fileprivate func testRemoveFile(_ provider: FileProvider, filePath: String) {
|
||||
let desc = "Deleting file in \(provider.type)"
|
||||
print("Test started: \(desc).")
|
||||
let expectation = XCTestExpectation(description: desc)
|
||||
provider.removeItem(path: filePath) { (error) in
|
||||
XCTAssertNil(error, "\(desc) failed: \(error?.localizedDescription ?? "no error desc")")
|
||||
expectation.fulfill()
|
||||
}
|
||||
wait(for: [expectation], timeout: timeout)
|
||||
print("Test fulfilled: \(desc).")
|
||||
}
|
||||
|
||||
private func randomData(size: Int = 262144) -> Data {
|
||||
var keyData = Data(count: size)
|
||||
let result = keyData.withUnsafeMutableBytes {
|
||||
SecRandomCopyBytes(kSecRandomDefault, keyData.count, $0)
|
||||
}
|
||||
if result == errSecSuccess {
|
||||
return keyData
|
||||
} else {
|
||||
fatalError("Problem generating random bytes")
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate func dummyFile() -> URL {
|
||||
let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("dummyfile.dat")
|
||||
|
||||
if !FileManager.default.fileExists(atPath: url.path) {
|
||||
let data = randomData()
|
||||
try! data.write(to: url)
|
||||
}
|
||||
return url
|
||||
}
|
||||
|
||||
fileprivate func testUploadFile(_ provider: FileProvider, filePath: String) {
|
||||
// test Upload/Download
|
||||
let desc = "Uploading file in \(provider.type)"
|
||||
print("Test started: \(desc).")
|
||||
let expectation = XCTestExpectation(description: desc)
|
||||
let dummy = dummyFile()
|
||||
provider.copyItem(localFile: dummy, to: filePath) { (error) in
|
||||
XCTAssertNil(error, "\(desc) failed: \(error?.localizedDescription ?? "no error desc")")
|
||||
expectation.fulfill()
|
||||
}
|
||||
wait(for: [expectation], timeout: timeout * 3)
|
||||
print("Test fulfilled: \(desc).")
|
||||
}
|
||||
|
||||
fileprivate func testDownloadFile(_ provider: FileProvider, filePath: String) {
|
||||
let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("downloadedfile.dat")
|
||||
let desc = "Downloading file in \(provider.type)"
|
||||
print("Test started: \(desc).")
|
||||
let expectation = XCTestExpectation(description: desc)
|
||||
provider.copyItem(path: filePath, toLocalURL: url) { (error) in
|
||||
XCTAssertNil(error, "\(desc) failed: \(error?.localizedDescription ?? "no error desc")")
|
||||
XCTAssertTrue(FileManager.default.fileExists(atPath: url.path), "downloaded file doesn't exist")
|
||||
let size = (try? FileManager.default.attributesOfItem(atPath: url.path))?[FileAttributeKey.size] as? Int64
|
||||
XCTAssertEqual(size, 262144, "downloaded file size is unexpected")
|
||||
XCTAssert(FileManager.default.contentsEqual(atPath: self.dummyFile().path, andPath: url.path), "downloaded data is corrupted")
|
||||
try? FileManager.default.removeItem(at: url)
|
||||
expectation.fulfill()
|
||||
}
|
||||
wait(for: [expectation], timeout: timeout * 3)
|
||||
print("Test fulfilled: \(desc).")
|
||||
}
|
||||
|
||||
fileprivate func testStorageProperties(_ provider: FileProvider, isExpected: Bool) {
|
||||
let desc = "Querying volume in \(provider.type)"
|
||||
print("Test started: \(desc).")
|
||||
let expectation = XCTestExpectation(description: desc)
|
||||
provider.storageProperties { (volume) in
|
||||
if !isExpected {
|
||||
XCTAssertNotNil(volume, "volume information is nil")
|
||||
guard volume != nil else { return }
|
||||
XCTAssertGreaterThan(volume!.totalCapacity, 0, "capacity must be greater than 0")
|
||||
XCTAssertGreaterThan(volume!.availableCapacity, 0, "available capacity must be greater than 0")
|
||||
XCTAssertEqual(volume!.totalCapacity, volume!.availableCapacity + volume!.usage, "total capacity is not equal to usage + available")
|
||||
} else {
|
||||
XCTAssertNil(volume, "volume information must be nil")
|
||||
}
|
||||
expectation.fulfill()
|
||||
}
|
||||
wait(for: [expectation], timeout: timeout)
|
||||
print("Test fulfilled: \(desc).")
|
||||
}
|
||||
|
||||
fileprivate func testReachability(_ provider: FileProvider) {
|
||||
let desc = "Reachability of \(provider.type)"
|
||||
print("Test started: \(desc).")
|
||||
let expectation = XCTestExpectation(description: desc)
|
||||
provider.isReachable { (status, error) in
|
||||
XCTAssertTrue(status, "\(provider.type) not reachable: \(error?.localizedDescription ?? "no error desc")")
|
||||
expectation.fulfill()
|
||||
}
|
||||
wait(for: [expectation], timeout: timeout)
|
||||
print("Test fulfilled: \(desc).")
|
||||
}
|
||||
|
||||
fileprivate func testArchiving(_ provider: FileProvider) {
|
||||
let archivedData = NSKeyedArchiver.archivedData(withRootObject: provider)
|
||||
let unarchived = NSKeyedUnarchiver.unarchiveObject(with: archivedData) as? FileProvider
|
||||
XCTAssertNotNil(unarchived)
|
||||
XCTAssertEqual(unarchived?.baseURL, provider.baseURL, "archived provider is not same with original")
|
||||
XCTAssertEqual(unarchived?.credential, provider.credential, "archived provider is not same with original")
|
||||
if let provider = provider as? FileProviderBasicRemote, let unarchived_r = unarchived as? FileProviderBasicRemote {
|
||||
XCTAssertEqual(unarchived_r.useCache, provider.useCache, "archived provider is not same with original")
|
||||
XCTAssertEqual(unarchived_r.validatingCache, provider.validatingCache, "archived provider is not same with original")
|
||||
}
|
||||
if let provider = provider as? OneDriveFileProvider, let unarchived_o = unarchived as? OneDriveFileProvider {
|
||||
XCTAssertEqual(unarchived_o.route.rawValue, provider.route.rawValue, "archived provider is not same with original")
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate func testBasic(_ provider: FileProvider) {
|
||||
let filepath = "/test/file.txt"
|
||||
let fileurl = provider.url(of: filepath)
|
||||
let composedfilepath = provider.relativePathOf(url: fileurl)
|
||||
XCTAssertEqual(composedfilepath, "test/file.txt", "file url synthesis error")
|
||||
|
||||
let dirpath = "/test/"
|
||||
let dirurl = provider.url(of: dirpath)
|
||||
let composeddirpath = provider.relativePathOf(url: dirurl)
|
||||
XCTAssertEqual(composeddirpath, "test", "directory url synthesis error")
|
||||
|
||||
let rooturl1 = provider.url(of: "")
|
||||
let rooturl2 = provider.url(of: "/")
|
||||
XCTAssertEqual(rooturl1, rooturl2, "root url synthesis error")
|
||||
}
|
||||
|
||||
fileprivate func testOperations(_ provider: FileProvider) {
|
||||
// Test file operations
|
||||
testReachability(provider)
|
||||
testCreateFolder(provider, folderName: testFolderName)
|
||||
testContentsOfDirectory(provider)
|
||||
testCreateFile(provider, filePath: textFilePath)
|
||||
testAttributesOfFile(provider, filePath: textFilePath)
|
||||
testContentsFile(provider, filePath: textFilePath)
|
||||
testRenameFile(provider, filePath: textFilePath, to: renamedFilePath)
|
||||
testCopyFile(provider, filePath: renamedFilePath, to: textFilePath)
|
||||
testRemoveFile(provider, filePath: textFilePath)
|
||||
|
||||
// TODO: Test search
|
||||
// TODO: Test provider delegate
|
||||
|
||||
// Test upload/download
|
||||
testUploadFile(provider, filePath: uploadFilePath)
|
||||
testDownloadFile(provider, filePath: uploadFilePath)
|
||||
}
|
||||
|
||||
func fileproviderSucceed(_ fileProvider: FileProviderOperations, operation: FileOperationType) {
|
||||
return
|
||||
}
|
||||
|
||||
func fileproviderFailed(_ fileProvider: FileProviderOperations, operation: FileOperationType, error: Error) {
|
||||
return
|
||||
}
|
||||
|
||||
func fileproviderProgress(_ fileProvider: FileProviderOperations, operation: FileOperationType, progress: Float) {
|
||||
switch operation {
|
||||
case .copy(source: let source, destination: let dest) where dest.hasPrefix("file://"):
|
||||
print("Downloading \(source) to \((dest as NSString).lastPathComponent): \(progress * 100) completed.")
|
||||
case .copy(source: let source, destination: let dest) where source.hasPrefix("file://"):
|
||||
print("Uploading \((source as NSString).lastPathComponent) to \(dest): \(progress * 100) completed.")
|
||||
case .copy(source: let source, destination: let dest):
|
||||
print("Copy \(source) to \(dest): \(progress * 100) completed.")
|
||||
default:
|
||||
break
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>CFBundleDevelopmentRegion</key>
|
||||
<string>$(DEVELOPMENT_LANGUAGE)</string>
|
||||
<key>CFBundleExecutable</key>
|
||||
<string>$(EXECUTABLE_NAME)</string>
|
||||
<key>CFBundleIdentifier</key>
|
||||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
|
||||
<key>CFBundleInfoDictionaryVersion</key>
|
||||
<string>6.0</string>
|
||||
<key>CFBundleName</key>
|
||||
<string>$(PRODUCT_NAME)</string>
|
||||
<key>CFBundlePackageType</key>
|
||||
<string>BNDL</string>
|
||||
<key>CFBundleShortVersionString</key>
|
||||
<string>1.0</string>
|
||||
<key>CFBundleVersion</key>
|
||||
<string>1</string>
|
||||
<key>NSAppTransportSecurity</key>
|
||||
<dict>
|
||||
<key>NSAllowsArbitraryLoads</key>
|
||||
<true/>
|
||||
</dict>
|
||||
</dict>
|
||||
</plist>
|
||||
Reference in New Issue
Block a user