Skip to content

Commit

Permalink
Refactor to ESM module
Browse files Browse the repository at this point in the history
  • Loading branch information
crycode-de committed Aug 31, 2023
1 parent 6a21342 commit ac81527
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ module.exports = {
'@crycode/eslint-config-js',
],

parserOptions: {
ecmaVersion: 2021,
sourceType: 'module',
},

rules: {
'no-console': 'off',
},
Expand Down
22 changes: 11 additions & 11 deletions http-server-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
*/
'use strict';

const http = require('http');
const formidable = require('formidable');
const fsPromises = require('fs').promises;
const path = require('path');
const os = require('os');
import http from 'node:http';
import { IncomingForm } from 'formidable';
import fs from 'node:fs/promises';
import path from 'node:path';
import os from 'node:os';

let port = process.env.PORT || 8080;
let disableAutoPort = !!process.env.DISABLE_AUTO_PORT;
Expand Down Expand Up @@ -155,15 +155,15 @@ console.log(`Upload target dir is ${uploadDir}`);

/**
* Cleanup uploaded temp files.
* @param {formidable.File[] | undefined} uploads
* @param {import('formidable').File[] | undefined} uploads
*/
async function cleanupUploads (uploads) {
if (!uploads) return;

for (const file of uploads) {
if (!file) continue;
try {
await fsPromises.unlink(file.filepath);
await fs.unlink(file.filepath);
} catch (err) {
console.log(`Error removing temporary file! ${file.filepath} ${err}`);
}
Expand All @@ -178,7 +178,7 @@ server.on('request', async (req, res) => {

if (req.url === '/upload' && req.method.toLowerCase() === 'post') {
// handle upload
const form = new formidable.IncomingForm({
const form = new IncomingForm({
uploadDir: uploadTmpDir,
multiples: true,
maxFileSize: maxFileSize,
Expand Down Expand Up @@ -233,14 +233,14 @@ server.on('request', async (req, res) => {
// check if target folder exists - if not create it if folder creation is enabled
try {
// get path stats and expect an error if not exists
await fsPromises.stat(targetPath);
await fs.stat(targetPath);

} catch (err) {
// path does not exist
if (enableFolderCreation) {
console.log(`Target path ${targetPath} does not exist, creating it`);
try {
await fsPromises.mkdir(targetPath, { recursive: true });
await fs.mkdir(targetPath, { recursive: true });
} catch (err2) {
console.log(`Error creating target path! ${err2}`);
res.statusCode = 500; // Internal Server Error
Expand All @@ -261,7 +261,7 @@ server.on('request', async (req, res) => {
if (!file) continue;
const newPath = path.join(targetPath, file.originalFilename);
try {
await fsPromises.rename(file.filepath, newPath);
await fs.rename(file.filepath, newPath);
console.log(new Date().toUTCString(), '- File uploaded', newPath);
count++;
} catch (err) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "A Simple zero-configuration command-line http server for uploading files",
"bin": "bin/http-server-upload",
"main": "http-server-upload.js",
"type": "module",
"keywords": [
"http",
"upload",
Expand Down

0 comments on commit ac81527

Please sign in to comment.