Skip to content

Commit

Permalink
Merge pull request #9 from SychO9/size_limit_setting
Browse files Browse the repository at this point in the history
Add maximum size setting
  • Loading branch information
SychO9 committed Oct 14, 2019
2 parents a1cb227 + 580b69c commit 3086589
Show file tree
Hide file tree
Showing 14 changed files with 172 additions and 42 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
![latest version](https://img.shields.io/github/release/SychO9/flarum-profile-cover.svg?style=flat-square)
![flarum version](https://img.shields.io/badge/flarum-%5E0.1.0--beta.10-%23e7742e?style=flat-square)
![mit license](https://img.shields.io/badge/license-MIT-green.svg?style=flat-square&color=green)
![downloads](https://img.shields.io/packagist/dt/sycho/flarum-profile-cover?color=%23f28d1a&style=flat-square)

A [Flarum](http://flarum.org/) extension. Adds the ability to upload a cover image to a user profile.

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
],
"type": "flarum-extension",
"require": {
"flarum/core": ">=0.1.0-beta.10 <0.1.0-beta.12"
"flarum/core": ">=0.1.0-beta.10 <0.1.0-beta.12",
"fof/extend": ">=0.2.0"
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 6 additions & 1 deletion extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,19 @@
->css(__DIR__.'/resources/less/forum.less'),

(new Extend\Frontend('admin'))
->js(__DIR__.'/js/dist/admin.js'),
->js(__DIR__.'/js/dist/admin.js')
->css(__DIR__.'/resources/less/admin.less'),

(new Extend\Routes('api'))
->post('/users/{id}/cover', 'users.cover.upload', Controller\UploadCoverController::class)
->delete('/users/{id}/cover', 'users.cover.delete', Controller\DeleteCoverController::class),

new Extend\Locales(__DIR__.'/resources/locale'),

(new FoF\Extend\Extend\ExtensionSettings())
->setPrefix('sycho-profile-cover.')
->addKeys(['max_size']),

function (Factory $view) {
$view->addNamespace('profile-cover', __DIR__.'/views');
},
Expand Down
61 changes: 47 additions & 14 deletions js/dist/admin.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion js/dist/admin.js.map

Large diffs are not rendered by default.

32 changes: 31 additions & 1 deletion js/dist/forum.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion js/dist/forum.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 15 additions & 13 deletions js/src/admin/components/CoverSettingsModal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import SettingsModal from 'flarum/components/SettingsModal';
import formatBytes from '../../common/formatBytes';

export default class CoverSettingsModal extends SettingsModal {
init() {
super.init();

this.maxSize = this.setting('sycho-profile-cover.max_size', 2048);
}
title() {
return app.translator.trans('sycho-profile-cover.admin.settings');
}
Expand All @@ -18,6 +24,14 @@ export default class CoverSettingsModal extends SettingsModal {
</label>
</div>,

<div className="Form-group">
<label>{app.translator.trans('sycho-profile-cover.admin.max_size')}</label>
<div className="ProfileCover-size-input">
<input type="number" className="FormControl" value={this.maxSize()} oninput={m.withAttr('value', this.maxSize)}/>
<input className="FormControl" value={formatBytes(this.maxSize() * Math.pow(2, 10))} disabled/>
</div>
</div>,

<div className="Form-group">
<div><strong>{app.translator.trans('sycho-profile-cover.admin.cover_size')}:</strong> {this.sizeOf('images')}</div>
<div><strong>{app.translator.trans('sycho-profile-cover.admin.thumb_size')}:</strong> {this.sizeOf('thumbs')}</div>
Expand All @@ -43,20 +57,8 @@ export default class CoverSettingsModal extends SettingsModal {
const count = parseInt(stats[type + '_count']);

return app.translator.transChoice(`sycho-profile-cover.admin.size_of_${type}`, count, {
size: this.formatBytes(parseFloat(size)),
size: formatBytes(size),
count: count
});
}

formatBytes(bytes, decimals = 2) {
if (bytes === 0) return '0 Bytes';

const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

const i = Math.floor(Math.log(bytes) / Math.log(k));

return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
}
11 changes: 11 additions & 0 deletions js/src/common/formatBytes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default function formatBytes(bytes, decimals = 2) {
if (bytes === 0) return '0 Bytes';

const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

const i = Math.floor(Math.log(bytes) / Math.log(k));

return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];
}
5 changes: 4 additions & 1 deletion js/src/forum/components/CoverEditorModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import FieldSet from 'flarum/components/FieldSet';
import ItemList from 'flarum/utils/ItemList';
import LoadingIndicator from 'flarum/components/LoadingIndicator';
import listItems from 'flarum/helpers/listItems';
import formatBytes from '../../common/formatBytes';

export default class CoverEditorModal extends Modal {
init() {
super.init();

this.maxSize = parseFloat(app.data['sycho-profile-cover.max_size'] || 2048);

this.alert = Alert.component({
children: app.translator.trans('sycho-profile-cover.forum.notice')
children: app.translator.trans('sycho-profile-cover.forum.notice', {size: formatBytes(this.maxSize * Math.pow(2, 10))})
});

this.loading = false;
Expand Down
10 changes: 10 additions & 0 deletions resources/less/admin.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.ProfileCover-size-input :first-child {
margin-bottom: 1px;
border-bottom-left-radius: 0;
border-bottom-right-radius: 0;
}

.ProfileCover-size-input :last-child {
border-top-left-radius: 0;
border-top-right-radius: 0;
}
3 changes: 2 additions & 1 deletion resources/locale/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ sycho-profile-cover:
admin:
settings: ProfileCover Settings
thumbnails: Create thumbnails
max_size: Maximum profile cover image size (in KB)
cover_size: Images size
thumb_size: Thumbnails size
size_of_images: "{size} out of {count} image|{size} out of {count} images"
size_of_thumbs: "{size} out of {count} thumbnail|{size} out of {count} thumbnails"
forum:
cover: Cover
edit_cover: Edit Cover
notice: "Maximum size: 2MB"
notice: "Maximum size: {size}"
added:
success: Profile cover updated.
error: Image could not be uploaded.
Expand Down
47 changes: 40 additions & 7 deletions src/CoverValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,47 @@
namespace SychO\ProfileCover;

use Flarum\Foundation\AbstractValidator;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Validation\Factory;
use Illuminate\Contracts\Events\Dispatcher;
use Symfony\Component\Translation\TranslatorInterface;

class CoverValidator extends AbstractValidator
{
protected $rules = [
'cover' => [
'required',
'mimes:jpeg,png,bmp',
'max:2048'
]
];
/**
* @var SettingsRepositoryInterface
*/
protected $config;

/**
* {@inheritdoc}
*/
public function __construct(Factory $validator, Dispatcher $events, TranslatorInterface $translator, SettingsRepositoryInterface $config)
{
parent::__construct($validator, $events, $translator);

$this->config = $config;
}

/**
* {@inheritdoc}
*/
public function getRules()
{
return [
'cover' => [
'required',
'mimes:jpeg,png,bmp',
'max:' . $this->getMaxSize()
]
];
}

/**
* @return string
*/
public function getMaxSize()
{
return $this->config->get('sycho-profile-cover.max_size') ?? '2048';
}
}

0 comments on commit 3086589

Please sign in to comment.