From 2d19195bb638a8d5478c9c9828f4906caa5a01cf Mon Sep 17 00:00:00 2001 From: 1aerostorm Date: Mon, 17 Jun 2024 12:43:38 +0300 Subject: [PATCH] Image proxy - use /start_upload instead of limit in Blogs code (limit is only for Settings - avatar, cover) --- app/components/elements/LocaleSelect.jsx | 6 -- app/components/elements/ReplyEditor.jsx | 2 +- .../modules/CommentForm/CommentForm.jsx | 1 - app/components/modules/PostForm/PostForm.jsx | 1 - app/components/modules/Settings.jsx | 2 +- app/locales/en.json | 4 +- app/locales/ru-RU.json | 4 +- app/redux/UserSaga_UploadImage.js | 80 +++++++++++++------ 8 files changed, 65 insertions(+), 35 deletions(-) diff --git a/app/components/elements/LocaleSelect.jsx b/app/components/elements/LocaleSelect.jsx index 306e9d0ea..e2c27156c 100644 --- a/app/components/elements/LocaleSelect.jsx +++ b/app/components/elements/LocaleSelect.jsx @@ -193,12 +193,6 @@ export default connect((state, props) => { locale, }; }, dispatch => ({ - uploadImage: (file, progress) => { - dispatch({ - type: 'user/UPLOAD_IMAGE', - payload: {file, progress}, - }) - }, changeLanguage: (language) => { dispatch(user.actions.changeLanguage(language)) }, diff --git a/app/components/elements/ReplyEditor.jsx b/app/components/elements/ReplyEditor.jsx index 320e90f0e..895d6b71f 100644 --- a/app/components/elements/ReplyEditor.jsx +++ b/app/components/elements/ReplyEditor.jsx @@ -861,7 +861,7 @@ export default formId => uploadImage: (file, progress) => { dispatch({ type: 'user/UPLOAD_IMAGE', - payload: { file, progress, useGolosImages: true, }, + payload: { file, progress, }, }); }, reply: replyAction(dispatch, remarkable), diff --git a/app/components/modules/CommentForm/CommentForm.jsx b/app/components/modules/CommentForm/CommentForm.jsx index 0b74ad349..1390f14ea 100644 --- a/app/components/modules/CommentForm/CommentForm.jsx +++ b/app/components/modules/CommentForm/CommentForm.jsx @@ -399,7 +399,6 @@ export default connect( progress(data); }, - useGolosImages: true, }, }); }, diff --git a/app/components/modules/PostForm/PostForm.jsx b/app/components/modules/PostForm/PostForm.jsx index 16f4ddf51..357292469 100644 --- a/app/components/modules/PostForm/PostForm.jsx +++ b/app/components/modules/PostForm/PostForm.jsx @@ -892,7 +892,6 @@ export default connect( progress(data); }, - useGolosImages: true, }, }); }, diff --git a/app/components/modules/Settings.jsx b/app/components/modules/Settings.jsx index 0b413e1db..27c8eca5e 100644 --- a/app/components/modules/Settings.jsx +++ b/app/components/modules/Settings.jsx @@ -565,7 +565,7 @@ export default connect( uploadImage: (file, progress) => { dispatch({ type: 'user/UPLOAD_IMAGE', - payload: {file, progress}, + payload: {file, progress, imageSizeLimit: 1000*1000}, }) }, changeLanguage: (language) => { diff --git a/app/locales/en.json b/app/locales/en.json index 63de8a9e2..3dd088df0 100644 --- a/app/locales/en.json +++ b/app/locales/en.json @@ -966,7 +966,9 @@ "login_first": "Please logged in first ...", "login_with_posting_key": "Login with your posting key", "server_unavailable": "The server is unavailable", - "image_size_is_too_large": "Upload images up to 1 MB in size" + "image_size_is_too_large": "Upload images up to 1 MB in size", + "low_reputation": "Too low reputation. Need at least ", + "low_golos_power_NEED_ADD": "No Golos Power. Need %(NEED)s, add please %(ADD)s." } } }, diff --git a/app/locales/ru-RU.json b/app/locales/ru-RU.json index 93234a453..6acf3262e 100644 --- a/app/locales/ru-RU.json +++ b/app/locales/ru-RU.json @@ -1029,7 +1029,9 @@ "login_first": "Пожалуйста, выполните вход в систему ...", "login_with_posting_key": "Недоступен постинг ключ", "server_unavailable": "Ошибка доступа к серверу", - "image_size_is_too_large": "Загрузите фото размером до 1 МБ" + "image_size_is_too_large": "Загрузите фото размером до 1 МБ", + "low_reputation": "Не хватает репутации. Нужно не менее ", + "low_golos_power_NEED_ADD": "Не хватает Силы Голоса. Нужно %(NEED)s, добавьте еще %(ADD)s." } } }, diff --git a/app/redux/UserSaga_UploadImage.js b/app/redux/UserSaga_UploadImage.js index b0f73c763..e3262f883 100644 --- a/app/redux/UserSaga_UploadImage.js +++ b/app/redux/UserSaga_UploadImage.js @@ -2,8 +2,9 @@ import tt from 'counterpart'; import { select, takeEvery } from 'redux-saga/effects'; import { signData } from 'golos-lib-js/lib/auth' import { Signature, hash } from 'golos-lib-js/lib/auth/ecc/index'; +import { Asset, fetchEx } from 'golos-lib-js/lib/utils' -const MAX_UPLOAD_IMAGE_SIZE = 1024 * 1024; +const MAX_UPLOAD_IMAGE_SIZE = 1; export default function* uploadImageWatch() { yield takeEvery('user/UPLOAD_IMAGE', uploadImage); @@ -31,7 +32,7 @@ const ERRORS_MATCH = [ ]; function* uploadImage(action) { - const { file, dataUrl, filename = 'image.txt', progress, useGolosImages = false } = action.payload; + const { file, dataUrl, filename = 'image.txt', progress, imageSizeLimit = 0 } = action.payload; function onError(txt) { progress({ @@ -70,27 +71,46 @@ function* uploadImage(action) { let postUrl = $STM_Config.images.upload_image let golosImages = false - if (file && file.size > MAX_UPLOAD_IMAGE_SIZE) { - if (useGolosImages && $STM_Config.images.use_img_proxy !== false) { - const user = yield select(state => state.user); - const username = user.getIn(['current', 'username']); - const postingKey = user.getIn([ - 'current', - 'private_keys', - 'posting_private', - ]); - if (!username || !postingKey) { - onError(tt('user_saga_js.image_upload.error.login_first')); - return; - } - const signatures = signData(data, { - posting: postingKey, - }) - postUrl = new URL('/@' + username + '/' + signatures.posting, $STM_Config.images.img_proxy_prefix).toString(); - golosImages = true - } else { + if (file) { + if (imageSizeLimit && file.size > imageSizeLimit) { onError(tt('user_saga_js.image_upload.error.image_size_is_too_large')); - return; + return + } + + if ($STM_Config.images.use_img_proxy !== false) { + let recommended = false + try { + let su = new URL('/start_upload/' + file.size, $STM_Config.images.img_proxy_prefix).toString() + su = yield fetchEx(su, { + timeout: 1500 + }) + su = yield su.json() + if (su.recommended === 'undefined') { + console.warning('image_proxy start_upload:', 'No recommended field:', su) + throw new Error('Wrong response') + } + recommended = !!su.recommended + } catch (err) { + console.error('image_proxy start_upload:', err) + } + if (recommended) { + const user = yield select(state => state.user); + const username = user.getIn(['current', 'username']); + const postingKey = user.getIn([ + 'current', + 'private_keys', + 'posting_private', + ]); + if (!username || !postingKey) { + onError(tt('user_saga_js.image_upload.error.login_first')); + return; + } + const signatures = signData(data, { + posting: postingKey, + }) + postUrl = new URL('/@' + username + '/' + signatures.posting, $STM_Config.images.img_proxy_prefix).toString(); + golosImages = true + } } } @@ -162,7 +182,21 @@ function* uploadImage(action) { } } if (!repeat) { - onError(xhr.responseText); + let err = xhr.responseText + if (golosImages) { + if (data.error === 'too_low_account_golos_power') { + const need = Asset(data.required) + const add = need.minus(Asset(data.power)) + err = tt('user_saga_js.image_upload.error.low_golos_power_NEED_ADD', + { + NEED: need.floatString, + ADD: add.floatString + }) + } else if (data.error === 'too_low_account_reputation') { + err = tt('user_saga_js.image_upload.error.low_reputation') + data.required + } + } + onError(err) } } else { let result = {}