Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(v2): return avatar url when finish #779

Merged
merged 1 commit into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/v2/controllers/user/upload-avatar/finish.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Type } from "@sinclair/typebox";
import { FastifyRequestTypebox, Response } from "../../../../types/Server";
import { UserUploadAvatarService } from "../../../services/user/upload-avatar";
import { UserUploadAvatarFinishReturn } from "../../../services/user/upload-avatar.type";
import { successJSON } from "../../internal/utils/response-json";

export const userUploadAvatarFinishSchema = {
Expand All @@ -13,14 +14,14 @@ export const userUploadAvatarFinishSchema = {

export const userUploadAvatarFinish = async (
req: FastifyRequestTypebox<typeof userUploadAvatarFinishSchema>,
): Promise<Response> => {
): Promise<Response<UserUploadAvatarFinishReturn>> => {
const userUploadAvatarSVC = new UserUploadAvatarService(
req.ids,
req.DBTransaction,
req.userUUID,
);

await userUploadAvatarSVC.finish(req.body.fileUUID);
const result = await userUploadAvatarSVC.finish(req.body.fileUUID);

return successJSON({});
return successJSON(result);
};
8 changes: 6 additions & 2 deletions src/v2/services/user/upload-avatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createLoggerService, parseError } from "../../../logger";
import { EntityManager } from "typeorm";
import {
GetAvatarInfoByRedisReturn,
UserUploadAvatarFinishReturn,
UserUploadAvatarStartConfig,
UserUploadAvatarStartReturn,
} from "./upload-avatar.type";
Expand Down Expand Up @@ -62,7 +63,7 @@ export class UserUploadAvatarService {
};
}

public async finish(fileUUID: string): Promise<void> {
public async finish(fileUUID: string): Promise<UserUploadAvatarFinishReturn> {
const { fileName } = await this.getFileInfoByRedis(fileUUID);

const ossFilePath = CloudStorageUploadService.generateOSSFilePath(fileName, fileUUID);
Expand All @@ -75,7 +76,8 @@ export class UserUploadAvatarService {
const { avatarURL: oldAvatarURL } = await userInfoSVC.basicInfo();

const userUpdateSVC = new UserUpdateService(this.ids, this.DBTransaction, this.userUUID);
await userUpdateSVC.avatarURL(`${this.oss.domain}/${ossFilePath}`);
const newAvatarURL = `${this.oss.domain}/${ossFilePath}`;
await userUpdateSVC.avatarURL(newAvatarURL);

if (oldAvatarURL.startsWith(this.oss.domain)) {
this.oss.remove(oldAvatarURL).catch(error => {
Expand All @@ -100,6 +102,8 @@ export class UserUploadAvatarService {
});
},
);

return { avatarURL: newAvatarURL };
}

public async getFileInfoByRedis(fileUUID: string): Promise<GetAvatarInfoByRedisReturn> {
Expand Down
4 changes: 4 additions & 0 deletions src/v2/services/user/upload-avatar.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ export interface UserUploadAvatarStartReturn {
export interface GetAvatarInfoByRedisReturn {
fileName: string;
}

export interface UserUploadAvatarFinishReturn {
avatarURL: string;
}