This repository has been archived by the owner on Jan 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from attakei/feature/fileupload
ファイルアップロード
- Loading branch information
Showing
13 changed files
with
699 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace App; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
use Storage; | ||
|
||
class Attachment extends Model | ||
{ | ||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = [ | ||
'owner_id', 'file_name', 'original_name', 'mime_type', 'article_id', | ||
]; | ||
|
||
public function toArray($options = 0) | ||
{ | ||
$data = parent::toArray($options); | ||
$data['url'] = Storage::url($this->file_name); | ||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Response; | ||
|
||
use Ramsey\Uuid\Uuid; | ||
|
||
use App\Http\Requests; | ||
use App\Http\Controllers\Controller; | ||
use App\Attachment; | ||
use App\Article; | ||
|
||
use Storage; | ||
use Auth; | ||
use DB; | ||
|
||
|
||
class AttachmentController extends Controller | ||
{ | ||
public function index(Request $request) | ||
{ | ||
$articleId = $request->input('articleId', null); | ||
// Currently, articleId is required. | ||
if ( is_null($articleId) ) { | ||
abort(400); | ||
} | ||
// Only article owner can get attachments | ||
$article = Article::find($articleId); | ||
if ( is_null($article) || $article->author_id != Auth::user()->id ) { | ||
abort(400); | ||
} | ||
return [ | ||
'data' => $article->attachments, | ||
]; | ||
} | ||
|
||
// | ||
public function store(Request $request) | ||
{ | ||
$file = $request->file('attachment'); | ||
if ( !$request->hasFile('attachment') ) { | ||
abort(400); | ||
} | ||
|
||
$fileName = 'attachments/' . Uuid::uuid4() . '.'. $file->getClientOriginalExtension(); | ||
// Store file | ||
Storage::disk()->put( | ||
$fileName, | ||
file_get_contents($file->getRealPath()) | ||
); | ||
$result = Storage::url($fileName); | ||
|
||
// Manage as model | ||
$attachment = Attachment::create([ | ||
'file_name' => $fileName, | ||
'original_name' => $file->getClientOriginalName(), | ||
'mime_type' => $file->getClientMimeType(), | ||
'owner_id' => Auth::user()->id, | ||
]); | ||
DB::transaction(function () use ($attachment) | ||
{ | ||
$attachment->save(); | ||
}); | ||
|
||
$data = [$attachment]; | ||
return ['data' => $data]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.