I want to make an iOS app using Vapor 3 as my backend. The model I am creating to represent my object contains some properties that will be files such as a .png and .plist files. I'm having trouble understanding how to use multipart in order to grab those files and send them to my model endpoint whenever I do a POST request.
I'm also confused on what data type I should be setting those file properties to be in my Model class. In the multipart docs (https://docs.vapor.codes/3.0/multipart/overview/#content) under the "Content" section, they say to make a Struct and they set their image to be of type Data, but also say you can make it of type File. I've also seen examples where they make it of type String.
I was hoping someone could clarify what exactly I should set those properties' data types to be and how I could upload those files in my Controllers/ModelController where I do the saving and call .post() in my boot(router: Router) function
I have already looked through the Multipart vapor docs and read through these stackoverflow posts but still don't understand what I should be doing when I try to use the post method: - Vapor upload multiple files at once - How to handle multipart request with Vapor 3 - Image Upload in Vapor 3 using PostgreSQL
This is my model class:
import Vapor
import FluentMySQL
final class AppObject: Codable {
var id: Int?
var plistFile: String // file
var imageFile: String // file
var notes: String
var name: String
init(ipaFile: String, plistFile: String, imageFile: String, notes: String, name: String) {
self.ipaFile = ipaFile
self.plistFile = plistFile
self.imageFile = imageFile
self.notes = notes
self.name = name
}
}
extension AppObject: MySQLModel {}
extension AppObject: Content {}
extension AppObject: Migration {}
extension AppObject: Parameter {}
This is my Controller for the above model:
import Vapor
import Fluent
struct AppObjectsController: RouteCollection {
func boot(router: Router) throws {
let appObjectsRoute = router.grouped("api", "apps")
appObjectsRoute.get(use: getAllHandler)
appObjectsRoute.post(AppObject.self, use: createHandler)
}
func getAllHandler(_ req: Request) throws -> Future<[AppObject]> {
return AppObject.query(on: req).all()
}
// what else should I be doing here in order to upload actual files?
func createHandler(_ req: Request, appobject: AppObject) throws -> Future<AppObject> {
return appobject.save(on: req)
}
}
some of the examples i've seen deal with uploading for a web app and they return a Future< View > but since I am doing an iOS app, I don't know if I should be returning an HTTPResponseStatus or my model object.
Please help, I tried my best to word this well, I am new to Vapor