ts: lib/imageRouter/filesystem.js

Signed-off-by: Raccoon <raccoon@hackmd.io>
This commit is contained in:
Raccoon 2021-06-12 05:23:22 +08:00
parent 8ae51aadef
commit 8222f1fad9
No known key found for this signature in database
GPG Key ID: 06770355DC9ECD38

View File

@ -1,17 +1,17 @@
'use strict' 'use strict'
const crypto = require('crypto') import * as fs from "fs";
const fs = require('fs') import * as path from "path";
const URL = require('url').URL import * as crypto from "crypto";
const path = require('path') import {URL} from "url";
const config = require('../config') import * as config from "../config";
const logger = require('../logger') import * as logger from "../logger";
/** /**
* generate a random filename for uploaded image * generate a random filename for uploaded image
*/ */
function randomFilename () { function randomFilename() {
const buf = crypto.randomBytes(16) const buf = crypto.randomBytes(16)
return `upload_${buf.toString('hex')}` return `upload_${buf.toString('hex')}`
} }
@ -20,7 +20,7 @@ function randomFilename () {
* pick a filename not exist in filesystem * pick a filename not exist in filesystem
* maximum attempt 5 times * maximum attempt 5 times
*/ */
function pickFilename (defaultFilename) { function pickFilename(defaultFilename) {
let retryCounter = 5 let retryCounter = 5
let filename = defaultFilename let filename = defaultFilename
const extname = path.extname(defaultFilename) const extname = path.extname(defaultFilename)
@ -34,7 +34,7 @@ function pickFilename (defaultFilename) {
throw new Error('file exists.') throw new Error('file exists.')
} }
exports.uploadImage = function (imagePath, callback) { export function uploadImage(imagePath, callback) {
if (!imagePath || typeof imagePath !== 'string') { if (!imagePath || typeof imagePath !== 'string') {
callback(new Error('Image path is missing or wrong'), null) callback(new Error('Image path is missing or wrong'), null)
return return