GCDWebServer:如何更改服务器上WebDAV操作的文件权限?(iOS)



我目前正在尝试使用GCDWebServer来托管WKWebView在应用程序启动时请求的本地网页。我希望能够在应用程序运行时通过UIDocumentPickerViewController抓取文件,将外部文件上传到服务器。在不同的端口上使用单独的GCDWebDAVServer似乎是一个好主意。

然而,如果我试图将文件上传到WebDAV服务器,我会从响应中获得以下数据:

<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>HTTP Error 500</title></head><body><h1>HTTP Error 500: Failed moving uploaded file to &quot;/arbitrary.txt&quot;</h1><h3>[NSCocoaErrorDomain] “35B890AC-7CD2-4A10-A67F-BAED3D6C34AB-3278-000005593C778876” couldn’t be moved because you don’t have permission to access “www”. (513)</h3></body></html>

为了可读性,去掉这个位:

Failed moving uploaded file to &quot;/arbitrary.txt&quot;</h1><h3>[NSCocoaErrorDomain] “35B890AC-7CD2-4A10-A67F-BAED3D6C34AB-3278-000005593C778876” couldn’t be moved because you don’t have permission to access “www”.

在此上下文中,www是我与GCDWebServer一起使用的本地文件夹。它没有子文件夹,只有文件。

这是我在viewDidLoad:中使用的代码

let webContentUrl = Bundle.main.path(forResource: "www", ofType: nil)!
httpServer = GCDWebServer()

webDAVURL = "http://localhost:(WEBDAV_PORT)/"
webDAVServer = GCDWebDAVServer(uploadDirectory: webContentUrl)

httpServer.addGETHandler(forBasePath: "/", directoryPath: webContentUrl, indexFilename: "index.html", cacheAge: 3600, allowRangeRequests: true)
httpServer.start(withPort: HTTP_PORT, bonjourName: nil)

let options: [String: Any] = [
GCDWebServerOption_Port: WEBDAV_PORT
]
do {
try webDAVServer.start(options: options)
} catch let error {
print("Could not start WebDAV server. Reason: (error.localizedDescription)")
}

let request = URLRequest(url: URL(string: "http://localhost:(HTTP_PORT)/")!)
webView.load(request)

用于PUT请求将文件上传到WebDAV服务器的代码:

let importedFileUrl = urls.first!
do {
let data = try Data(contentsOf: importedFileUrl)
var request = URLRequest(url: URL(string: "http://localhost:(WEBDAV_PORT)/arbitrary.txt")!)
request.httpMethod = "PUT"
let task = URLSession(configuration: .ephemeral).uploadTask(with: request, from: data) { data, response, error in
print("Data: (String(describing: data))")
print("Response: (String(describing: response))")
print("Error: (String(describing: error))")
print(String(decoding: data!, as: UTF8.self))
}
task.resume()
} catch let error {
createErrorAlert(message: error.localizedDescription)
}

这与iOS 14本地网络隐私功能无关。我尝试修改Info.plist以包含新密钥,但没有任何更改。www文件夹似乎没有写入权限。

是否有我缺少的功能可以让您更改GCDWebServer中的文件权限,或者有解决方法吗?现在,我能想到的唯一解决办法是在web服务器旁边创建一个本地数据库。

不允许写入权限的原因是我直接从应用程序捆绑包中提供文件,无法写入。

我的解决方案是将该目录的内容复制到Documents目录中,然后使用WebDAV写入该目录。

最新更新