File Uploads and Asset Management
Share:
Sails.js provides a multitude of options when it comes to handling files and asset management. Whether you need to upload files such as photos and videos or are trying to manage a library of multimedia assets, Sails.js can be your reliable sidekick. This chapter will delve into the specifics of file uploads and asset management with Sails.js by using movie-centric examples. So let's commence our blockbuster!
Let's begin with uploading files. Imagine we are developing an application for a film festival and need a function for filmmakers to upload their movies. To simplify things, we'll accept movie posters and metadata in JSON format for our example.
The first thing you need to do is install skipper
, a middleware designed specifically for handling file uploads in Sails.js. This can be done by running the following command in your terminal:
npm install skipper --save
In your MovieController.js
, you will need to create an upload
action. This may look something like this:
upload: function(req, res) {
req.file('poster').upload(function(err, uploadedFiles) {
if (err) return res.serverError(err);
return res.json({
message: 'File uploaded successfully!',
files: uploadedFiles
});
});
}
In the above action, req.file('poster')
is referring to the file that is being uploaded. poster
will be the field name that will contain the file in the form upload. The upload
function is called on req.file('poster')
to process and upload the file.
Next, let's suppose we want to record additional movie information associated with the poster. This is where req.param('info')
comes in handy. It enables us to access parameters that are not part of the URL, provided that they have been included in the body of the request.
upload: function(req, res) {
var info = req.param('info');
req.file('poster').upload(function(err, uploadedFiles) {
if (err) return res.serverError(err);
if (!info) return res.badRequest('No movie info provided');
try {
var movieInfo = JSON.parse(info);
// TODO: Store movieInfo in db along with uploadedFiles path
return res.json({
message: 'File and movie info uploaded successfully!',
files: uploadedFiles,
movie: movieInfo
});
} catch(e) {
return res.badRequest('Invalid JSON format for movie info');
}
});
}
The file and the movie information is now successfully uploaded. However, handling files doesn't end at uploading them. It also involves serving these files when requested.
For example, if you are hosting your files on your server, then they will be stored in .tmp/uploads
by default. These uploaded files are static assets and Sails.js automatically sets up your application to serve these static assets from certain directories.
In our movie application, if we want to display the movie poster to the audience, we will need to use a GET request to the location where the movie poster is stored.
getMoviePoster: function(req, res) {
var posterPath = req.param('posterPath');
if (!posterPath) return res.notFound('The poster was not found.');
var SkipperDisk = require('skipper-disk');
var fileAdapter = SkipperDisk();
// Stream the file down
fileAdapter.read(posterPath)
.on('error', function(err) {
return res.serverError(err);
})
.pipe(res);
}
In the first line above, we are retrieving the path of the movie poster that we want to serve. We are then using Skipper's disk adapter to read the file from the disk. After we read the file, we are immediately writing it to the HTTP response using the .pipe(res)
command. If an error occurs while reading, an error will be returned as a server error with the details of the error.
While your uploaded files will typically be stored in .tmp/uploads
, you might want to consider storing these files in a more permanent location because the content in .tmp/uploads
will vanish when the Sails.js server is lifted. If you're contemplating about storing assets like movie trailers or even full movies, you will likely want to consider using an external service such as Amazon S3, which can handle large files more effectively.
Remember to adapt and change the code snippets provided here to better meet your unique needs while developing your movie-based Sails.js application. With this newfound understanding of file uploads and asset management in Sails.js, you are now well-equipped to handle any feature that involves dealing files in your Sails.js applications. Happy coding, and may all your productions be a hit!
0 Comment
Sign up or Log in to leave a comment