From 9a658d41a810e6a7b9367516971feed86c3f8a70 Mon Sep 17 00:00:00 2001 From: Travis Eden Date: Mon, 2 Apr 2018 09:38:06 -0400 Subject: [PATCH] add doResetThumbnailStatus and doUploadThumbnail actions --- src/renderer/redux/actions/publish.js | 84 ++++++++++++++++++++++++--- 1 file changed, 75 insertions(+), 9 deletions(-) diff --git a/src/renderer/redux/actions/publish.js b/src/renderer/redux/actions/publish.js index 5163af6fc..37c364e5d 100644 --- a/src/renderer/redux/actions/publish.js +++ b/src/renderer/redux/actions/publish.js @@ -6,6 +6,7 @@ import { doNotify, MODALS, selectMyChannelClaims, + STATUSES } from 'lbry-redux'; import { selectPendingPublishes } from 'redux/selectors/publish'; import type { @@ -13,6 +14,8 @@ import type { UpdatePublishFormAction, PublishParams, } from 'redux/reducers/publish'; +import fs from 'fs'; +import path from 'path'; type Action = UpdatePublishFormAction | { type: ACTIONS.CLEAR_PUBLISH }; type PromiseAction = Promise; @@ -30,15 +33,78 @@ export const doUpdatePublishForm = (publishFormValue: UpdatePublishFormData) => data: { ...publishFormValue }, }); -export const doPrepareEdit = (claim: any, uri: string) => (dispatch: Dispatch) => { - const { - name, - amount, - channel_name: channelName, - value: { - stream: { metadata }, - }, - } = claim; +export const doResetThumbnailStatus = () => (dispatch: Dispatch): Action => + fetch('https://spee.ch/api/channel/availability/@testing') + .then(() => + dispatch({ + type: ACTIONS.UPDATE_PUBLISH_FORM, + data: { thumbnailStatus: STATUSES.READY }, + }) + ) + .catch(() => + dispatch({ + type: ACTIONS.UPDATE_PUBLISH_FORM, + data: { thumbnailStatus: STATUSES.DOWN }, + }) + ); + +export const doUploadThumbnail = (filePath: string, nsfw: boolean) => (dispatch: Dispatch) => { + const thumbnail = fs.readFileSync(filePath); + const fileExt = path.extname(filePath); + + const makeid = () => { + let text = ''; + const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; + for (let i = 0; i < 24; i += 1) text += possible.charAt(Math.floor(Math.random() * 62)); + return text; + }; + + const uploadError = (error: string = '') => + dispatch( + batchActions( + { + type: ACTIONS.UPDATE_PUBLISH_FORM, + data: { thumbnailStatus: STATUSES.DOWN }, + }, + doAlertError(error) + ) + ); + + dispatch({ + type: ACTIONS.UPDATE_PUBLISH_FORM, + data: { thumbnailStatus: STATUSES.IN_PROGRESS }, + }); + + const data = new FormData(); + const name = makeid(); + const blob = new Blob([thumbnail], { type: `image/${fileExt.slice(1)}` }); + data.append('name', name); + data.append('file', blob); + data.append('nsfw', nsfw.toString()); + return fetch('https://spee.ch/api/claim/publish', { + method: 'POST', + body: data, + }) + .then(response => response.json()) + .then( + json => + json.success + ? dispatch({ + type: ACTIONS.UPDATE_PUBLISH_FORM, + data: { + thumbnailStatus: STATUSES.COMPLETE, + thumbnailUrl: `${json.data.url}${fileExt}`, + }, + }) + : uploadError() + ) + .catch(() => uploadError()); +}; + +export const doPrepareEdit = (claim: any) => (dispatch: Dispatch) => { + const { name, amount, channel_name: channelName, value: { stream: { metadata } } } = claim; + + const { author, description,