• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

richardgirges / express-fileupload / #333

24 May 2022 01:09AM UTC coverage: 94.551%. Remained the same
#333

push

richardgirges
1.4.0

137 of 150 branches covered (0.0%)

295 of 312 relevant lines covered (94.55%)

903.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.74
/lib/tempFileHandler.js
1
const fs = require('fs');
1✔
2
const path = require('path');
1✔
3
const crypto = require('crypto');
1✔
4
const {
5
  debugLog,
6
  checkAndMakeDir,
7
  getTempFilename,
8
  deleteFile
9
} = require('./utilities');
1✔
10

11
module.exports = (options, fieldname, filename) => {
1✔
12
  const dir = path.normalize(options.tempFileDir);
27✔
13
  const tempFilePath = path.join(dir, getTempFilename());
27✔
14
  checkAndMakeDir({ createParentPath: true }, tempFilePath);
27✔
15

16
  debugLog(options, `Temporary file path is ${tempFilePath}`);
27✔
17
 
18
  const hash = crypto.createHash('md5');
27✔
19
  let fileSize = 0;
27✔
20
  let completed = false;
27✔
21

22
  debugLog(options, `Opening write stream for ${fieldname}->${filename}...`);
27✔
23
  const writeStream = fs.createWriteStream(tempFilePath);
27✔
24
  const writePromise = new Promise((resolve, reject) => {
27✔
25
    writeStream.on('finish', () => resolve());
27✔
26
    writeStream.on('error', (err) => {
27✔
27
      debugLog(options, `Error write temp file: ${err}`);
×
28
      reject(err);
×
29
    });
30
  });
31

32
  return {
27✔
33
    dataHandler: (data) => {
34
      if (completed === true) {
119!
35
        debugLog(options, `Error: got ${fieldname}->${filename} data chunk for completed upload!`);
×
36
        return;
×
37
      }
38
      writeStream.write(data);
119✔
39
      hash.update(data);
119✔
40
      fileSize += data.length;
119✔
41
      debugLog(options, `Uploading ${fieldname}->${filename}, bytes:${fileSize}...`);
119✔
42
    },
43
    getFilePath: () => tempFilePath,
24✔
44
    getFileSize: () => fileSize,
54✔
45
    getHash: () => hash.digest('hex'),
24✔
46
    complete: () => {
47
      completed = true;
24✔
48
      debugLog(options, `Upload ${fieldname}->${filename} completed, bytes:${fileSize}.`);
24✔
49
      if (writeStream !== false) writeStream.end();
24!
50
      // Return empty buff since data was uploaded into a temp file.
51
      return Buffer.concat([]);
24✔
52
    },
53
    cleanup: () => {
54
      completed = true;
3✔
55
      debugLog(options, `Cleaning up temporary file ${tempFilePath}...`);
3✔
56
      writeStream.end();
3✔
57
      deleteFile(tempFilePath, err => (err 
3!
58
        ? debugLog(options, `Cleaning up temporary file ${tempFilePath} failed: ${err}`)
59
        : debugLog(options, `Cleaning up temporary file ${tempFilePath} done.`)
60
      ));
61
    },
62
    getWritePromise: () => writePromise
27✔
63
  };
64
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc