-
Notifications
You must be signed in to change notification settings - Fork 113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add zip download support #749
Changes from 3 commits
755353d
b1eb362
2f9f7e6
0e507c9
9a04a65
a1a3879
24b5a64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1188,5 +1188,77 @@ public class FilesModule { | |
} | ||
} | ||
|
||
/// Creates a zip of multiple files and folders. | ||
/// | ||
/// - Parameters: | ||
/// - name: The name of the zip file to be created | ||
/// - items: Array of files or folders to be part of the created zip | ||
/// - completion: Returns a standard ZipDownload object or an error | ||
public func createZip( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you make this private to match the implementation in Python and .Net SDKs? The idea is to offer just one method that simply does the download and sends back a "combined response." There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do! |
||
name: String, | ||
items: [ZipDownloadItem], | ||
completion: @escaping Callback<ZipDownload> | ||
) { | ||
var body: [String: Any] = [:] | ||
body["download_file_name"] = name | ||
body["items"] = items.map { $0.bodyDictWithDefaultKeys } | ||
|
||
boxClient.post( | ||
url: URL.boxAPIEndpoint("/2.0/zip_downloads", configuration: boxClient.configuration), | ||
json: body, | ||
completion: ResponseHandler.default(wrapping: completion) | ||
) | ||
} | ||
|
||
/// Creates a zip of multiple files and folders and downloads it. | ||
/// | ||
/// - Parameters: | ||
/// - name: The name of the zip file to be created | ||
/// - items: Array of files or folders to be part of the created zip | ||
/// - destinationURL: A URL for the location on device that we want to store the file once it has been downloaded | ||
/// - completion: Returns an empty response or an error | ||
public func downloadZip(name: String, items: [ZipDownloadItem], destinationURL: URL, completion: @escaping Callback<ZipDownloadStatus>) { | ||
createZip(name: name, items: items) { [weak self] result in | ||
guard let self = self else { | ||
completion(.failure(BoxSDKError(message: .instanceDeallocated("Unable to create Zip - FilesModule deallocated")))) | ||
return | ||
} | ||
|
||
switch result { | ||
case let .success(zip): | ||
sujaygarlanka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.boxClient.download( | ||
url: zip.downloadUrl, | ||
downloadDestinationURL: destinationURL | ||
) { [weak self] zipDownloadResult in | ||
guard let self = self else { | ||
completion(.failure(BoxSDKError(message: .instanceDeallocated("Unable to download Zip - FilesModule deallocated")))) | ||
return | ||
} | ||
|
||
switch zipDownloadResult { | ||
case .success: | ||
self.getZipDownloadStatus(statusURL: zip.statusUrl, completion: completion) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to combine this with zip.nameConflicts, returned from the call to createZip, so that callers to downloadZip get back both the download status AND the name conflicts (since we made createZip private). You can follow the example in .Net. |
||
case let .failure(error): | ||
completion(.failure(error)) | ||
} | ||
} | ||
case let .failure(error): | ||
completion(.failure(error)) | ||
} | ||
} | ||
} | ||
|
||
/// Gets zip download status | ||
/// | ||
/// - Parameters: | ||
/// - statusURL: The URL to monitor the status of the zip download | ||
/// - completion: Returns a standard ZipDownloadStatus object or an error | ||
private func getZipDownloadStatus(statusURL: URL, completion: @escaping Callback<ZipDownloadStatus>) { | ||
boxClient.get( | ||
url: statusURL, | ||
completion: ResponseHandler.default(wrapping: completion) | ||
) | ||
} | ||
|
||
// swiftlint:disable:next file_length | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// ZipDownload.swift | ||
// BoxSDK-iOS | ||
// | ||
// Created by Skye Free on 1/11/21. | ||
// Copyright © 2021 box. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Defines a Zip Download | ||
public class ZipDownload: BoxModel { | ||
// MARK: - Properties | ||
|
||
public private(set) var rawData: [String: Any] | ||
|
||
/// The URL to download the Zip file. If entered in a browser, this URL will trigger a download of the Zip file. | ||
public let downloadUrl: URL | ||
/// The URL to monitor the status of the download. | ||
public let statusUrl: URL | ||
/// Expiration date of the Zip file download. | ||
public let expiresAt: Date? | ||
/// Conflicts that occur between items that have the same name. | ||
public let nameConflicts: [ZipDownloadConflict]? | ||
|
||
|
||
/// Initializer. | ||
/// | ||
/// - Parameter json: JSON dictionary. | ||
/// - Throws: Decoding error. | ||
public required init(json: [String: Any]) throws { | ||
rawData = json | ||
downloadUrl = try BoxJSONDecoder.decodeURL(json: json, forKey: "download_url") | ||
statusUrl = try BoxJSONDecoder.decodeURL(json: json, forKey: "status_url") | ||
expiresAt = try BoxJSONDecoder.optionalDecodeDate(json: json, forKey: "expires_at") | ||
nameConflicts = try BoxJSONDecoder.optionalDecodeZipCollection(json: json, forKey: "name_conflicts") | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// ZipDownloadConflict.swift | ||
// BoxSDK-iOS | ||
// | ||
// Created by Skye Free on 1/19/21. | ||
// Copyright © 2021 box. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Defines a Zip Download Conflict | ||
public class ZipDownloadConflict: BoxModel { | ||
// MARK: - Properties | ||
|
||
public private(set) var rawData: [String: Any] | ||
|
||
/// Conflict that occurs between items that have the same name. | ||
public let conflict: [ZipDownloadConflictItem]? | ||
|
||
/// Initializer. | ||
/// | ||
/// - Parameter json: JSON dictionary. | ||
/// - Throws: Decoding error. | ||
public required init(json: [String: Any]) throws { | ||
rawData = json | ||
conflict = try BoxJSONDecoder.optionalDecodeCollection(json: json, forKey: "conflict") | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// ZipDownloadConflictItem.swift | ||
// BoxSDK-iOS | ||
// | ||
// Created by Skye Free on 1/19/21. | ||
// Copyright © 2021 box. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Defines a Zip Download Conflict Item | ||
public class ZipDownloadConflictItem: BoxModel { | ||
|
||
// MARK: - Properties | ||
|
||
public private(set) var rawData: [String: Any] | ||
|
||
/// Identfier | ||
public let id: String | ||
/// Box item type - file or folder. | ||
public let type: String | ||
/// The original name of the item that has the conflict | ||
public let originalName: String | ||
/// The new name of the item when it downloads that resolves the conflict | ||
public let downloadName: String | ||
|
||
/// Initializer. | ||
/// | ||
/// - Parameter json: JSON dictionary. | ||
/// - Throws: Decoding error. | ||
public required init(json: [String: Any]) throws { | ||
rawData = json | ||
id = try BoxJSONDecoder.decode(json: json, forKey: "id") | ||
type = try BoxJSONDecoder.decode(json: json, forKey: "type") | ||
originalName = try BoxJSONDecoder.decode(json: json, forKey: "original_name") | ||
downloadName = try BoxJSONDecoder.decode(json: json, forKey: "download_name") | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// ZipDownloadItem.swift | ||
// BoxSDK-iOS | ||
// | ||
// Created by Skye Free on 1/12/21. | ||
// Copyright © 2021 box. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
/// Item field for creating a Zip download. | ||
public struct ZipDownloadItem: BoxInnerModel { | ||
|
||
// MARK: - Properties | ||
|
||
/// Identfier | ||
public let id: String | ||
/// Box item type - should be file or folder. | ||
public let type: String | ||
|
||
/// Initializer. | ||
/// | ||
/// - Parameters: | ||
/// - id: Identfier | ||
/// - type: Box item type - should be file or folder | ||
public init( | ||
id: String, | ||
type: String | ||
) { | ||
self.id = id | ||
self.type = type | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is cool, BTW! Good job!