修正头像上传

This commit is contained in:
2022-02-19 05:58:58 +08:00
parent b060ea05a9
commit 9914560a22
8 changed files with 516 additions and 31 deletions

61
plugins/GravatarCache.js Normal file
View File

@@ -0,0 +1,61 @@
/**
* Gravatar 头像缓存插件
*/
import md5 from 'md5'
import path from 'path'
import request from 'request'
import { createWriteStream, existsSync, mkdirSync } from 'fs'
const 镜像站列表 = [
'gravatar.loli.net', // loli.net
'sdn.geekzu.org', // 极客族公共加速服务
'cravatar.cn', // Cravatar - 中国的公共头像服务
'gravatar.inwao.com', // 「公益」Gravatar 韩国免费高速镜像源|支持负载均衡 - inwao blog
'gravatar.loli.top', // 自建Gravatar国内+全球镜像加速服务 | 城南旧事 (loli.top)
'gravatar.zeruns.tech', // Gravatar头像介绍 镜像地址大全 - Zeruns's Blog
'secure.gravatar.com', // Gravatar 官网
]
var 头像来源 = 0
function getGravatar(email, size) {
let str = email ? md5(email.toLowerCase()) : 'default'
let 本地路径 = './data/avatar/' + str + '.jpg'
let 网络路径 = `https://${镜像站列表[头像来源]}/avatar/${str}.jpg?s=${size}&d=mm&r=g`
if (!existsSync(本地路径)) {
request(网络路径).on('error', function (err) {
头像来源++
if (镜像站列表.length <= 头像来源) {
return console.log("下载头像失败, 使用默认头像")
}
getGravatar(email, size)
}).pipe(createWriteStream(本地路径))
}
}
function 检查并创建目录(dirname) {
if (existsSync(dirname)) {
return true
} else if (检查并创建目录(path.dirname(dirname))) {
mkdirSync(dirname)
return true
}
return fasle
}
export default {
// 激活插件方法
activate() {
检查并创建目录('./data/avatar/')
},
// 禁用插件方法
deactivate() {
// 停用插件并删除目录
},
// 向头像获取方法插入替换
getGravatar, // 获取头像(如果不存在自动从网络下载)
}

15
plugins/comment.js Normal file
View File

@@ -0,0 +1,15 @@
/**
* 博客评论插件
*/
export default class {
constructor() {
// 初始化
}
// 发表一条评论
// 获取某个主题的评论列表时, 隐藏未经审核的 SID
// 在本地生成一个会话sid(非时间种子随机码), 发送评论时附上, 以及邮箱
// 则服务端对相同的会话sid(已审核的)直接放行显示
// 否则不放行
}

59
plugins/file.js Normal file
View File

@@ -0,0 +1,59 @@
const FileListTemp = new Map()
export default {
// 上传图像时
upload(req, res) {
formidable({
multiples: true,
uploadDir: 'data/file',
keepExtensions: true,
maxFieldsSize: 200 * 1024 * 1024
}).parse(req, (err, fields, files) => {
let data = {}
// 先处理图像的
let image = files['image']
if (image) {
data.image = [];
(Array.isArray(image) ? image : [image]).forEach(item => {
file_temp_list.set(item.newFilename, item) // 每帧图像记录到临时表
data.image.push(item)
})
}
// 其它类型的文件另行处理
let file = files['file']
if (file) {
data.file = [];
(Array.isArray(file) ? file : [file]).forEach(item => {
file_temp_list.set(item.newFilename, item)
data.file.push(item)
})
}
// 返回总记录
res.json(data)
})
},
// 引用图像时
// 返回文件记录, 并从 map 中移除
// 将文件记录到对象的引用列表
quote(filename) {
let file = FileListTemp.get(filename)
if (file) FileListTemp.delete(filename)
return file
}
// 然后只需要定期清理未被引用的过期记录
}
// 终端的对象操作全部ID化, 因而不必有任何其它牵涉
// 如在设置头像时, 直接指定 account/avatar = image/id
// 剩余事务均由后端处理
// patch: /api/account/avatar
// { }
// 获取本账户上传的所有头像
// 删除本账户上传的指定头像
// 设置头像为指定头像

7
plugins/image.js Normal file
View File

@@ -0,0 +1,7 @@
export default {
// 任意用户上传图像, 但仅被缓存一段时间
// 当提交引用不存在的图像时, 移除引用
// 当提交引用已被引用的图像时, 增加引用计数
}