Skip to content
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

Merged
merged 7 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions BoxSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,15 @@
9DE5D7E022EF53E30081A031 /* EventItemType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9DE5D7DF22EF53E30081A031 /* EventItemType.swift */; };
9DEA474522D384520035C959 /* GetFileSharedLink.json in Resources */ = {isa = PBXBuildFile; fileRef = 9DEA474322D380A50035C959 /* GetFileSharedLink.json */; };
9DEA474922D391220035C959 /* GetFolderSharedLink.json in Resources */ = {isa = PBXBuildFile; fileRef = 9DEA474822D391220035C959 /* GetFolderSharedLink.json */; };
B40558AE25AD28C80068784E /* ZipDownload.swift in Sources */ = {isa = PBXBuildFile; fileRef = B40558AD25AD28C80068784E /* ZipDownload.swift */; };
B40558B025AD2ABF0068784E /* ZipDownload.json in Resources */ = {isa = PBXBuildFile; fileRef = B40558AF25AD2ABF0068784E /* ZipDownload.json */; };
B40558B125AD2AFC0068784E /* ZipDownload.json in Resources */ = {isa = PBXBuildFile; fileRef = B40558AF25AD2ABF0068784E /* ZipDownload.json */; };
B40558B725AE54680068784E /* ZipDownloadStatus.swift in Sources */ = {isa = PBXBuildFile; fileRef = B40558B625AE54680068784E /* ZipDownloadStatus.swift */; };
B40558B925AE54810068784E /* ZipDownloadItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = B40558B825AE54810068784E /* ZipDownloadItem.swift */; };
B40558BB25AE57C10068784E /* ZipDownloadStatus.json in Resources */ = {isa = PBXBuildFile; fileRef = B40558BA25AE57C10068784E /* ZipDownloadStatus.json */; };
B40558BC25AE58540068784E /* ZipDownloadStatus.json in Resources */ = {isa = PBXBuildFile; fileRef = B40558BA25AE57C10068784E /* ZipDownloadStatus.json */; };
B492C0AB25B78A4500F3823D /* ZipDownloadConflict.swift in Sources */ = {isa = PBXBuildFile; fileRef = B492C0AA25B78A4500F3823D /* ZipDownloadConflict.swift */; };
B492C0AD25B78A6A00F3823D /* ZipDownloadConflictItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = B492C0AC25B78A6A00F3823D /* ZipDownloadConflictItem.swift */; };
F90888EF246DAC110002267A /* URLSessionTaskExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F90888EE246DAC110002267A /* URLSessionTaskExtension.swift */; };
F928142C233DA3AB00E42EEE /* MetadataFieldFilter.swift in Sources */ = {isa = PBXBuildFile; fileRef = F928142B233DA3AB00E42EEE /* MetadataFieldFilter.swift */; };
F9281444233E826E00E42EEE /* FullRetentionPolicyAssignment.json in Resources */ = {isa = PBXBuildFile; fileRef = F9281430233E814400E42EEE /* FullRetentionPolicyAssignment.json */; };
Expand Down Expand Up @@ -400,9 +409,9 @@
F92CF9A52356A8430046A277 /* BoxAPIAuthError.swift in Sources */ = {isa = PBXBuildFile; fileRef = F92CF9A42356A8430046A277 /* BoxAPIAuthError.swift */; };
F941067223513DD00061A3F8 /* ArrayInputStream.swift in Sources */ = {isa = PBXBuildFile; fileRef = F941067123513DD00061A3F8 /* ArrayInputStream.swift */; };
F9571FB42371F47400D30EF1 /* LoggingSpecs.swift in Sources */ = {isa = PBXBuildFile; fileRef = F9571FB22371ECCB00D30EF1 /* LoggingSpecs.swift */; };
F95A81C124606DC300C46E98 /* UIColorExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F95A81C024606DC300C46E98 /* UIColorExtension.swift */; };
F95D6F802451F6D0008B09CF /* BoxDownloadTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = F95D6F7F2451F6D0008B09CF /* BoxDownloadTask.swift */; };
F95D6F832453415A008B09CF /* BoxUploadTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = F95D6F822453415A008B09CF /* BoxUploadTask.swift */; };
F95A81C124606DC300C46E98 /* UIColorExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F95A81C024606DC300C46E98 /* UIColorExtension.swift */; };
F95D6F85245C9DFF008B09CF /* Classification.swift in Sources */ = {isa = PBXBuildFile; fileRef = F95D6F84245C9DFF008B09CF /* Classification.swift */; };
F969D37C233A7146001301FC /* StoragePoliciesModule.swift in Sources */ = {isa = PBXBuildFile; fileRef = F969D37B233A7146001301FC /* StoragePoliciesModule.swift */; };
F969D381233A71B4001301FC /* StoragePolicy.swift in Sources */ = {isa = PBXBuildFile; fileRef = F969D380233A71B4001301FC /* StoragePolicy.swift */; };
Expand Down Expand Up @@ -847,6 +856,13 @@
9DE5D7DF22EF53E30081A031 /* EventItemType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EventItemType.swift; sourceTree = "<group>"; };
9DEA474322D380A50035C959 /* GetFileSharedLink.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = GetFileSharedLink.json; sourceTree = "<group>"; };
9DEA474822D391220035C959 /* GetFolderSharedLink.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = GetFolderSharedLink.json; sourceTree = "<group>"; };
B40558AD25AD28C80068784E /* ZipDownload.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZipDownload.swift; sourceTree = "<group>"; };
B40558AF25AD2ABF0068784E /* ZipDownload.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = ZipDownload.json; sourceTree = "<group>"; };
B40558B625AE54680068784E /* ZipDownloadStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZipDownloadStatus.swift; sourceTree = "<group>"; };
B40558B825AE54810068784E /* ZipDownloadItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZipDownloadItem.swift; sourceTree = "<group>"; };
B40558BA25AE57C10068784E /* ZipDownloadStatus.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = ZipDownloadStatus.json; sourceTree = "<group>"; };
B492C0AA25B78A4500F3823D /* ZipDownloadConflict.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZipDownloadConflict.swift; sourceTree = "<group>"; };
B492C0AC25B78A6A00F3823D /* ZipDownloadConflictItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ZipDownloadConflictItem.swift; sourceTree = "<group>"; };
F90888EE246DAC110002267A /* URLSessionTaskExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLSessionTaskExtension.swift; sourceTree = "<group>"; };
F928142B233DA3AB00E42EEE /* MetadataFieldFilter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MetadataFieldFilter.swift; sourceTree = "<group>"; };
F9281430233E814400E42EEE /* FullRetentionPolicyAssignment.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = FullRetentionPolicyAssignment.json; sourceTree = "<group>"; };
Expand Down Expand Up @@ -880,9 +896,9 @@
F92CF9A42356A8430046A277 /* BoxAPIAuthError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BoxAPIAuthError.swift; sourceTree = "<group>"; };
F941067123513DD00061A3F8 /* ArrayInputStream.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArrayInputStream.swift; sourceTree = "<group>"; };
F9571FB22371ECCB00D30EF1 /* LoggingSpecs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoggingSpecs.swift; sourceTree = "<group>"; };
F95A81C024606DC300C46E98 /* UIColorExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIColorExtension.swift; sourceTree = "<group>"; };
F95D6F7F2451F6D0008B09CF /* BoxDownloadTask.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BoxDownloadTask.swift; sourceTree = "<group>"; };
F95D6F822453415A008B09CF /* BoxUploadTask.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BoxUploadTask.swift; sourceTree = "<group>"; };
F95A81C024606DC300C46E98 /* UIColorExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIColorExtension.swift; sourceTree = "<group>"; };
F95D6F84245C9DFF008B09CF /* Classification.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Classification.swift; sourceTree = "<group>"; };
F969D37B233A7146001301FC /* StoragePoliciesModule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StoragePoliciesModule.swift; sourceTree = "<group>"; };
F969D380233A71B4001301FC /* StoragePolicy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StoragePolicy.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1285,6 +1301,11 @@
F969D385233A71EF001301FC /* StoragePolicyAssignment.swift */,
F928142B233DA3AB00E42EEE /* MetadataFieldFilter.swift */,
F95D6F84245C9DFF008B09CF /* Classification.swift */,
B40558AD25AD28C80068784E /* ZipDownload.swift */,
B40558B625AE54680068784E /* ZipDownloadStatus.swift */,
B40558B825AE54810068784E /* ZipDownloadItem.swift */,
B492C0AA25B78A4500F3823D /* ZipDownloadConflict.swift */,
B492C0AC25B78A6A00F3823D /* ZipDownloadConflictItem.swift */,
);
path = Responses;
sourceTree = "<group>";
Expand Down Expand Up @@ -1430,6 +1451,8 @@
9DEA474322D380A50035C959 /* GetFileSharedLink.json */,
9DD0AA4422DDBFDD0028702B /* GetFileSharedLink_PasswordNotEnabled.json */,
9717866E22F0C9ED004A5F6D /* RemoveFileSharedLink.json */,
B40558AF25AD2ABF0068784E /* ZipDownload.json */,
B40558BA25AE57C10068784E /* ZipDownloadStatus.json */,
);
path = Files;
sourceTree = "<group>";
Expand Down Expand Up @@ -2075,10 +2098,12 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
B40558B025AD2ABF0068784E /* ZipDownload.json in Resources */,
8093D29822FA398500DB628E /* GetWebLink.json in Resources */,
8093D29A22FA399D00DB628E /* UpdateWebLink.json in Resources */,
80E5679822FB931300798E3A /* FullWebLink.json in Resources */,
80E5679F2303683100798E3A /* GetWebLinkSharedLink_PasswordNotEnabled.json in Resources */,
B40558BB25AE57C10068784E /* ZipDownloadStatus.json in Resources */,
806ACE4D22FB8E0000257353 /* GetWebLinkSharedLink.json in Resources */,
80E567B523061A5000798E3A /* RemoveWebLinkSharedLink.json in Resources */,
);
Expand All @@ -2088,6 +2113,8 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
B40558BC25AE58540068784E /* ZipDownloadStatus.json in Resources */,
B40558B125AD2AFC0068784E /* ZipDownload.json in Resources */,
22E60D6B2303207300E86A4A /* FileRepresentationState.json in Resources */,
F929D6152331AE280039E452 /* GetLegalHoldPolicies.json in Resources */,
92514E952319C9EB002D44A8 /* FullWatermark.json in Resources */,
Expand Down Expand Up @@ -2357,6 +2384,8 @@
224E1BEE22C57C8400F31F3A /* MetadataModule+Files.swift in Sources */,
225639EE22539E8F00D951B5 /* BoxSDK.swift in Sources */,
F92CF9A1235695F50046A277 /* BoxNetworkError.swift in Sources */,
B40558AE25AD28C80068784E /* ZipDownload.swift in Sources */,
B40558B725AE54680068784E /* ZipDownloadStatus.swift in Sources */,
0CCBC29A22841B0C00640FA8 /* TokenStore.swift in Sources */,
224E1BF322C57D4A00F31F3A /* MetadataModule+Templates.swift in Sources */,
225639522253897D00D951B5 /* BoxNetworkAgent.swift in Sources */,
Expand Down Expand Up @@ -2391,6 +2420,8 @@
2228B19422C0C908001F759D /* FolderUploadEmail.swift in Sources */,
0C8B8DFC2285CA2C00D8A42F /* FileLogDestination.swift in Sources */,
F95D6F802451F6D0008B09CF /* BoxDownloadTask.swift in Sources */,
B492C0AB25B78A4500F3823D /* ZipDownloadConflict.swift in Sources */,
B492C0AD25B78A6A00F3823D /* ZipDownloadConflictItem.swift in Sources */,
225639AE2253897D00D951B5 /* FoldersModule.swift in Sources */,
0C4A9D18226DBCB4002D069D /* BoxSDKConfiguration.swift in Sources */,
8053E2C622AF06FF000B42E1 /* SharedItemsModule.swift in Sources */,
Expand Down Expand Up @@ -2426,6 +2457,7 @@
97F04DAF22B96BA60034B9A3 /* TaskAssignment.swift in Sources */,
223A7F2422770A6E00C136E1 /* EncodableExtension.swift in Sources */,
223A7F2822770A6E00C136E1 /* URLComponentsExtension.swift in Sources */,
B40558B925AE54810068784E /* ZipDownloadItem.swift in Sources */,
1772A480230EBEBC00546866 /* EventContainer.swift in Sources */,
0C10B2CE22AA8D130035371D /* CollaborationItem.swift in Sources */,
97F04DB322B97E500034B9A3 /* Collaborator.swift in Sources */,
Expand Down
15 changes: 15 additions & 0 deletions Sources/Core/BoxJSONDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,21 @@ class BoxJSONDecoder {
return color
}

