FEATURE: add file size to uploaded files
This commit is contained in:
+10
-14
@@ -24,12 +24,8 @@ class File extends Model
|
||||
*/
|
||||
public $timestamps = true;
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'filename',
|
||||
'original_filename',
|
||||
'file_hash',
|
||||
'hash_algorithm',
|
||||
'mime_type',
|
||||
'description',
|
||||
'keywords',
|
||||
'directory'
|
||||
@@ -97,25 +93,25 @@ class File extends Model
|
||||
$hash = hash_file(self::HASH_ALGORITHM, $path);
|
||||
|
||||
// define mandatory fields
|
||||
$params = [
|
||||
'user_id' => Auth::user()->id,
|
||||
'filename' => $file->getFilename(),
|
||||
'original_filename' => $originalFilename,
|
||||
'file_hash' => $hash,
|
||||
'hash_algorithm' => self::HASH_ALGORITHM,
|
||||
'mime_type' => $file->getMimeType(),
|
||||
];
|
||||
$newFile = new File();
|
||||
$newFile->user_id = Auth::user()->id;
|
||||
$newFile->filename = $file->getFilename();
|
||||
$newFile->original_filename = $originalFilename;
|
||||
$newFile->file_hash = $hash;
|
||||
$newFile->hash_algorithm = self::HASH_ALGORITHM;
|
||||
$newFile->mime_type = $file->getMimeType();
|
||||
$newFile->file_size = $file->getSize();
|
||||
|
||||
// save optional fields
|
||||
$optionalFields = ['description', 'keywords', 'directory'];
|
||||
foreach ($optionalFields as $field) {
|
||||
if ($request->has($field)) {
|
||||
$params[$field] = $request->$field;
|
||||
$newFile->$field = $request->$field;
|
||||
}
|
||||
}
|
||||
|
||||
// create new database row
|
||||
$newFile = File::create($params);
|
||||
$newFile->save();
|
||||
|
||||
return $newFile->id;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
|
||||
class AddFileSizeTofiles extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('files', function (Blueprint $table) {
|
||||
$table->integer('file_size')
|
||||
->after('mime_type')
|
||||
->comment('Filesize given in bytes');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('files', function (Blueprint $table) {
|
||||
$table->dropColumn('file_size');
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user