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(flat-pages): upload avatar use v2 #2025

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
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