question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How should I handle the files stored in /tmp after use?

See original GitHub issue

Support plan

  • which support plan is this issue covered by? (e.g. Community, Sponsor, or Enterprise): Community
  • is this issue currently blocking your project? (yes/no): no
  • is this issue affecting a production system? (yes/no): probably

Context

  • node version: v12.16.3
  • module (formidable) version: ^1.2.2
  • environment (e.g. node, browser, native, OS): WSL2, Ubuntu 18.04 LTS
  • used with (i.e. popular names of modules): express
  • any other relevant information:

What are you trying to achieve or the steps to reproduce?

This is more of a question than a bug. I would like to know what is the recommended way to handle the temporary files that are not in use anymore.

The temporary files that get stored in the /tmp folder don’t get deleted after finished use. So I guess I have to do it myself. So my main question is, should I delete the file even though an error occurred processing the request by parse():

formidable().parse(req, async (err, fields, files) => {
      if (!err) {
        // I'm going to remove the file here, e.g. fs.unlink(files.someFile.path, someCallback)
      } else {
         // My question is should I also do it here, or does formidable remove the file automatically when there is an error??
      }
      next();
    });


I really don’t want to risk temporary files getting piled up in production. And I know that files in /tmp folder gets deleted after a reboot, but my server will not get rebooted very often I think.

What was the result you got?

Again this issue is more of a question than a bug report.

What result did you expect?

It would be nice if formidable removed the file automatically after use, but again this issue is more of a question than a bug report.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
proup24commented, Jun 5, 2020

@AndysonDK In that case you want to manually delete the temp file right after the request gets fulfilled, that’s how I personally go about body parsing with formidable:

const asyncForm = require('./lib/asyncFormidable');
const tmp = require('tmp');

module.exports = async function bodyParser(req, res, next) {
  let tmpObj = tmp.dirSync({unsafeCleanup: true});
  let {fields, files} = await asyncForm(req, {uploadDir: tmpObj.name, keepExtensions: true})
    .catch(err => next(err))

  req.input = {fields, files};

  res.on('finish', () => {
    tmpObj.removeCallback()
  })
  res.on('error', () => {
    tmpObj.removeCallback()
  });

  next();
};

asyncFormidable.js

const formidable = require('formidable')

function formidablePromise (req, opts) {
  return new Promise(function (resolve, reject) {
    var form = new formidable.IncomingForm(opts)
    form.parse(req, function (err, fields, files) {
      if (err) return reject("Body parser: " + err)
      resolve({ fields: fields, files: files })
    })
  })
}

module.exports = formidablePromise

If you find a better solution let us know.

4reactions
tunnckoCorecommented, May 29, 2020

Ah, yea. It would be good Formidable to handle that too. But that’s for now.

Also, in future, it won’t write to disk by default so it will disappear.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Windows Temporary Files - Everything you want to know
The Temporary files created by the Windows operating system are usually stored in the %system%\Windows\Temp folder, whereas the ones created by ...
Read more >
tmp directory overview - DreamHost Knowledge Base
Web servers have a directory named /tmp used to store temporary files. Many programs use this /tmp directory for writing temporary data and ......
Read more >
Should temporary files be saved to /tmp or the current working ...
Definitely check the environment for the location of temporary files and never hard-code /tmp. Because a shared tmp has security issues, one ...
Read more >
How to Delete Temporary Files on your Windows PC - AVG
Don't let temporary files take up space, slow down your computer, ... TMP and are stored in the C:\Users\AppData\Local\Temp folder.
Read more >
How to View and Remove Temporary Program Files
Which temp files are safe to delete? ... Because all temporary files are only a location to hold information temporarily, all temporary files...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found