2023-05-08 16:36:10 +00:00
|
|
|
import { createSlice, createAsyncThunk } from '@reduxjs/toolkit';
|
2023-05-07 22:29:37 +00:00
|
|
|
import type { PayloadAction } from '@reduxjs/toolkit';
|
2023-05-09 00:22:00 +00:00
|
|
|
import type { RootState } from '~/store';
|
|
|
|
import { decryptionQueue } from '~/decrypt-worker/client';
|
2023-05-07 22:29:37 +00:00
|
|
|
|
2023-05-09 00:23:35 +00:00
|
|
|
import type { DecryptionResult } from '~/decrypt-worker/constants';
|
|
|
|
|
2023-05-07 22:29:37 +00:00
|
|
|
export enum ProcessState {
|
|
|
|
UNTOUCHED = 'UNTOUCHED',
|
|
|
|
COMPLETE = 'COMPLETE',
|
|
|
|
ERROR = 'ERROR',
|
|
|
|
}
|
|
|
|
|
|
|
|
export enum ListingMode {
|
|
|
|
LIST = 'LIST',
|
|
|
|
CARD = 'CARD',
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface AudioMetadata {
|
|
|
|
name: string;
|
|
|
|
artist: string;
|
|
|
|
album: string;
|
|
|
|
albumArtist: string;
|
|
|
|
cover: string; // blob uri
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface DecryptedAudioFile {
|
|
|
|
fileName: string;
|
|
|
|
raw: string; // blob uri
|
2023-05-14 22:47:56 +00:00
|
|
|
ext: string;
|
2023-05-07 22:29:37 +00:00
|
|
|
decrypted: string; // blob uri
|
|
|
|
state: ProcessState;
|
|
|
|
errorMessage: null | string;
|
|
|
|
metadata: AudioMetadata;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface FileListingState {
|
2023-05-08 14:54:00 +00:00
|
|
|
files: Record<string, DecryptedAudioFile>;
|
2023-05-07 22:29:37 +00:00
|
|
|
displayMode: ListingMode;
|
|
|
|
}
|
|
|
|
const initialState: FileListingState = {
|
2023-05-08 14:54:00 +00:00
|
|
|
files: Object.create(null),
|
2023-05-07 22:29:37 +00:00
|
|
|
displayMode: ListingMode.LIST,
|
|
|
|
};
|
|
|
|
|
2023-05-09 00:23:35 +00:00
|
|
|
export const processFile = createAsyncThunk<
|
|
|
|
DecryptionResult,
|
|
|
|
{ fileId: string },
|
|
|
|
{ rejectValue: { message: string; stack?: string } }
|
|
|
|
>('fileListing/processFile', async ({ fileId }, thunkAPI) => {
|
2023-05-08 16:36:10 +00:00
|
|
|
const file = selectFiles(thunkAPI.getState() as RootState)[fileId];
|
|
|
|
if (!file) {
|
2023-05-09 00:23:35 +00:00
|
|
|
const { message, stack } = new Error('ERROR: File not found');
|
|
|
|
return thunkAPI.rejectWithValue({ message, stack });
|
2023-05-08 16:36:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return decryptionQueue.add({ id: fileId, blobURI: file.raw });
|
|
|
|
});
|
|
|
|
|
2023-05-07 22:29:37 +00:00
|
|
|
export const fileListingSlice = createSlice({
|
|
|
|
name: 'fileListing',
|
|
|
|
initialState,
|
|
|
|
reducers: {
|
|
|
|
addNewFile: (state, { payload }: PayloadAction<{ id: string; fileName: string; blobURI: string }>) => {
|
2023-05-08 14:54:00 +00:00
|
|
|
state.files[payload.id] = {
|
2023-05-07 22:29:37 +00:00
|
|
|
fileName: payload.fileName,
|
|
|
|
raw: payload.blobURI,
|
|
|
|
decrypted: '',
|
2023-05-14 22:47:56 +00:00
|
|
|
ext: '',
|
2023-05-07 22:29:37 +00:00
|
|
|
state: ProcessState.UNTOUCHED,
|
|
|
|
errorMessage: null,
|
|
|
|
metadata: {
|
|
|
|
name: '',
|
|
|
|
artist: '',
|
|
|
|
album: '',
|
|
|
|
albumArtist: '',
|
|
|
|
cover: '',
|
|
|
|
},
|
2023-05-08 14:54:00 +00:00
|
|
|
};
|
2023-05-07 22:29:37 +00:00
|
|
|
},
|
|
|
|
setDecryptedContent: (state, { payload }: PayloadAction<{ id: string; decryptedBlobURI: string }>) => {
|
2023-05-08 14:54:00 +00:00
|
|
|
const file = state.files[payload.id];
|
2023-05-07 22:29:37 +00:00
|
|
|
if (file) {
|
|
|
|
file.decrypted = payload.decryptedBlobURI;
|
|
|
|
}
|
|
|
|
},
|
2023-05-15 20:05:03 +00:00
|
|
|
deleteFile: (state, { payload }: PayloadAction<{ id: string }>) => {
|
|
|
|
if (state.files[payload.id]) {
|
|
|
|
const file = state.files[payload.id];
|
|
|
|
if (file.decrypted) {
|
|
|
|
URL.revokeObjectURL(file.decrypted);
|
|
|
|
}
|
|
|
|
if (file.raw) {
|
|
|
|
URL.revokeObjectURL(file.raw);
|
|
|
|
}
|
|
|
|
delete state.files[payload.id];
|
|
|
|
}
|
|
|
|
},
|
2023-05-07 22:29:37 +00:00
|
|
|
},
|
2023-05-09 00:23:35 +00:00
|
|
|
extraReducers(builder) {
|
|
|
|
builder.addCase(processFile.fulfilled, (state, action) => {
|
|
|
|
const { fileId } = action.meta.arg;
|
|
|
|
const file = state.files[fileId];
|
|
|
|
if (!file) return;
|
|
|
|
|
|
|
|
file.state = ProcessState.COMPLETE;
|
|
|
|
file.decrypted = action.payload.decrypted;
|
2023-05-14 22:47:56 +00:00
|
|
|
file.ext = action.payload.ext;
|
2023-05-09 00:23:35 +00:00
|
|
|
// TODO: populate file metadata
|
|
|
|
});
|
|
|
|
|
|
|
|
builder.addCase(processFile.rejected, (state, action) => {
|
|
|
|
const { fileId } = action.meta.arg;
|
|
|
|
const file = state.files[fileId];
|
|
|
|
if (!file) return;
|
|
|
|
|
|
|
|
if (action.payload) {
|
|
|
|
file.errorMessage = action.payload.message;
|
|
|
|
} else {
|
|
|
|
file.errorMessage = action.error.message || 'unknown error';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2023-05-07 22:29:37 +00:00
|
|
|
});
|
|
|
|
|
2023-05-15 20:05:03 +00:00
|
|
|
export const { addNewFile, setDecryptedContent, deleteFile } = fileListingSlice.actions;
|
2023-05-07 22:29:37 +00:00
|
|
|
|
|
|
|
export const selectFileCount = (state: RootState) => state.fileListing.files.length;
|
|
|
|
export const selectFiles = (state: RootState) => state.fileListing.files;
|
|
|
|
export const selectFileListingMode = (state: RootState) => state.fileListing.displayMode;
|
|
|
|
|
|
|
|
export default fileListingSlice.reducer;
|