-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSample-iOS.swift
117 lines (96 loc) · 4.54 KB
/
Sample-iOS.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
}
}
}