Uploading a photo post
Before Rocket 0.5, uploading multipart files had to be implemented manually, as in the previous section. Starting from Rocket 0.5, there's a rocket::fs::TempFile
type that can be used directly to handle uploaded files.
To handle processing image files, we can use an image
crate. The crate can handle opening and saving various image file formats. The image
crate also provides ways to manipulate the image.
Websites process uploaded media files such as images for various reasons, including reducing disk usage. Some websites reduce the image quality and encode the uploaded images into file format with a default smaller size. In this example, we are going to convert all uploaded images into JPEG files with 75% quality.
Let's implement uploading image files using the image
crate and the TempFile
struct by following these steps:
- Remove
multer
andtokio-util
fromCargo.toml
. Then, add theimage
crate toCargo.toml
:image = "0.24.0"...