Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

Starting work on attachments. #177

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions app/Attachments/AttachmentTypeValidationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Attachment type validation exception.
*
* Can be thrown when validating an exception as a given type.
* If thrown, the string provided as the message will be shown to the end user.
*
* @author MyBB Group
* @version 2.0.0
* @package mybb/core
* @copyright Copyright (c) 2015, MyBB Group
* @license http://www.mybb.com/licenses/bsd3 BSD-3
* @link http://www.mybb.com
*/

namespace MyBB\Core\Attachments;

class AttachmentTypeValidationException extends \Exception
{

}
35 changes: 35 additions & 0 deletions app/Attachments/AttachmentTypeValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Attachment type validator interface.
*
* An attachment type validator performs any logic required for a given attachment type, such as checking whether an
* attachment is a valid image, etc.
*
* @author MyBB Group
* @version 2.0.0
* @package mybb/core
* @copyright Copyright (c) 2015, MyBB Group
* @license http://www.mybb.com/licenses/bsd3 BSD-3
* @link http://www.mybb.com
*/

namespace MyBB\Core\Attachments;

use MyBB\Core\Attachments\Database\Models\AttachmentType;
use MyBB\Core\Database\Models\User;

interface AttachmentTypeValidator
{
/**
* Validate a given file to ensure it's a valid type.
*
* @param string $filePath The path to the uploaded file to validate.
* @param AttachmentType $type The attachment type mapped to the file.
* @param User $uploadingUser The user uploading the attachment.
*
* @return boolean Whether the file is valid.
*
* @throws AttachmentTypeValidationException Can be thrown to provide more detail of failure.
*/
public function validate($filePath, AttachmentType $type, User $uploadingUser);
}
47 changes: 47 additions & 0 deletions app/Attachments/AttachmentTypeValidators/ImageTypeValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Attachment type validator for image files.
*
* @author MyBB Group
* @version 2.0.0
* @package mybb/core
* @copyright Copyright (c) 2015, MyBB Group
* @license http://www.mybb.com/licenses/bsd3 BSD-3
* @link http://www.mybb.com
*/

namespace MyBB\Core\Attachments\AttachmentTypeValidators;

use MyBB\Core\Attachments\AttachmentTypeValidationException;
use MyBB\Core\Attachments\AttachmentTypeValidator;
use MyBB\Core\Attachments\Database\Models\AttachmentType;
use MyBB\Core\Database\Models\User;

class ImageTypeValidator implements AttachmentTypeValidator
{
/**
* Validate a given file to ensure it's a valid type.
*
* This validator checks if a file is a valid image using `exif_imagetype`.
*
* @param string $filePath The path to the uploaded file to validate.
* @param AttachmentType $type The attachment type mapped to the file.
* @param User $uploadingUser The user uploading the attachment.
*
* @return bool Whether the file is valid.
*
* @throws AttachmentTypeValidationException Can be thrown to provide more detail of failure.
*/
public function validate($filePath, AttachmentType $type, User $uploadingUser)
{
$imageType = exif_imagetype($filePath);

if ($imageType === false) {
throw new AttachmentTypeValidationException(trans('attachments.not_a_valid_image_file'));
}

// TODO: Any more validation?

return true;
}
}
87 changes: 87 additions & 0 deletions app/Attachments/Database/Models/Attachment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Attachment model.
*
* @author MyBB Group
* @version 2.0.0
* @package mybb/core
* @copyright Copyright (c) 2015, MyBB Group
* @license http://www.mybb.com/licenses/bsd3 BSD-3
* @link http://www.mybb.com
*/

namespace MyBB\Core\Attachments\Database\Models;

use Illuminate\Database\Eloquent\Model;
use MyBB\Core\Database\Models\User;

/**
* @property int id
* @property int user_id
* @property int attachment_type_id
* @property string title
* @property string description
* @property string file_name
* @property string file_path
* @property int file_size
* @property string file_hash
* @property int num_downloads
* @property \Carbon\Carbon deleted_at
* @property \Carbon\Carbon created_at
* @property \Carbon\Carbon updated_at
*/
class Attachment extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'attachments';

/**
* The attributes that aren't mass assignable.
*
* @var array
*/
protected $guarded = [];

/**
* The relations to eager load on every query.
*
* @var array
*/
protected $with = ['user', 'type'];

/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'user_id' => 'integer',
'attachment_type_id' => 'integer',
'file_size' => 'integer',
'num_downloads' => 'integer',
];

/**
* An attachment belongs to a single user.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo(User::class);
}

/**
* An attachment belongs to a single attachment type.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function type()
{
return $this->belongsTo(AttachmentType::class, 'attachment_type_id');
}
}
69 changes: 69 additions & 0 deletions app/Attachments/Database/Models/AttachmentType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Attachment type model.
*
* @author MyBB Group
* @version 2.0.0
* @package mybb/core
* @copyright Copyright (c) 2015, MyBB Group
* @license http://www.mybb.com/licenses/bsd3 BSD-3
* @link http://www.mybb.com
*/

namespace MyBB\Core\Attachments\Database\Models;

use Illuminate\Database\Eloquent\Model;

/**
* @property int id
* @property string name
* @property array mime_types
* @property array file_extensions
* @property int max_file_size
* @property string validation_class
* @property \Carbon\Carbon created_at
* @property \Carbon\Carbon updated_at
*/
class AttachmentType extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'attachment_types';

/**
* The attributes that aren't mass assignable.
*
* @var array
*/
protected $guarded = [];

/**
* The relations to eager load on every query.
*
* @var array
*/
protected $with = ['attachments'];

/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'mime_types' => 'array',
'file_extensions' => 'array',
];

/**
* An attachment belongs to a single user.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function attachments()
{
return $this->hasMany(Attachment::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
/**
* Attachment repository contract.
*
* @author MyBB Group
* @version 2.0.0
* @package mybb/core
* @copyright Copyright (c) 2015, MyBB Group
* @license http://www.mybb.com/licenses/bsd3 BSD-3
* @link http://www.mybb.com
*/

namespace MyBB\Core\Attachments\Database\Repositories;

use MyBB\Core\Database\Repositories\RepositoryInterface;

interface AttachmentRepositoryInterface extends RepositoryInterface
{
/**
* Add a new download to the total downloads count for an integer.
*
* @param int $attachmentId The attachment to add a new download to the count for.
*
* @return int The number of affected rows.
*/
public function addDownload($attachmentId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Attachment repository using Eloquent ORM.
*
* @author MyBB Group
* @version 2.0.0
* @package mybb/core
* @copyright Copyright (c) 2015, MyBB Group
* @license http://www.mybb.com/licenses/bsd3 BSD-3
* @link http://www.mybb.com
*/

namespace MyBB\Core\Attachments\Database\Repositories\Eloquent;

use MyBB\Core\Attachments\Database\Models\Attachment;
use MyBB\Core\Attachments\Database\Repositories\AttachmentRepositoryInterface;

class AttachmentRepsoitory implements AttachmentRepositoryInterface
{
/**
* @var Attachment $model
*/
private $model;

/**
* @param Attachment $model
*/
public function __construct(Attachment $model)
{
$this->model = $model;
}

/**
* Add a new download to the total downloads count for an integer.
*
* @param int $attachmentId The attachment to add a new download to the count for.
*
* @return int The number of affected rows.
*/
public function addDownload($attachmentId)
{
return $this->model->newQuery()->where('id', $attachmentId)->increment('num_downloads');
}

/**
* @param int $id
*
* @return Attachment
*/
public function find($id)
{
return $this->model->findOrFail($id);
}
}