Skip to content

Commit

Permalink
Image proxy - imgur fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
1aerostorm committed Jun 18, 2024
1 parent a2e2d74 commit 5a77e29
Showing 1 changed file with 50 additions and 34 deletions.
84 changes: 50 additions & 34 deletions app/redux/UserSaga_UploadImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,27 @@ function* uploadImage(action) {
}

let postUrl = $STM_Config.images.upload_image

let golosImages = false
const user = yield select(state => state.user)
const switchToGolosImages = async () => {
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
}

if (file) {
if (imageSizeLimit && file.size > imageSizeLimit) {
onError(tt('user_saga_js.image_upload.error.image_size_is_too_large'));
Expand All @@ -94,25 +114,18 @@ function* uploadImage(action) {
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
yield switchToGolosImages()
}
}
}
const onImgurFail = async (imgurErr) => {
console.log('onImgurFail - switch to Golos Images..')
await switchToGolosImages()
console.log('onImgurFail - ok, sending..')
xhr.open('POST', postUrl);
formData.append('fallback', imgurErr)
xhr.send(formData)
}

/**
* The challenge needs to be prefixed with a constant (both on the server
Expand Down Expand Up @@ -140,7 +153,7 @@ function* uploadImage(action) {
xhr.setRequestHeader('Authorization', 'Client-ID ' + $STM_Config.images.client_id)
}

xhr.onload = function() {
xhr.onload = async function() {
let data;

try {
Expand All @@ -167,37 +180,40 @@ function* uploadImage(action) {

console.error('Cannot upload image:', xhr.responseText);

let repeat = false;
if (!golosImages) {
if (xhr.responseText.includes('Invalid client')) {
++imgurFailCounter;
if (imgurFailCounter < 5) {
repeat = true;
setTimeout(() => {
xhr.open('POST', postUrl);
xhr.setRequestHeader('Authorization', 'Client-ID ' + $STM_Config.images.client_id)
xhr.send(formData);
}, 1000);
return
}
}
if (!xhr.responseText.includes('file type invalid')
&& !xhr.responseText.includes('We don\'t support that file type!')) {
await onImgurFail(xhr.responseText)
return
}
}
if (!repeat) {
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
}

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)
}
onError(err)
} else {
let result = {}
if (!golosImages) {
Expand Down

0 comments on commit 5a77e29

Please sign in to comment.