Skip to content

Commit

Permalink
Update upload demo
Browse files Browse the repository at this point in the history
  • Loading branch information
alexiscn committed Jan 2, 2020
1 parent e71c629 commit 04fe5bd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
37 changes: 29 additions & 8 deletions Source/SynologyClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
import Foundation
import Alamofire

#if os(iOS) || os(watchOS) || os(tvOS)
import MobileCoreServices
#elseif os(macOS)
import CoreServices
#endif

// https://global.download.synology.com/download/Document/DeveloperGuide/Synology_File_Station_API_Guide.pdf

public typealias SynologyCompletion<T: Codable> = (Swift.Result<T, SynologyError>) -> Void
Expand Down Expand Up @@ -119,6 +125,15 @@ public class SynologyClient {
completion(.failure(.decodeDataError(response, text)))
}
}

private func mimeType(forPathExtension pathExtension: String) -> String {
if let id = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension as CFString, nil)?.takeRetainedValue(),
let contentType = UTTypeCopyPreferredTagWithClass(id, kUTTagClassMIMEType)?.takeRetainedValue()
{
return contentType as String
}
return "application/octet-stream"
}
}

// MARK: - Public functions
Expand Down Expand Up @@ -513,12 +528,6 @@ extension SynologyClient {
var value: String
}
var parameters: [UploadParam] = []
parameters.append(UploadParam(key: "api", value: SynologyAPI.upload.rawValue))
parameters.append(UploadParam(key: "version", value: "2"))
parameters.append(UploadParam(key: "method", value: SynologyMethod.upload.rawValue))
if let sid = self.sessionid {
parameters.append(UploadParam(key: "_sid", value: sid))
}
parameters.append(UploadParam(key: "path", value: destinationFolderPath))
parameters.append(UploadParam(key: "create_parents", value: String(createParents)))

Expand All @@ -537,12 +546,24 @@ extension SynologyClient {
}
}

let url = URL(string: baseURLString().appending("webapi/entry.cgi"))!
var urlString = baseURLString().appending("webapi/entry.cgi?api=\(SynologyAPI.upload.rawValue)&method=upload&version=2")
if let sid = self.sessionid {
urlString = urlString.appending("&_sid=\(sid)")
}
let url = URL(string: urlString)!

Alamofire.upload(multipartFormData: { (formData) in
for param in parameters {
formData.append(Data(param.value.utf8), withName: param.key)
}
formData.append(data, withName: "file", fileName: filename, mimeType: "application/octet-stream")
let mimeType: String
if filename.contains(".") {
let pathExtension = String(filename.split(separator: ".").last!)
mimeType = self.mimeType(forPathExtension: pathExtension)
} else {
mimeType = "application/octet-stream"
}
formData.append(data, withName: "file", fileName: filename, mimeType: mimeType)
}, to: url) { result in
switch result {
case .failure(let error):
Expand Down
4 changes: 4 additions & 0 deletions SynologyKitExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
A3496C3B23BD93BC00130529 /* unsplash.jpg in Resources */ = {isa = PBXBuildFile; fileRef = A3496C3A23BD93BB00130529 /* unsplash.jpg */; };
A37D4B4B237931A70003B908 /* Test.json in Resources */ = {isa = PBXBuildFile; fileRef = A37D4B4A237931A70003B908 /* Test.json */; };
A3AD405D23729AFA00F38D14 /* LoginViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3AD405C23729AFA00F38D14 /* LoginViewController.swift */; };
A3AD406023729C4000F38D14 /* LoginModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3AD405F23729C4000F38D14 /* LoginModel.swift */; };
Expand All @@ -27,6 +28,7 @@

/* Begin PBXFileReference section */
9FB844C6BFB9F98500C924BC /* Pods-SynologyKitExample.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SynologyKitExample.debug.xcconfig"; path = "Target Support Files/Pods-SynologyKitExample/Pods-SynologyKitExample.debug.xcconfig"; sourceTree = "<group>"; };
A3496C3A23BD93BB00130529 /* unsplash.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = unsplash.jpg; sourceTree = "<group>"; };
A37D4B4A237931A70003B908 /* Test.json */ = {isa = PBXFileReference; lastKnownFileType = text.json; path = Test.json; sourceTree = "<group>"; };
A3AD405C23729AFA00F38D14 /* LoginViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginViewController.swift; sourceTree = "<group>"; };
A3AD405F23729C4000F38D14 /* LoginModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginModel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -72,6 +74,7 @@
A3AD405E23729B0100F38D14 /* Supporting Files */ = {
isa = PBXGroup;
children = (
A3496C3A23BD93BB00130529 /* unsplash.jpg */,
A3B1B423236A8F27001D76E2 /* Info.plist */,
A3B1B420236A8F27001D76E2 /* LaunchScreen.storyboard */,
A3B1B41B236A8F27001D76E2 /* Main.storyboard */,
Expand Down Expand Up @@ -202,6 +205,7 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
A3496C3B23BD93BC00130529 /* unsplash.jpg in Resources */,
A3B1B422236A8F27001D76E2 /* LaunchScreen.storyboard in Resources */,
A37D4B4B237931A70003B908 /* Test.json in Resources */,
A3B1B41F236A8F27001D76E2 /* Assets.xcassets in Resources */,
Expand Down
7 changes: 4 additions & 3 deletions SynologyKitExample/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ extension BrowserViewController: BrowserTableViewCellDelegate {
private func showMoreContext() {
let actionSheet = WXActionSheet(cancelButtonTitle: "Cancel")
actionSheet.add(WXActionSheetItem(title: "Upload", handler: { [weak self] _ in
//self?.uploadFile()
self?.uploadImage()
}))
actionSheet.add(WXActionSheetItem(title: "Search", handler: { [weak self] _ in
Expand All @@ -217,14 +216,16 @@ extension BrowserViewController: BrowserTableViewCellDelegate {
}

private func uploadImage() {
guard let img = UIImage(named: "swift"), let data = img.pngData(), let folder = folderPath else {
guard let url = Bundle.main.url(forResource: "unsplash", withExtension: "jpg"),
let data = try? Data(contentsOf: url),
let folder = folderPath else {
return
}

var options = SynologyClient.UploadOptions()
options.overwrite = true
options.modificationTime = Int64(Date().timeIntervalSince1970*1000)
client.upload(data: data, filename: "swift.png", destinationFolderPath: folder, createParents: true, options: options, progressHandler: { (progress) in
client.upload(data: data, filename: "unsplash.jpg", destinationFolderPath: folder, createParents: true, options: options, progressHandler: { (progress) in
print("progress: \(progress.fractionCompleted)")
}) { result in
switch result {
Expand Down
Binary file added SynologyKitExample/Supporting Files/unsplash.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 04fe5bd

Please sign in to comment.