我正在尝试将照片发布到蒸汽 4 服务器。我将团队名称作为字符串发送,将图像作为数据发送。
struct SendTeam: Content {
var name: String
var img: Data
}
我想在验证它的大小不超过 1MB 后上传照片,并且 mimetype 是图像类型(jpg,jpeg,png),然后将该图像调整为 300px*300px,最后将其保存到public\uploads
目录中。
我不知道该怎么做。
这是我的代码。
func create(req: Request) async throws -> SendTeam {
let team = try req.content.decode(SendTeam.self)
let path = req.application.directory.publicDirectory + "originals/" + team.name + "-\(UUID())"
try await req.fileio.writeFile(.init(data: team.img), at: path)
if team.name.count < 4 || team.name.count > 20 {
throw Abort(.badRequest, reason: "wrong name")
}
return team
}
代码也应该适用于 ubuntu 服务器 VPS 云实例。