10 changed files with 287 additions and 20 deletions
After Width: | Height: | Size: 1.3 MiB |
Binary file not shown.
@ -0,0 +1,82 @@ |
|||
import path from "path" |
|||
import { gSuccess, gFail, uploadDir, uploadPath } from "@/util" |
|||
import { dateTimeFormat } from "@/util/util" |
|||
import {fileTypeFromFile} from 'file-type'; |
|||
const fs = require("fs") |
|||
const multiparty = require("multiparty") |
|||
|
|||
function saveFile(file) { |
|||
return new Promise(async (resolve, reject) => { |
|||
const filename = file.originalFilename |
|||
const uploadedPath = file.path |
|||
const filetype = await fileTypeFromFile(uploadedPath) |
|||
const _file = path.parse(filename) |
|||
if (filetype && (filetype.ext == "jpg" || filetype.ext == "png")) { |
|||
let _name = |
|||
_file.name + "_" + dateTimeFormat(new Date(), "yyyy_MM_dd") + "_" + new Date().getTime() + _file.ext |
|||
const dstPath = path.resolve(uploadDir, _name) |
|||
fs.rename(uploadedPath, dstPath, function (err) { |
|||
if (err) { |
|||
console.log("rename error: " + err) |
|||
reject() |
|||
} else { |
|||
resolve(path.resolve("/public/upload/"+_name)) |
|||
} |
|||
}) |
|||
} else { |
|||
fs.unlinkSync(uploadedPath) |
|||
reject(new Error(filename + "文件不是图片")) |
|||
} |
|||
}) |
|||
} |
|||
|
|||
export default function (payload) { |
|||
const form = new multiparty.Form({ |
|||
uploadDir: uploadDir, //路径需要对应自己的项目更改
|
|||
/*设置文件保存路径 */ |
|||
encoding: "utf-8", |
|||
/*编码设置 */ |
|||
maxFilesSize: 20000 * 1024 * 1024, |
|||
/*设置文件最大值 20MB */ |
|||
keepExtensions: true, |
|||
/*保留后缀*/ |
|||
}) |
|||
return new Promise(async (resolve, reject) => { |
|||
form.on("part", function (part) { |
|||
console.log(part.filename) |
|||
}) |
|||
form.on("progress", function (bytesReceived, bytesExpected) { |
|||
if (bytesExpected === null) { |
|||
return |
|||
} |
|||
|
|||
var percentComplete = (bytesReceived / bytesExpected) * 100 |
|||
console.log("the form is " + Math.floor(percentComplete) + "%" + " complete") |
|||
}) |
|||
form.parse(payload, async function (err, fields, files) { |
|||
// console.log(err, fields, files);
|
|||
|
|||
if (err) { |
|||
resolve(err.message) |
|||
return |
|||
} |
|||
const errList = [] |
|||
const fileList = [] |
|||
for (let i = 0; i < files.file.length; i++) { |
|||
const file = files.file[i] |
|||
try { |
|||
const dstPath = await saveFile(file) |
|||
fileList.push(dstPath) |
|||
} catch (error) { |
|||
errList.push(error.message) |
|||
} |
|||
} |
|||
if (errList.length) { |
|||
resolve(gFail(null, errList.join("\n"))) |
|||
return |
|||
} |
|||
// resolve(h.view("views/upload.ejs"));
|
|||
resolve([...new Set(fileList)]) |
|||
}) |
|||
}) |
|||
} |
@ -0,0 +1,125 @@ |
|||
import { auth, config, method, route, swagger, validate } from "@noderun/hapi-router" |
|||
import UploadFunc from "./_upload" |
|||
import path, { resolve } from "path"; |
|||
import { gFail, uploadDir } from "@/util"; |
|||
import { fileTypeFromFile, fileTypeFromStream } from "file-type"; |
|||
import { dateTimeFormat } from "@/util/util"; |
|||
import fs from "fs-extra"; |
|||
import { Req, Res } from "#/global"; |
|||
const multiparty = require("multiparty") |
|||
|
|||
export default class { |
|||
@config({ |
|||
payload: { |
|||
maxBytes: 20000 * 1024 * 1024, |
|||
output: "stream", |
|||
parse: false, |
|||
multipart: true, |
|||
timeout: false, |
|||
allow: ["multipart/form-data", "application/x-www-form-urlencoded"], |
|||
}, |
|||
}) |
|||
@method("POST") |
|||
@auth() |
|||
async index(request: Req, h: Res) { |
|||
const { id } = request.auth.credentials |
|||
const { filelist, fields } = await Save(request.payload) |
|||
const result = {} |
|||
if (fields["username"] && fields["username"][0]) { |
|||
result["username"] = fields["username"][0] |
|||
} |
|||
if (filelist && filelist[0]) { |
|||
result["avatar"] = filelist[0] |
|||
} |
|||
if (JSON.stringify(result) !== "{}") { |
|||
const UserModel = request.getModel("user") |
|||
UserModel.update(result, { where: { id } }) |
|||
} |
|||
return h.redirect("/user") |
|||
} |
|||
} |
|||
|
|||
function saveFile(file) { |
|||
return new Promise(async (resolve, reject) => { |
|||
const filename = file.originalFilename |
|||
const uploadedPath = file.path |
|||
const filetype = await fileTypeFromFile(uploadedPath) |
|||
const _file = path.parse(filename) |
|||
if (filetype && (filetype.ext == "jpg" || filetype.ext == "png")) { |
|||
let _name = |
|||
_file.name + "_" + dateTimeFormat(new Date(), "yyyy_MM_dd") + "_" + new Date().getTime() + _file.ext |
|||
const dstPath = path.resolve(uploadDir, _name) |
|||
fs.rename(uploadedPath, dstPath, function (err) { |
|||
if (err) { |
|||
console.log("rename error: " + err) |
|||
reject() |
|||
} else { |
|||
resolve(path.resolve("/public/upload/" + _name)) |
|||
} |
|||
}) |
|||
} else { |
|||
fs.unlinkSync(uploadedPath) |
|||
reject(new Error(filename + "文件不是图片")) |
|||
} |
|||
}) |
|||
} |
|||
|
|||
function Save(payload) { |
|||
const form = new multiparty.Form({ |
|||
uploadDir: uploadDir, //路径需要对应自己的项目更改
|
|||
/*设置文件保存路径 */ |
|||
encoding: "utf-8", |
|||
/*编码设置 */ |
|||
maxFilesSize: 20000 * 1024 * 1024, |
|||
/*设置文件最大值 20MB */ |
|||
keepExtensions: true, |
|||
/*保留后缀*/ |
|||
}) |
|||
return new Promise<any>(async (resolve, reject) => { |
|||
form.on("part", function (part) { |
|||
console.log(1111) |
|||
console.log(part.filename) |
|||
}) |
|||
form.on("progress", function (bytesReceived, bytesExpected) { |
|||
if (bytesExpected === null) { |
|||
return |
|||
} |
|||
|
|||
// var percentComplete = (bytesReceived / bytesExpected) * 100
|
|||
// console.log("the form is " + Math.floor(percentComplete) + "%" + " complete")
|
|||
}) |
|||
form.parse(payload, async function (err, fields, files) { |
|||
// console.log(err, fields, files);
|
|||
if (err) { |
|||
reject(err.message) |
|||
return |
|||
} |
|||
const errList = [] |
|||
const fileList = [] |
|||
if (files && files.file && files.file.length) { |
|||
for (let i = 0; i < files.file.length; i++) { |
|||
const file = files.file[i] |
|||
if (file.originalFilename === "" && file.size === 0) { |
|||
const uploadedPath = file.path |
|||
fs.unlinkSync(uploadedPath) |
|||
continue |
|||
} |
|||
try { |
|||
const dstPath = await saveFile(file) |
|||
fileList.push(dstPath) |
|||
} catch (error) { |
|||
errList.push(error.message) |
|||
} |
|||
} |
|||
} |
|||
if (errList.length) { |
|||
resolve(gFail(null, errList.join("\n"))) |
|||
return |
|||
} |
|||
resolve({ |
|||
fields, |
|||
filelist: [...new Set(fileList)] |
|||
}) |
|||
}) |
|||
}) |
|||
} |
Loading…
Reference in new issue