static func optionalDecodeZip<T>(json: [[String: Any]]) throws -> T? where T: BoxModel {
Copy link
Contributor

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!

var modelJSON: [String: Any] = [:]
modelJSON["conflict"] = json

return try decode(json: modelJSON)
}

static func optionalDecodeZipCollection<T>(json: [String: Any], forKey key: String) throws -> [T]? where T: BoxModel {
guard let modelCollectionJSON: [[[String: Any]]] = try optionalExtractJSON(json: json, key: key) else {
return nil
}

return try modelCollectionJSON.compactMap { try optionalDecodeZip(json: $0) }
}

// MARK: - Decodes

static func decode<T>(json: [String: Any]) throws -> T where T: BoxModel {
Expand Down
72 changes: 72 additions & 0 deletions Sources/Modules/FilesModule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Copy link
Contributor

Choose a reason for hiding this comment

The 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."

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
Copy link
Contributor

Choose a reason for hiding this comment

The 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
}
38 changes: 38 additions & 0 deletions Sources/Responses/ZipDownload.swift
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")
}
}
28 changes: 28 additions & 0 deletions Sources/Responses/ZipDownloadConflict.swift
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")
}
}
38 changes: 38 additions & 0 deletions Sources/Responses/ZipDownloadConflictItem.swift
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")
}
}
33 changes: 33 additions & 0 deletions Sources/Responses/ZipDownloadItem.swift
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
}
}
Loading