Skip to content
This repository has been archived by the owner on Dec 21, 2023. It is now read-only.

Commit

Permalink
Change media upload limits and remove client-side resizing (mastodon#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Gargron authored Mar 25, 2023
1 parent ef127c9 commit 9bda933
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 251 deletions.
73 changes: 34 additions & 39 deletions app/javascript/mastodon/actions/compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { defineMessages } from 'react-intl';
import api from 'mastodon/api';
import { search as emojiSearch } from 'mastodon/features/emoji/emoji_mart_search_light';
import { tagHistory } from 'mastodon/settings';
import resizeImage from 'mastodon/utils/resize_image';
import { showAlert, showAlertForError } from './alerts';
import { useEmoji } from './emojis';
import { importFetchedAccounts, importFetchedStatus } from './importer';
Expand Down Expand Up @@ -274,46 +273,42 @@ export function uploadCompose(files) {

dispatch(uploadComposeRequest());

for (const [i, f] of Array.from(files).entries()) {
for (const [i, file] of Array.from(files).entries()) {
if (media.size + i > 3) break;

resizeImage(f).then(file => {
const data = new FormData();
data.append('file', file);
// Account for disparity in size of original image and resized data
total += file.size - f.size;

return api(getState).post('/api/v2/media', data, {
onUploadProgress: function({ loaded }){
progress[i] = loaded;
dispatch(uploadComposeProgress(progress.reduce((a, v) => a + v, 0), total));
},
}).then(({ status, data }) => {
// If server-side processing of the media attachment has not completed yet,
// poll the server until it is, before showing the media attachment as uploaded

if (status === 200) {
dispatch(uploadComposeSuccess(data, f));
} else if (status === 202) {
dispatch(uploadComposeProcessing());

let tryCount = 1;

const poll = () => {
api(getState).get(`/api/v1/media/${data.id}`).then(response => {
if (response.status === 200) {
dispatch(uploadComposeSuccess(response.data, f));
} else if (response.status === 206) {
const retryAfter = (Math.log2(tryCount) || 1) * 1000;
tryCount += 1;
setTimeout(() => poll(), retryAfter);
}
}).catch(error => dispatch(uploadComposeFail(error)));
};

poll();
}
});
const data = new FormData();
data.append('file', file);

api(getState).post('/api/v2/media', data, {
onUploadProgress: function({ loaded }){
progress[i] = loaded;
dispatch(uploadComposeProgress(progress.reduce((a, v) => a + v, 0), total));
},
}).then(({ status, data }) => {
// If server-side processing of the media attachment has not completed yet,
// poll the server until it is, before showing the media attachment as uploaded

if (status === 200) {
dispatch(uploadComposeSuccess(data, file));
} else if (status === 202) {
dispatch(uploadComposeProcessing());

let tryCount = 1;

const poll = () => {
api(getState).get(`/api/v1/media/${data.id}`).then(response => {
if (response.status === 200) {
dispatch(uploadComposeSuccess(response.data, file));
} else if (response.status === 206) {
const retryAfter = (Math.log2(tryCount) || 1) * 1000;
tryCount += 1;
setTimeout(() => poll(), retryAfter);
}
}).catch(error => dispatch(uploadComposeFail(error)));
};

poll();
}
}).catch(error => dispatch(uploadComposeFail(error)));
}
};
Expand Down
189 changes: 0 additions & 189 deletions app/javascript/mastodon/utils/resize_image.js

This file was deleted.

2 changes: 1 addition & 1 deletion app/models/concerns/attachmentable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
module Attachmentable
extend ActiveSupport::Concern

MAX_MATRIX_LIMIT = 16_777_216 # 4096x4096px or approx. 16MB
MAX_MATRIX_LIMIT = 33_177_600 # 7680x4320px or approx. 847MB in RAM
GIF_MATRIX_LIMIT = 921_600 # 1280x720px

# For some file extensions, there exist different content
Expand Down
10 changes: 5 additions & 5 deletions app/models/media_attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ class MediaAttachment < ApplicationRecord

MAX_DESCRIPTION_LENGTH = 1_500

IMAGE_LIMIT = 10.megabytes
VIDEO_LIMIT = 40.megabytes
IMAGE_LIMIT = 16.megabytes
VIDEO_LIMIT = 99.megabytes

MAX_VIDEO_MATRIX_LIMIT = 2_304_000 # 1920x1200px
MAX_VIDEO_FRAME_RATE = 60
MAX_VIDEO_MATRIX_LIMIT = 8_294_400 # 3840x2160px
MAX_VIDEO_FRAME_RATE = 120

IMAGE_FILE_EXTENSIONS = %w(.jpg .jpeg .png .gif .webp .heic .heif .avif).freeze
VIDEO_FILE_EXTENSIONS = %w(.webm .mp4 .m4v .mov).freeze
Expand All @@ -69,7 +69,7 @@ class MediaAttachment < ApplicationRecord

IMAGE_STYLES = {
original: {
pixels: 2_073_600, # 1920x1080px
pixels: 8_294_400, # 3840x2160px
file_geometry_parser: FastGeometryParser,
}.freeze,

Expand Down
4 changes: 2 additions & 2 deletions app/models/preview_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class PreviewCard < ApplicationRecord
include Attachmentable

IMAGE_MIME_TYPES = ['image/jpeg', 'image/png', 'image/gif', 'image/webp'].freeze
LIMIT = 1.megabytes
LIMIT = 2.megabytes

BLURHASH_OPTIONS = {
x_comp: 4,
Expand Down Expand Up @@ -121,7 +121,7 @@ class << self
def image_styles(file)
styles = {
original: {
geometry: '400x400>',
pixels: 230_400, # 640x360px
file_geometry_parser: FastGeometryParser,
convert_options: '-coalesce',
blurhash: BLURHASH_OPTIONS,
Expand Down
2 changes: 1 addition & 1 deletion dist/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ server {

keepalive_timeout 70;
sendfile on;
client_max_body_size 80m;
client_max_body_size 99m;

root /home/mastodon/live/public;

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"emoji-mart": "npm:emoji-mart-lazyload@latest",
"es6-symbol": "^3.1.3",
"escape-html": "^1.0.3",
"exif-js": "^2.3.0",
"express": "^4.18.2",
"file-loader": "^6.2.0",
"font-awesome": "^4.7.0",
Expand Down
8 changes: 0 additions & 8 deletions spec/controllers/settings/profiles_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,4 @@
expect(ActivityPub::UpdateDistributionWorker).to have_received(:perform_async).with(account.id)
end
end

describe 'PUT #update with oversized image' do
it 'gives the user an error message' do
allow(ActivityPub::UpdateDistributionWorker).to receive(:perform_async)
put :update, params: { account: { avatar: fixture_file_upload('4096x4097.png', 'image/png') } }
expect(response.body).to include('images are not supported')
end
end
end
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4821,11 +4821,6 @@ execa@^5.0.0:
signal-exit "^3.0.3"
strip-final-newline "^2.0.0"

exif-js@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/exif-js/-/exif-js-2.3.0.tgz#9d10819bf571f873813e7640241255ab9ce1a814"
integrity sha1-nRCBm/Vx+HOBPnZAJBJVq5zhqBQ=

exit@^0.1.2:
version "0.1.2"
resolved "https://registry.yarnpkg.com/exit/-/exit-0.1.2.tgz#0632638f8d877cc82107d30a0fff1a17cba1cd0c"
Expand Down

0 comments on commit 9bda933

Please sign in to comment.