【问题标题】:Swift Vapor 4 upload , validate , resize an image fileSwift Vapor 4 上传、验证、调整图像文件大小
【发布时间】:2022-01-12 22:20:21
【问题描述】:

我正在尝试将照片发布到 steam 4 服务器。 我将团队名称作为字符串发送,将图像作为数据发送。

struct SendTeam: Content {
    var name: String
    var img: Data
}

我想在验证照片大小不超过 1MB 后上传照片,并且 mimetype 是 (jpg, jpeg, png) 之类的图像类型,然后将该图像调整为 300px*300px,最后将其保存到 @ 987654324@目录。

我不知道该怎么做。

这是我的代码。

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 云实例上运行。

【问题讨论】:

标签: swift vapor swift-nio


【解决方案1】:

经过两天的测试,我可以使用SwiftGD 做到这一点,所以我想出了这个..希望它有用。

图像验证

// Do not forget to decode the image to File type Not Data type
let img = team.img

if img.data.readableBytes > 1000000  {
    errors.append( "error ... image size should not exceed 1 mb")
}

if !["png", "jpeg", "jpg"].contains(img.extension?.lowercased()) {
    errors.append("extension is not acceptable")
}

    let imageNewNameAndExtension = "\(UUID())"+".\(img.extension!.lowercased())"

上传调整大小的部分

// The upload Path
        let path = req.application.directory.publicDirectory + "uploads/" + imageNewNameAndExtension
// The path to save the resized img
        let newPath = req.application.directory.publicDirectory + "uploads/teams/" + imageNewNameAndExtension
        
        // SwiftNIO File handle
        let handle = try await req.application.fileio.openFile(path: path,mode: .write,flags:.allowFileCreation(posixMode:0x744),eventLoop: req.eventLoop).get()
        
        // Save the file to the server
    req.application.fileio.write(fileHandle:handle,buffer:img.data,eventLoop: req.eventLoop).whenSuccess { _ in
// SwiftGD part to resize the image
            let url = URL(fileURLWithPath: path)
            let newUrl = URL(fileURLWithPath: newPath)
            let image = Image(url: url)
            if let im = image {
                if let mg = im.resizedTo(width: 250, height: 250){
                    mg.write(to: newUrl)
                }
            }
            
            try? handle.close()
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多