Skip to content

Commit

Permalink
Image proxy - use /start_upload instead of limit in Blogs code (limit…
Browse files Browse the repository at this point in the history
… is only for Settings - avatar, cover)
  • Loading branch information
1aerostorm committed Jun 17, 2024
1 parent 5579866 commit 2d19195
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 35 deletions.
6 changes: 0 additions & 6 deletions app/components/elements/LocaleSelect.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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))
},
Expand Down
2 changes: 1 addition & 1 deletion app/components/elements/ReplyEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 0 additions & 1 deletion app/components/modules/CommentForm/CommentForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,6 @@ export default connect(

progress(data);
},
useGolosImages: true,
},
});
},
Expand Down
1 change: 0 additions & 1 deletion app/components/modules/PostForm/PostForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,6 @@ export default connect(

progress(data);
},
useGolosImages: true,
},
});
},
Expand Down
2 changes: 1 addition & 1 deletion app/components/modules/Settings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
4 changes: 3 additions & 1 deletion app/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
}
},
Expand Down
4 changes: 3 additions & 1 deletion app/locales/ru-RU.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
}
}
},
Expand Down
80 changes: 57 additions & 23 deletions app/redux/UserSaga_UploadImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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
}
}
}

Expand Down Expand Up @@ -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 = {}
Expand Down

0 comments on commit 2d19195

Please sign in to comment.