I have a React user interface with a NodeJS express backend, through which users can upload files. To prevent tampering, I would like to generate a hash code for the file contents prior to the file upload, send that with the file to the express upload route, compute a hash code for the uploaded contents and then compare the two hash codes to ensure that the file contents are the same (within the limits of hash code uniqueness).
I'm able generate an MD5 hash code on the contents in the React UI and send that along with the file to the back end. But I can't figure out how to take the uploaded file contents and create a hash code for that.
I'm using Multer and tried Multer-MD5 as well as writing my own using CryptoJS. I suspect the issue lies in trying to convert the uploaded content (which seems to be a buffer) into something CryptoJS can process.
Or use another Hash generator.
Has anyone had to work through this before?
I worked out a solution, partly because I expect the uploaded content to be text.:
utils.fileHash(file.buffer.toString())
and
fileHash: (file) => {
return CryptoJS.MD5(file).toString();
},
Related
I have a client in React that sends a form data with a file. When that file arrives to the server, the body is parsed by body parser and its result is a buffer. The idea is that the file keep saved in some place of my server, because I want to use it later from my client. So I'd like to know how should I handle this problem.
I tried to write directly this buffer as a file with fs, but the file created has an error of format, so I can't access it.
You can do stuff like this
var fs = require('fs');
fs.writeFile('newImage', req.files.image, function (err) {
if (err) throw err;
console.log("It's saved");
});
correct parameter order of fs.writeFile is the filename first then the content.
If you're using express.bodyParser() you'll have the uploaded files in the req.files field.
And of course you should write the callback:
POV: Your image file should be in req.files not the body.
I think you need a package for storing a file on your backend service. I had used morgan package for that and I was satisfied with using it. I have just searched other packages for storing a file, i found express-fileupload package. Maybe you want to look at how to use those. If you want to store a file, using the third package would be better for you and for your effort.
I have a Node.js backend that sends an icon image to a React frontend. Initially, I encode this image as a Base64 string and store it directly in the database. I created an api that returns this string to the frontend. However, I read that it is bad to store it in the database, so I re-wrote the api to store this string in the server as a json file and now my api returns the absolute path to this json file. However, how is the React frontend supposed to retrieve the file? Is it possible to use the absolute path or do I have to create another api to return a file object ? If so, how do I do that?
I'm a newbie at both react and node.js so any help is appreciated.Thanks.
EDIT: I stored the file path as __dirname of where my code resides + filename.I'm getting cors error when accessing that url
Use Express to share out that folder as such-and-such.
Suppose you keep your files in a folder called files, just off the root. And suppose your server structure looks roughly like this:
- backend
- app.js
- frontend
- Components
- Routes
- public
- dev
- index.html (your template file)
- files
- myimage.png
In your root js file (app.js? server.js? index.js?):
const app = express()
app.use('/static', express.static('public'));
app.use('/files', express.static('public/files'));
Then, in the frontend code:
<img src='/files/myimage.png' />
By the way, there is no problem storing images as base64 in your database... but every database backup will also backup those images. If you have a small database, no worries. However, if you have a LOT of images, especially very large images, this will make your database (and backups) unnecessarily large and unwieldy.
Everything depends on your use case.
I have read a lot of examples/tutorials on internet that explain how to upload and store files in PostgreSQL using NodeJS, Multer, Sequelize, Express, ...
With none of them I have understood how to do it in my project.
I have an Angular app where I have an input file, I have an API in NodeJS using pg-promise to link with my PostgreSQL database. I have done all my project api with pg-promise so I would like to use it and no Sequelize.
In the database I have read that the best is probably the BYTEA type for files like jpg, png, pdf, docx...
Here is my API hierarchy :
www file contains code to initiate the server
app.js file contains code to initiate express app and the require([library]) of what I need
index.js file contains code to link urls to function that are stored in queries.js
The only thing I want is How to store a file.
Thanks if you can help me
I strongly recommend you the use of multer library. It helps you with the storage of files in disk. You get the path where it is stored, and you could save the path on the database.
https://github.com/expressjs/multer
I am currently working on an application with the current tech stack:
Backend:
Mongoose
Express
Apollo
GraphQL
Frontend:
Vuejs
Apollo
GraphQL
I have succeeded in uploading files to the server using GraphQL, what I am stuck with is how to implement the 'download' feature. With a normal RESTApi endpoint I can use res.download(filePath) and it works. How do I do this with GraphQL since I don't want to use REST.
Or is there any other standard to go by in this scenario?
Thanks!
GraphQL uses JSON format, which represents as text format, not as binary.
If you don't want download files with REST, then you should:
Encode your file content into base64 string in the back end. Related question
Send this string as part of query response.
Save encoded base64 string as a file in the front end. Related question
But right architecture design is add a file link in the GraphQL response and use browser for downloading/rendering the file.
It's better to send a hashed & temporary link to download it
Save the file and hash the name on your static server (to limit access to other users)
The file should be temporary and should expire in a short time
Send the link of the file in response to API
I'm using Heroku to store my Express app and I can't use bodyParser, or formidable, because both uses temp files to store the posted file.
The file I'm posting is simply a large text file (csv). I don't really need to store the file anywhere, because the data will go into mongodb.
Is there anyway to parse the csv file as it is being uploaded?
Starting with Connect 2.9.0 (Express 3.4.0), bodyParser uses multiparty instead of formidable.
In multiparty's default configuration, it does not create temporary files. Read the docs to see exactly how to handle the incoming data, but you'll want to listen to the part event, which gives you a readable stream containing the uploaded file.