mirror of
https://github.com/leonboe1/QuickDrop.git
synced 2026-11-19 21:06:10 +00:00
Fix ZIP creation for certain file types, improve UX for outgoing transfer
This commit is contained in:
1 parent
a5e1030c1f
commit
8393a7495f
9 files changed
+259
-190
No files matched your search
+25
-45
@@ -6,63 +6,43 @@
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import ZIPFoundation
|
||||
|
||||
class Zip {
|
||||
|
||||
static func createAtTemporaryDirectory(zipFilename: String, zipExtension: String = "zip", fromDirectory directoryURL: URL) throws -> URL {
|
||||
|
||||
let finalURL = FileManager.default.temporaryDirectory
|
||||
final class Zip {
|
||||
|
||||
static func createAtTemporaryDirectory(
|
||||
zipFilename: String,
|
||||
zipExtension: String = "zip",
|
||||
fromDirectory directoryURL: URL
|
||||
) throws -> URL {
|
||||
|
||||
guard directoryURL.isDirectory else { throw CreateZipError.urlNotADirectory(directoryURL) }
|
||||
|
||||
let zipFinalURL = FileManager.default.temporaryDirectory
|
||||
.appendingPathComponent(zipFilename)
|
||||
.appendingPathExtension(zipExtension)
|
||||
|
||||
return try createAt(
|
||||
zipFinalURL: finalURL,
|
||||
fromDirectory: directoryURL
|
||||
|
||||
if FileManager.default.fileExists(atPath: zipFinalURL.path) {
|
||||
try FileManager.default.removeItem(at: zipFinalURL)
|
||||
}
|
||||
|
||||
try FileManager.default.zipItem(
|
||||
at: directoryURL,
|
||||
to: zipFinalURL,
|
||||
shouldKeepParent: true,
|
||||
compressionMethod: .deflate
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
static func createAt(zipFinalURL: URL, fromDirectory directoryURL: URL) throws -> URL {
|
||||
|
||||
guard directoryURL.isDirectory else {
|
||||
throw CreateZipError.urlNotADirectory(directoryURL)
|
||||
}
|
||||
|
||||
var fileManagerError: Swift.Error?
|
||||
var coordinatorError: NSError?
|
||||
let coordinator = NSFileCoordinator()
|
||||
|
||||
coordinator.coordinate(readingItemAt: directoryURL, options: .forUploading, error: &coordinatorError) { zipCreatedURL in
|
||||
do {
|
||||
// clean if file exists
|
||||
if FileManager.default.fileExists(atPath: zipFinalURL.path) {
|
||||
try FileManager.default.removeItem(at: zipFinalURL)
|
||||
}
|
||||
|
||||
try FileManager.default.moveItem(at: zipCreatedURL, to: zipFinalURL)
|
||||
|
||||
} catch {
|
||||
fileManagerError = error
|
||||
}
|
||||
}
|
||||
|
||||
if let error = coordinatorError ?? fileManagerError {
|
||||
throw CreateZipError.failedToCreateZIP(error)
|
||||
}
|
||||
|
||||
return zipFinalURL
|
||||
}
|
||||
|
||||
|
||||
enum CreateZipError: Swift.Error {
|
||||
enum CreateZipError: Error {
|
||||
case urlNotADirectory(URL)
|
||||
case failedToCreateZIP(Swift.Error)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension URL {
|
||||
private extension URL {
|
||||
var isDirectory: Bool {
|
||||
(try? resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true
|
||||
(try? resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user