There is a code that accepts files through a multipart/form-data type website webform, written using Perfect. The following function processes the result:
import Foundation
import PerfectLib
import PerfectHTTP
import PerfectMustache
guard var path = request.param(name: "path") else {
httpErrorResponse(response: response, e: CMSError.BadRequest)
return
}
path = webroot + "/" + path + "/"
if let uploads = request.postFileUploads, uploads.count > 0 {
var array = [FileNode]()
for upload in uploads {
array.append(FileNode(name: upload.fileName, type: "file"))
print("Uploading to path: \(path), filename: \(upload.fileName), tmpname: \(upload.tmpFileName)")
// move file to webroot
let thisFile = File(upload.tmpFileName)
do {
let newFile = try thisFile.moveTo(path: path + upload.fileName, overWrite: true)
newFile.perms = [.rwxUser, .rxGroup, .rxOther]
} catch {
print(error)
}
}
}
struct FileNode: Encodable {
let name: String
let type: String
}
In Vapor, how can I get a list of files?