Railsにmedium-editorでMedium.comライクな投稿機能を導入するで導入まですませている前提になります。
やりたいこと
画像を登録する際に、容量制限を設定したい。
調査
画像を保存する部分には、medium-editor-insert-pluginを使っている。
ドキュメントを読んで見ると、jQuery-File-Uploadをラップしてアップロード処理を行なっていた。
medium-editor-insert-pluginのconfigrationに以下のようにあった。
jQuery File Uploadを見に行く
maxFileSize
The maximum allowed file size in bytes.Type: integer
Default: undefined
Example: 10000000 // 10 MB
Note: This option has only an effect for browsers supporting the File API.
maxFileSizeに関する記述を見つけた。
maxFileSizeを設定する
$('.editable').mediumInsert({
editor: editor,
addons: {
images: {
useDragAndDrop: false,
fileUploadOptions: {
url: '/images/upload',
type: 'post',
// 以下を追加
maxFileSize: 3000000, //3MBで制限を掛ける
acceptFileTypes: /(.|\/)(gif|jpe?g|png)$/i
},
fileDeleteOptions: {
url: '/images/delete',
},
messages: {
acceptFileTypesError: 'This file is not in a supported format: ',
maxFileSizeError: 'This file is too big: '
}
}
}
});
大きな画像をアップロードしてみます。
大きな画像を防ぐことが出来ました。
以上です。