mirror of
https://github.com/immich-app/immich
synced 2025-11-14 17:36:12 +00:00
refactor(web): shared link key auth (#3855)
This commit is contained in:
parent
10c2bda3a9
commit
9bbef4a97b
21 changed files with 115 additions and 108 deletions
|
|
@ -3,20 +3,22 @@ import { downloadManager } from '$lib/stores/download';
|
|||
import { api, BulkIdResponseDto, AssetResponseDto, DownloadResponseDto, DownloadInfoDto } from '@api';
|
||||
import { handleError } from './handle-error';
|
||||
|
||||
export const addAssetsToAlbum = async (
|
||||
albumId: string,
|
||||
assetIds: Array<string>,
|
||||
key: string | undefined = undefined,
|
||||
): Promise<BulkIdResponseDto[]> =>
|
||||
api.albumApi.addAssetsToAlbum({ id: albumId, bulkIdsDto: { ids: assetIds }, key }).then(({ data: results }) => {
|
||||
const count = results.filter(({ success }) => success).length;
|
||||
notificationController.show({
|
||||
type: NotificationType.Info,
|
||||
message: `Added ${count} asset${count === 1 ? '' : 's'}`,
|
||||
});
|
||||
export const addAssetsToAlbum = async (albumId: string, assetIds: Array<string>): Promise<BulkIdResponseDto[]> =>
|
||||
api.albumApi
|
||||
.addAssetsToAlbum({
|
||||
id: albumId,
|
||||
bulkIdsDto: { ids: assetIds },
|
||||
key: api.getKey(),
|
||||
})
|
||||
.then(({ data: results }) => {
|
||||
const count = results.filter(({ success }) => success).length;
|
||||
notificationController.show({
|
||||
type: NotificationType.Info,
|
||||
message: `Added ${count} asset${count === 1 ? '' : 's'}`,
|
||||
});
|
||||
|
||||
return results;
|
||||
});
|
||||
return results;
|
||||
});
|
||||
|
||||
const downloadBlob = (data: Blob, filename: string) => {
|
||||
const url = URL.createObjectURL(data);
|
||||
|
|
@ -32,11 +34,11 @@ const downloadBlob = (data: Blob, filename: string) => {
|
|||
URL.revokeObjectURL(url);
|
||||
};
|
||||
|
||||
export const downloadArchive = async (fileName: string, options: DownloadInfoDto, key?: string) => {
|
||||
export const downloadArchive = async (fileName: string, options: DownloadInfoDto) => {
|
||||
let downloadInfo: DownloadResponseDto | null = null;
|
||||
|
||||
try {
|
||||
const { data } = await api.assetApi.getDownloadInfo({ downloadInfoDto: options, key });
|
||||
const { data } = await api.assetApi.getDownloadInfo({ downloadInfoDto: options, key: api.getKey() });
|
||||
downloadInfo = data;
|
||||
} catch (error) {
|
||||
handleError(error, 'Unable to download files');
|
||||
|
|
@ -61,7 +63,7 @@ export const downloadArchive = async (fileName: string, options: DownloadInfoDto
|
|||
|
||||
try {
|
||||
const { data } = await api.assetApi.downloadArchive(
|
||||
{ assetIdsDto: { assetIds: archive.assetIds }, key },
|
||||
{ assetIdsDto: { assetIds: archive.assetIds }, key: api.getKey() },
|
||||
{
|
||||
responseType: 'blob',
|
||||
signal: abort.signal,
|
||||
|
|
@ -80,7 +82,7 @@ export const downloadArchive = async (fileName: string, options: DownloadInfoDto
|
|||
}
|
||||
};
|
||||
|
||||
export const downloadFile = async (asset: AssetResponseDto, key?: string) => {
|
||||
export const downloadFile = async (asset: AssetResponseDto) => {
|
||||
const assets = [
|
||||
{
|
||||
filename: `${asset.originalFileName}.${getFilenameExtension(asset.originalPath)}`,
|
||||
|
|
@ -104,7 +106,7 @@ export const downloadFile = async (asset: AssetResponseDto, key?: string) => {
|
|||
downloadManager.add(downloadKey, size, abort);
|
||||
|
||||
const { data } = await api.assetApi.downloadFile(
|
||||
{ id, key },
|
||||
{ id, key: api.getKey() },
|
||||
{
|
||||
responseType: 'blob',
|
||||
onDownloadProgress: (event: ProgressEvent) => {
|
||||
|
|
|
|||
|
|
@ -14,10 +14,7 @@ const getExtensions = async () => {
|
|||
return _extensions;
|
||||
};
|
||||
|
||||
export const openFileUploadDialog = async (
|
||||
albumId: string | undefined = undefined,
|
||||
sharedKey: string | undefined = undefined,
|
||||
) => {
|
||||
export const openFileUploadDialog = async (albumId: string | undefined = undefined) => {
|
||||
const extensions = await getExtensions();
|
||||
|
||||
return new Promise<(string | undefined)[]>((resolve, reject) => {
|
||||
|
|
@ -34,7 +31,7 @@ export const openFileUploadDialog = async (
|
|||
}
|
||||
const files = Array.from(target.files);
|
||||
|
||||
resolve(fileUploadHandler(files, albumId, sharedKey));
|
||||
resolve(fileUploadHandler(files, albumId));
|
||||
};
|
||||
|
||||
fileSelector.click();
|
||||
|
|
@ -45,18 +42,14 @@ export const openFileUploadDialog = async (
|
|||
});
|
||||
};
|
||||
|
||||
export const fileUploadHandler = async (
|
||||
files: File[],
|
||||
albumId: string | undefined = undefined,
|
||||
sharedKey: string | undefined = undefined,
|
||||
) => {
|
||||
export const fileUploadHandler = async (files: File[], albumId: string | undefined = undefined) => {
|
||||
const extensions = await getExtensions();
|
||||
const iterable = {
|
||||
files: files.filter((file) => extensions.some((ext) => file.name.toLowerCase().endsWith(ext)))[Symbol.iterator](),
|
||||
|
||||
async *[Symbol.asyncIterator]() {
|
||||
for (const file of this.files) {
|
||||
yield fileUploader(file, albumId, sharedKey);
|
||||
yield fileUploader(file, albumId);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
|
@ -78,11 +71,7 @@ const fromAsync = async function <T>(iterable: AsyncIterable<T>) {
|
|||
};
|
||||
|
||||
// TODO: should probably use the @api SDK
|
||||
async function fileUploader(
|
||||
asset: File,
|
||||
albumId: string | undefined = undefined,
|
||||
sharedKey: string | undefined = undefined,
|
||||
): Promise<string | undefined> {
|
||||
async function fileUploader(asset: File, albumId: string | undefined = undefined): Promise<string | undefined> {
|
||||
const formData = new FormData();
|
||||
const fileCreatedAt = new Date(asset.lastModified).toISOString();
|
||||
const deviceAssetId = 'web' + '-' + asset.name + '-' + asset.lastModified;
|
||||
|
|
@ -103,9 +92,7 @@ async function fileUploader(
|
|||
});
|
||||
|
||||
const response = await axios.post('/api/asset/upload', formData, {
|
||||
params: {
|
||||
key: sharedKey,
|
||||
},
|
||||
params: { key: api.getKey() },
|
||||
onUploadProgress: (event) => {
|
||||
const percentComplete = Math.floor((event.loaded / event.total) * 100);
|
||||
uploadAssetsStore.updateProgress(deviceAssetId, percentComplete);
|
||||
|
|
@ -120,7 +107,7 @@ async function fileUploader(
|
|||
}
|
||||
|
||||
if (albumId && res.id) {
|
||||
await addAssetsToAlbum(albumId, [res.id], sharedKey);
|
||||
await addAssetsToAlbum(albumId, [res.id]);
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue