23 changed files with 174 additions and 412 deletions
Binary file not shown.
Before Width: | Height: | Size: 1.3 MiB |
@ -1,10 +1,10 @@ |
|||||
import { Sequelize } from "sequelize" |
import { Sequelize } from "sequelize" |
||||
import path from "path" |
import path from "path" |
||||
import { sourceDir } from "@/util" |
import { baseDir } from "@/util" |
||||
|
|
||||
export const sequelize = new Sequelize({ |
export const sequelize = new Sequelize({ |
||||
dialect: "sqlite", |
dialect: "sqlite", |
||||
storage: path.resolve(sourceDir, "./db/data.db"), |
storage: process.env.SQLITE_PATH || path.resolve(baseDir, "./data/data.db"), |
||||
// logging: false,
|
// logging: false,
|
||||
logging: loggerSQL.debug.bind(loggerSQL) // Alternative way to use custom logger, displays all messages
|
logging: loggerSQL.debug.bind(loggerSQL) // Alternative way to use custom logger, displays all messages
|
||||
}) |
}) |
@ -1,82 +0,0 @@ |
|||||
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)]) |
|
||||
}) |
|
||||
}) |
|
||||
} |
|
@ -1,149 +0,0 @@ |
|||||
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" |
|
||||
import * as bcrypt from "bcrypt" |
|
||||
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, username, nickname, email, tel } = request.auth.credentials |
|
||||
const { filelist, fields } = await Save(request.payload, request) |
|
||||
const result = {} |
|
||||
if (fields["username"] && fields["username"][0] && username !== fields["username"][0]) { |
|
||||
result["username"] = fields["username"][0] |
|
||||
} |
|
||||
if (fields["tel"] && fields["tel"][0] && tel !== fields["tel"][0]) { |
|
||||
result["tel"] = fields["tel"][0] |
|
||||
} |
|
||||
if (fields["nickname"] && fields["nickname"][0] && nickname !== fields["nickname"][0]) { |
|
||||
result["nickname"] = fields["nickname"][0] |
|
||||
} |
|
||||
if (fields["email"] && fields["email"][0] && email !== fields["email"][0]) { |
|
||||
result["email"] = fields["email"][0] |
|
||||
} |
|
||||
if (fields["password"] && fields["password"][0]) { |
|
||||
const pwd = fields["password"][0] |
|
||||
let salt = bcrypt.genSaltSync(10) |
|
||||
let pwdLock = bcrypt.hashSync(pwd, salt) |
|
||||
result["password"] = pwdLock |
|
||||
} |
|
||||
if (filelist && filelist[0]) { |
|
||||
result["avatar"] = filelist[0] |
|
||||
} |
|
||||
if (JSON.stringify(result) !== "{}") { |
|
||||
const UserModel = request.getModel("user") |
|
||||
const user = await UserModel.findOne({ where: { username: fields["username"] } }) |
|
||||
if (!!user && user.id !== id) { |
|
||||
request.yar.flash("error", "用户名已被他人占用") |
|
||||
return h.redirect("/user") |
|
||||
} |
|
||||
console.log(fields); |
|
||||
|
|
||||
await 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("/public/upload/" + _name) |
|
||||
} |
|
||||
}) |
|
||||
} else { |
|
||||
fs.unlinkSync(uploadedPath) |
|
||||
reject(new Error(filename + "文件不是图片")) |
|
||||
} |
|
||||
}) |
|
||||
} |
|
||||
|
|
||||
function Save(payload, req: Req) { |
|
||||
const AttachmentModel = req.getModel("attachment") |
|
||||
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