fixed and improved profile pictures
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { UpdateUserResponse } from '../types';
|
||||
import { apiFetch } from './client'
|
||||
import { upload } from '@tauri-apps/plugin-upload';
|
||||
import { getAuthData } from '../authStore';
|
||||
// import { upload } from '@tauri-apps/plugin-upload';
|
||||
import { getAuthData } from '../store';
|
||||
import { API } from '../main.ts';
|
||||
|
||||
export function updateSettings(username: string, email: string, password: string) {
|
||||
@@ -12,24 +12,41 @@ export function updateSettings(username: string, email: string, password: string
|
||||
}
|
||||
|
||||
export async function uploadAvatar(
|
||||
filePath: string,
|
||||
fileData: Uint8Array,
|
||||
onProgress: (progress: number, total: number) => void
|
||||
) {
|
||||
const auth = await getAuthData();
|
||||
const url = `${API}/account/upload-avatar`;
|
||||
const headers = new Map<string, string>([
|
||||
['Authorization', `Bearer ${auth.token}`],
|
||||
['Content-Type', 'application/octet-stream']
|
||||
]);
|
||||
|
||||
return upload(
|
||||
url,
|
||||
filePath,
|
||||
({ progress, total }) => {
|
||||
onProgress(progress, total);
|
||||
},
|
||||
headers
|
||||
);
|
||||
return new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest();
|
||||
xhr.open('POST', url);
|
||||
|
||||
xhr.setRequestHeader('Authorization', `Bearer ${auth.token}`);
|
||||
xhr.setRequestHeader('Content-Type', 'application/octet-stream');
|
||||
|
||||
// Handle Progress
|
||||
if (xhr.upload && onProgress) {
|
||||
xhr.upload.onprogress = (event) => {
|
||||
if (event.lengthComputable) {
|
||||
onProgress(event.loaded, event.total);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Handle Response
|
||||
xhr.onload = () => {
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
resolve(xhr.response);
|
||||
} else {
|
||||
reject(new Error(`Upload failed with status ${xhr.status}: ${xhr.responseText}`));
|
||||
}
|
||||
};
|
||||
|
||||
xhr.onerror = () => reject(new Error('Network error during upload'));
|
||||
|
||||
xhr.send(fileData);
|
||||
});
|
||||
}
|
||||
|
||||
export function getAvatar(uuid: string): string {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { fetch } from '@tauri-apps/plugin-http';
|
||||
import { getAuthData, clearAuthData } from '../authStore'
|
||||
import { getAuthData, clearAuthData } from '../store'
|
||||
import { API } from '../main.ts'
|
||||
import router from '../router'
|
||||
|
||||
|
||||
Reference in New Issue
Block a user