mirror of
https://github.com/jeffvli/feishin.git
synced 2026-05-13 20:10:07 +02:00
Add user profile image
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
import fs from 'fs';
|
||||
import { FileType } from '@prisma/client';
|
||||
import bcrypt from 'bcryptjs';
|
||||
import md5 from 'md5';
|
||||
import sharp from 'sharp';
|
||||
import { prisma } from '@lib/prisma';
|
||||
import { AuthUser } from '@middleware/authenticate';
|
||||
import { randomString, ApiError } from '@utils/index';
|
||||
import { SortOrder } from '../types/types';
|
||||
|
||||
const findById = async (user: AuthUser, options: { id: string }) => {
|
||||
const { id } = options;
|
||||
@@ -11,7 +16,11 @@ const findById = async (user: AuthUser, options: { id: string }) => {
|
||||
}
|
||||
|
||||
const uniqueUser = await prisma.user.findUnique({
|
||||
include: { serverFolderPermissions: true, serverPermissions: true },
|
||||
include: {
|
||||
files: true,
|
||||
serverFolderPermissions: true,
|
||||
serverPermissions: true,
|
||||
},
|
||||
where: { id },
|
||||
});
|
||||
|
||||
@@ -23,20 +32,23 @@ const findById = async (user: AuthUser, options: { id: string }) => {
|
||||
};
|
||||
|
||||
const findMany = async () => {
|
||||
const users = await prisma.user.findMany({});
|
||||
const users = await prisma.user.findMany({
|
||||
include: { files: true },
|
||||
orderBy: [{ isAdmin: SortOrder.DESC }, { username: SortOrder.ASC }],
|
||||
});
|
||||
return users;
|
||||
};
|
||||
|
||||
const createUser = async (
|
||||
user: AuthUser,
|
||||
options: {
|
||||
data: {
|
||||
displayName?: string;
|
||||
isAdmin?: boolean;
|
||||
password: string;
|
||||
username: string;
|
||||
}
|
||||
) => {
|
||||
const { password, username, displayName, isAdmin } = options;
|
||||
const { password, username, displayName, isAdmin } = data;
|
||||
|
||||
if (isAdmin && !user.isSuperAdmin) {
|
||||
throw ApiError.badRequest('You are not authorized to create an admin.');
|
||||
@@ -91,18 +103,81 @@ const updateUser = async (
|
||||
options: { userId: string },
|
||||
data: {
|
||||
displayName?: string;
|
||||
image?: Express.Multer.File | null;
|
||||
isAdmin?: boolean;
|
||||
password?: string;
|
||||
username?: string;
|
||||
}
|
||||
) => {
|
||||
const { userId } = options;
|
||||
const { username, password, isAdmin, displayName } = data;
|
||||
const { username, password, isAdmin, displayName, image } = data;
|
||||
|
||||
const hashedPassword = password && (await bcrypt.hash(password, 12));
|
||||
|
||||
let avatar: {
|
||||
fileName: string;
|
||||
path: string;
|
||||
size: number;
|
||||
type: FileType;
|
||||
} | null = null;
|
||||
|
||||
if (image) {
|
||||
const existingUser = await prisma.user.findUnique({
|
||||
include: { files: true },
|
||||
where: { id: userId },
|
||||
});
|
||||
|
||||
const existingFile = existingUser?.files.find(
|
||||
(file) => file.type === FileType.USER
|
||||
);
|
||||
|
||||
// Delete the existing file
|
||||
if (existingFile) {
|
||||
await prisma.file.delete({ where: { id: existingFile.id } });
|
||||
const filePath = `../files/${existingFile.fileName}`;
|
||||
fs.unlink(filePath, (err) => {
|
||||
if (err) console.log(err);
|
||||
});
|
||||
}
|
||||
|
||||
// Create optimized webp image and delete the original
|
||||
const avatarFilename = `${md5(randomString(12))}.webp`;
|
||||
const avatarPath = `files/${avatarFilename}`;
|
||||
const newImage = await sharp(image.buffer)
|
||||
.webp({ quality: 20 })
|
||||
.toFile(avatarPath);
|
||||
avatar = {
|
||||
fileName: avatarFilename,
|
||||
path: avatarPath,
|
||||
size: newImage.size,
|
||||
type: FileType.USER,
|
||||
};
|
||||
}
|
||||
|
||||
const user = await prisma.user.update({
|
||||
data: { displayName, isAdmin, password: hashedPassword, username },
|
||||
data: {
|
||||
displayName,
|
||||
files:
|
||||
image && avatar
|
||||
? {
|
||||
create: {
|
||||
fileName: avatar.fileName,
|
||||
originalName: image?.originalname!,
|
||||
path: avatar.path,
|
||||
size: avatar.size,
|
||||
type: avatar.type,
|
||||
},
|
||||
}
|
||||
: undefined,
|
||||
isAdmin,
|
||||
password: hashedPassword,
|
||||
username,
|
||||
},
|
||||
include: {
|
||||
files: true,
|
||||
serverFolderPermissions: true,
|
||||
serverPermissions: true,
|
||||
},
|
||||
where: { id: userId },
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user