From 4a3de2ab01019ff74a0c390140e86be4c6642ea2 Mon Sep 17 00:00:00 2001 From: Fedor Katurov Date: Wed, 9 Oct 2019 21:17:00 +0700 Subject: [PATCH] uploading mpegs --- src/redux/uploads/constants.ts | 8 +++++--- src/redux/uploads/sagas.ts | 18 ++++++------------ src/utils/uploader.ts | 6 ++---- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/src/redux/uploads/constants.ts b/src/redux/uploads/constants.ts index d4ec2fe6..74a1528b 100644 --- a/src/redux/uploads/constants.ts +++ b/src/redux/uploads/constants.ts @@ -1,5 +1,5 @@ import { IFile, IUploadType } from '~/redux/types'; -import { IUploadState, IUploadStatus } from './reducer'; +import { IUploadStatus } from './reducer'; const prefix = 'UPLOAD.'; @@ -62,6 +62,8 @@ export const UPLOAD_TYPES: Record = { }; export const FILE_MIMES = { - [UPLOAD_TYPES.image]: ['image/jpeg', 'image/jpg', 'image/png'], - [UPLOAD_TYPES.audio]: ['audio/mpeg3', 'audio/mpeg', 'audio/mp3'], + [UPLOAD_TYPES.VIDEO]: [], + [UPLOAD_TYPES.IMAGE]: ['image/jpeg', 'image/jpg', 'image/png'], + [UPLOAD_TYPES.AUDIO]: ['audio/mpeg3', 'audio/mpeg', 'audio/mp3'], + [UPLOAD_TYPES.OTHER]: [], }; diff --git a/src/redux/uploads/sagas.ts b/src/redux/uploads/sagas.ts index 6d86e21a..d5472f9b 100644 --- a/src/redux/uploads/sagas.ts +++ b/src/redux/uploads/sagas.ts @@ -1,8 +1,6 @@ -import { - takeEvery, all, spawn, call, put, take, fork, race, -} from 'redux-saga/effects'; +import { takeEvery, all, spawn, call, put, take, fork, race } from 'redux-saga/effects'; import { postUploadFile } from './api'; -import { UPLOAD_ACTIONS } from '~/redux/uploads/constants'; +import { UPLOAD_ACTIONS, FILE_MIMES } from '~/redux/uploads/constants'; import { uploadUploadFiles, uploadSetStatus, @@ -49,9 +47,7 @@ function* uploadCancelWorker(id) { return true; } -function* uploadWorker({ - file, temp_id, target, type, -}: IFileWithUUID) { +function* uploadWorker({ file, temp_id, target, type }: IFileWithUUID) { const [promise, chan] = createUploader, Partial>( uploadCall, { temp_id, target, type } @@ -69,10 +65,8 @@ function* uploadWorker({ return result; } -function* uploadFile({ - file, temp_id, type, target, -}: IFileWithUUID) { - if (!file.type || !VALIDATORS.IS_IMAGE_MIME(file.type)) { +function* uploadFile({ file, temp_id, type, target }: IFileWithUUID) { + if (!file.type || !file.type || !FILE_MIMES[type].includes(file.type)) { return { error: 'File_Not_Image', status: HTTP_RESPONSES.BAD_REQUEST, data: {} }; } @@ -137,6 +131,6 @@ function* uploadFiles({ files }: ReturnType) { yield all(files.map(file => spawn(uploadFile, file))); } -export default function* () { +export default function*() { yield takeEvery(UPLOAD_ACTIONS.UPLOAD_FILES, uploadFiles); } diff --git a/src/utils/uploader.ts b/src/utils/uploader.ts index 2adac2cb..1326bc6e 100644 --- a/src/utils/uploader.ts +++ b/src/utils/uploader.ts @@ -71,11 +71,9 @@ export const fakeUploader = ({ }); }; -export const getFileType = (file: File) => { - console.log({ type: file.type }); - +export const getFileType = (file: File): keyof typeof UPLOAD_TYPES => { return ( (file.type && Object.keys(FILE_MIMES).find(mime => FILE_MIMES[mime].includes(file.type))) || - UPLOAD_TYPES.OTHER + null ); };