FEATURE: add file size to uploaded files

This commit is contained in:
Michal Kváček
2016-02-08 14:50:44 +01:00
parent eb28696d82
commit e98032a3e1
2 changed files with 43 additions and 14 deletions
+10 -14
View File
@@ -24,12 +24,8 @@ class File extends Model
*/ */
public $timestamps = true; public $timestamps = true;
protected $fillable = [ protected $fillable = [
'user_id',
'filename', 'filename',
'original_filename', 'original_filename',
'file_hash',
'hash_algorithm',
'mime_type',
'description', 'description',
'keywords', 'keywords',
'directory' 'directory'
@@ -97,25 +93,25 @@ class File extends Model
$hash = hash_file(self::HASH_ALGORITHM, $path); $hash = hash_file(self::HASH_ALGORITHM, $path);
// define mandatory fields // define mandatory fields
$params = [ $newFile = new File();
'user_id' => Auth::user()->id, $newFile->user_id = Auth::user()->id;
'filename' => $file->getFilename(), $newFile->filename = $file->getFilename();
'original_filename' => $originalFilename, $newFile->original_filename = $originalFilename;
'file_hash' => $hash, $newFile->file_hash = $hash;
'hash_algorithm' => self::HASH_ALGORITHM, $newFile->hash_algorithm = self::HASH_ALGORITHM;
'mime_type' => $file->getMimeType(), $newFile->mime_type = $file->getMimeType();
]; $newFile->file_size = $file->getSize();
// save optional fields // save optional fields
$optionalFields = ['description', 'keywords', 'directory']; $optionalFields = ['description', 'keywords', 'directory'];
foreach ($optionalFields as $field) { foreach ($optionalFields as $field) {
if ($request->has($field)) { if ($request->has($field)) {
$params[$field] = $request->$field; $newFile->$field = $request->$field;
} }
} }
// create new database row // create new database row
$newFile = File::create($params); $newFile->save();
return $newFile->id; 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');
});
}
}