Skip to content
This repository has been archived by the owner on Jan 7, 2025. It is now read-only.

Commit

Permalink
Merge pull request #57 from attakei/feature/fileupload
Browse files Browse the repository at this point in the history
ファイルアップロード
  • Loading branch information
attakei authored Jul 29, 2016
2 parents f64a0f6 + 1a5f222 commit f440db0
Show file tree
Hide file tree
Showing 13 changed files with 699 additions and 58 deletions.
35 changes: 35 additions & 0 deletions app/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public function comments()
return $this->hasMany(\App\Comment::class, 'article_id');
}

public function attachments()
{
return $this->hasMany(\App\Attachment::class, 'article_id');
}

/**
* 紐付いているタグを、HTMLフォームに合わせるために文字列化する
* FIXME: もうちょっと直感的な方法があるはず
Expand Down Expand Up @@ -69,6 +74,36 @@ public function updateTags(array $tags)
}
}

/**
* 紐付いている添付ファイルを、HTMLフォームに合わせるために文字列化する
*
* TODO: Not testing
* FIXME: もうちょっと直感的な方法があるはず
*/
public function attachmentsForInput()
{
$text = '';
foreach ($this->attachments as $attachment) {
$text .= ',' . $attachment->id;
}
return $text == '' ? '' : mb_substr($text, 1);
}

/**
* 記事に紐づくタグを、まとめて差し替える
*
* TODO: Not testing
*/
public function possessAttachments($attachments)
{
foreach ($attachments as $attachment) {
if ($attachment->article_id != $this->id) {
$attachment->article_id = $this->id;
$attachment->save();
}
}
}

/**
* @return \Illuminate\Database\Query\Builder
*/
Expand Down
26 changes: 26 additions & 0 deletions app/Attachment.php
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;
}
}
7 changes: 7 additions & 0 deletions app/Http/Controllers/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Http\Requests;
use App\Article;
use App\ArticleTag;
use App\Attachment;
use Illuminate\Http\Request;
use Maknz\Slack\Facades\Slack;

Expand Down Expand Up @@ -111,6 +112,12 @@ public function postOne(Request $request)
if (count($tagBodySet) > 1 || $tagBodySet[0] != '') {
$article->updateTags($tagBodySet);
}

$attachmentIds = explode(',', $request->input('attachmentIds', ''));
if (count($attachmentIds) > 1 || $attachmentIds[0] != '') {
$attachments = Attachment::whereIn('id', $attachmentIds)->get();
$article->possessAttachments($attachments);
}
$article->save();
$request->session()->flash('flash_message', $message);
});
Expand Down
70 changes: 70 additions & 0 deletions app/Http/Controllers/AttachmentController.php
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];
}
}
3 changes: 3 additions & 0 deletions app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@

Route::get('/tags/', ['as' => 'get_tags_list', 'uses' => 'TagController@getList']);
Route::get('/tags/{tagBody}', ['as' => 'list_by_tag', 'uses' => 'ArticleController@getListByTag']);

Route::resource('attachments', 'AttachmentController');

});


Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"cebe/markdown": "^1.1",
"laravelcollective/html": "^5.2",
"doctrine/dbal": "^2.5",
"maknz/slack": "^1.7"
"maknz/slack": "^1.7",
"ramsey/uuid": "^3.4",
"league/flysystem-aws-s3-v3": "~1.0"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
Expand Down
Loading

0 comments on commit f440db0

Please sign in to comment.