-
Notifications
You must be signed in to change notification settings - Fork 829
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
478 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
desktop/renderer-app/src/pages/UserSettingPage/GeneralSettingPage/UploadAvatar.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import Axios from "axios"; | ||
import React, { ChangeEvent, useContext, useRef, useState } from "react"; | ||
import classNames from "classnames"; | ||
import { Region } from "flat-components"; | ||
import { observer } from "mobx-react-lite"; | ||
import { useTranslation } from "react-i18next"; | ||
|
||
import { GlobalStoreContext } from "../../../components/StoreProvider"; | ||
import { useSafePromise } from "../../../utils/hooks/lifecycle"; | ||
import { CLOUD_STORAGE_OSS_ALIBABA_CONFIG } from "../../../constants/process"; | ||
import { uploadAvatarFinish, uploadAvatarStart } from "../../../api-middleware/flatServer"; | ||
import { globalStore } from "../../../stores/global-store"; | ||
|
||
export interface UploadAvatarProps { | ||
fileRef?: React.MutableRefObject<File | undefined>; | ||
} | ||
|
||
export function useFileRef(): React.MutableRefObject<File | undefined> { | ||
return useRef<File>(); | ||
} | ||
|
||
export const UploadAvatar = observer<UploadAvatarProps>(function UploadAvatar({ fileRef }) { | ||
const globalStore = useContext(GlobalStoreContext); | ||
const sp = useSafePromise(); | ||
const { t } = useTranslation(); | ||
|
||
const [loading, setLoading] = useState(false); | ||
const [imageUrl, setImageUrl] = useState(globalStore.userInfo?.avatar || ""); | ||
|
||
const updateInput = (event: ChangeEvent<HTMLInputElement>): void => { | ||
const file: File | undefined = (event.target.files || [])[0]; | ||
if (fileRef) { | ||
fileRef.current = file; | ||
} | ||
if (file) { | ||
setLoading(true); | ||
sp(fileToDataUrl(file)).then(url => { | ||
setLoading(false); | ||
setImageUrl(url); | ||
}); | ||
} else { | ||
setImageUrl(globalStore.userInfo?.avatar || ""); | ||
} | ||
}; | ||
|
||
return ( | ||
<div | ||
className={classNames("general-setting-user-avatar", { | ||
"is-loading": loading, | ||
})} | ||
> | ||
<input className="user-avatar-input" type="file" onChange={updateInput} /> | ||
{imageUrl ? ( | ||
<img alt="avatar" className="user-avatar-image" src={imageUrl} /> | ||
) : ( | ||
<span className="user-avatar-text">{t("upload-avatar")}</span> | ||
)} | ||
</div> | ||
); | ||
}); | ||
|
||
function fileToDataUrl(file: File): Promise<string> { | ||
return new Promise<string>((resolve, reject) => { | ||
const reader = new FileReader(); | ||
reader.onload = () => resolve(reader.result as string); | ||
reader.onerror = reject; | ||
reader.readAsDataURL(file); | ||
}); | ||
} | ||
|
||
export async function uploadAvatar(file: File): Promise<void> { | ||
const ticket = await uploadAvatarStart( | ||
file.name, | ||
file.size, | ||
globalStore.region ?? Region.CN_HZ, | ||
); | ||
|
||
const formData = new FormData(); | ||
const encodedFileName = encodeURIComponent(file.name); | ||
formData.append("key", ticket.filePath); | ||
formData.append("name", file.name); | ||
formData.append("policy", ticket.policy); | ||
formData.append("OSSAccessKeyId", CLOUD_STORAGE_OSS_ALIBABA_CONFIG.accessKey); | ||
formData.append("success_action_status", "200"); | ||
formData.append("callback", ""); | ||
formData.append("signature", ticket.signature); | ||
formData.append( | ||
"Content-Disposition", | ||
`attachment; filename="${encodedFileName}"; filename*=UTF-8''${encodedFileName}`, | ||
); | ||
formData.append("file", file); | ||
|
||
await Axios.post(ticket.policyURL, formData, { | ||
headers: { | ||
"Content-Type": "multipart/form-data", | ||
}, | ||
}); | ||
|
||
const { avatarURL } = await uploadAvatarFinish(ticket.fileUUID); | ||
|
||
globalStore.updateUserAvatar(avatarURL); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.