How do I get the image link after uploading the image。 The image save path may be outside the main program directory。
func addProfilePictureHandler(for req: Request) async throws -> String {
let data = try req.content.decode(ImageUploadData.self)
let requestUser = try req.auth.require(User.self)
guard let user = try await User.find(requestUser.id, on: req.db) else {
throw Abort(.notFound)
}
let userID: UUID
do {
userID = try user.requireID()
} catch {
throw try await req.eventLoop.future(error: error).get()
}
let imageName = "\(userID)-\(UUID()).jpg"
// It can be a path outside the main program
let path = req.application.directory.workingDirectory + imageFolder + imageName
try await req.fileio.writeFile(.init(data: data.picture), at: path)
user.profilePicture = imageName
try await user.save(on: req.db)
// return .ok
// return image url like http://localhost:8080/xxx/xxx.jpg
}