Skip to content

Commit

Permalink
refactor(flat-pages): upload avatar use v2 (#2025)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyrious authored Aug 25, 2023
1 parent e539ed9 commit 56a2b0b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Axios from "axios";
import React, { ChangeEvent, useContext, useEffect, useState } from "react";
import classNames from "classnames";
import { message, Region } from "flat-components";
import { message } from "flat-components";
import { observer } from "mobx-react-lite";
import { FlatI18nTFunction, useTranslate } from "@netless/flat-i18n";

Expand Down Expand Up @@ -80,15 +80,11 @@ export async function uploadAvatar(file: File, t: FlatI18nTFunction): Promise<vo
throw new Error("upload avatar size limit");
}

const ticket = await uploadAvatarStart(
file.name,
file.size,
globalStore.region ?? (globalStore.serverRegionConfig?.whiteboard.convertRegion as Region),
);
const ticket = await uploadAvatarStart(file.name, file.size);

const formData = new FormData();
const encodedFileName = encodeURIComponent(file.name);
formData.append("key", ticket.filePath);
formData.append("key", ticket.ossFilePath);
formData.append("name", file.name);
formData.append("policy", ticket.policy);
formData.append("OSSAccessKeyId", globalStore.cloudStorageAK);
Expand All @@ -101,7 +97,7 @@ export async function uploadAvatar(file: File, t: FlatI18nTFunction): Promise<vo
);
formData.append("file", file);

await Axios.post(ticket.policyURL, formData, {
await Axios.post(ticket.ossDomain, formData, {
headers: {
"Content-Type": "multipart/form-data",
},
Expand Down
13 changes: 5 additions & 8 deletions packages/flat-server-api/src/room.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RoomStatus, RoomType, Week, Region } from "./constants";
import { post } from "./utils";
import { post, postV2 } from "./utils";

export interface CreateOrdinaryRoomPayload {
title: string;
Expand Down Expand Up @@ -402,26 +402,23 @@ export async function rename(name: string): Promise<RenameResult> {
export interface UploadAvatarStartPayload {
fileName: string;
fileSize: number;
region: Region;
}

export interface UploadAvatarResult {
fileUUID: string;
filePath: string;
ossDomain: string;
ossFilePath: string;
policy: string;
policyURL: string;
signature: string;
}

export async function uploadAvatarStart(
fileName: string,
fileSize: number,
region: Region,
): Promise<UploadAvatarResult> {
return await post<UploadAvatarStartPayload, UploadAvatarResult>("user/upload-avatar/start", {
return await postV2<UploadAvatarStartPayload, UploadAvatarResult>("user/upload-avatar/start", {
fileName,
fileSize,
region,
});
}

Expand All @@ -434,7 +431,7 @@ export interface UploadAvatarFinishResult {
}

export async function uploadAvatarFinish(fileUUID: string): Promise<UploadAvatarFinishResult> {
return await post<UploadAvatarFinishPayload, UploadAvatarFinishResult>(
return await postV2<UploadAvatarFinishPayload, UploadAvatarFinishResult>(
"user/upload-avatar/finish",
{
fileUUID,
Expand Down

0 comments on commit 56a2b0b

Please sign in to comment.