2b149560 by Jeff Balicki

filestorage

1 parent 824521f7
1 <?php
2
3 namespace App;
4
5 use Illuminate\Database\Eloquent\Model;
6
7 class Fileentry extends Model
8 {
9 //
10 }
1 <?php namespace App\Http\Controllers;
2
3 use App\Http\Controllers\Controller;
4 use App\Fileentry;
5 use Request;
6
7 use Illuminate\Support\Facades\Storage;
8 use Illuminate\Support\Facades\File;
9 use Illuminate\Http\Response;
10
11 class FileEntryController extends Controller {
12
13 /**
14 * Display a listing of the resource.
15 *
16 * @return Response
17 */
18 public function index()
19 {
20 $entries = Fileentry::all();
21
22 return view('fileentries.index', compact('entries'));
23 }
24
25 public function add() {
26
27 $file = Request::file('filefield');
28 $extension = $file->getClientOriginalExtension();
29 Storage::disk('local')->put($file->getFilename().'.'.$extension, File::get($file));
30 $entry = new Fileentry();
31 $entry->mime = $file->getClientMimeType();
32 $entry->original_filename = $file->getClientOriginalName();
33 $entry->filename = $file->getFilename().'.'.$extension;
34
35 $entry->save();
36
37 return redirect('fileentry');
38
39 }
40 public function get($filename){
41
42 $entry = Fileentry::where('filename', '=', $filename)->firstOrFail();
43 $file = Storage::disk('local')->get($entry->filename);
44
45 return (new Response($file, 200))
46 ->header('Content-Type', $entry->mime);
47 }
48 }
1 <?php
2
3 namespace App\Http\Controllers;
4
5 use Illuminate\Http\Request;
6 use App\Http\Requests;
7 use App\Http\Controllers;
8
9 use URL;
10 use Storage;
11 use Response;
12 use File;
13 Use PDF;
14 use Illuminate\Support\Facades\Input;
15 use Intervention\Image\Facades\Image;
16 use Illuminate\Support\Facades\Facade;
17
18 use League\Flysystem\Filesystem;
19
20
21
22
23
24 class FilesController extends Controller
25 {
26
27 /**
28 * Previews an image if possible.
29 *
30 * @param File $file
31 * @return mixed
32 * @internal param $id
33 */
34 public function preview(File $file)
35 {
36 $path = storage_path('app/') . $file->path . $file->name_thumbnail;
37 $handler = new File($path);
38
39 $lifetime = 31556926; // One year in seconds
40
41 /**
42 * Prepare some header variables
43 */
44 $file_time = $handler->getMTime(); // Get the last modified time for the file (Unix timestamp)
45
46 $header_content_type = $handler->getMimeType();
47 $header_content_length = $handler->getSize();
48 $header_etag = md5($file_time . $path);
49 $header_last_modified = gmdate('r', $file_time);
50 $header_expires = gmdate('r', $file_time + $lifetime);
51
52 $headers = array(
53 'Content-Disposition' => 'inline; filename="' . $file->name_thumbnail . '"',
54 'Last-Modified' => $header_last_modified,
55 'Cache-Control' => 'must-revalidate',
56 'Expires' => $header_expires,
57 'Pragma' => 'public',
58 'Etag' => $header_etag
59 );
60
61 /**
62 * Is the resource cached?
63 */
64 $h1 = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $_SERVER['HTTP_IF_MODIFIED_SINCE'] == $header_last_modified;
65 $h2 = isset($_SERVER['HTTP_IF_NONE_MATCH']) && str_replace('"', '', stripslashes($_SERVER['HTTP_IF_NONE_MATCH'])) == $header_etag;
66
67 if ($h1 || $h2) {
68 return Response::make('', 304, $headers); // File (image) is cached by the browser, so we don't have to send it again
69 }
70
71 $headers = array_merge($headers, array(
72 'Content-Type' => $header_content_type,
73 'Content-Length' => $header_content_length
74 ));
75
76 return Response::make(file_get_contents($path), 200, $headers);
77 }
78
79 }
...\ No newline at end of file ...\ No newline at end of file
...@@ -29,10 +29,9 @@ Route::group(array('prefix' => 'api/v1'), function($json) ...@@ -29,10 +29,9 @@ Route::group(array('prefix' => 'api/v1'), function($json)
29 29
30 }); 30 });
31 31
32 Route::get('files/{file}', 'ApiController@view'); 32 Route::get('fileentry', 'FileEntryController@index');
33 Route::get('fileentry/get/{filename}', [
34 'as' => 'getentry', 'uses' => 'FileEntryController@get']);
35 Route::post('fileentry/add',[
36 'as' => 'addentry', 'uses' => 'FileEntryController@add']);
33 37
...\ No newline at end of file ...\ No newline at end of file
34 //Route::get('files/{file}', ['as' => 'file_preview', 'uses' => 'FApiController@getApi']);
35
36 //Route::resource('js/interface.js', function () {
37 // return asset('js/interface.js');
38 //});
......
1 <?php
2
3 use Illuminate\Database\Schema\Blueprint;
4 use Illuminate\Database\Migrations\Migration;
5
6 class CreateFileentriesTable extends Migration
7 {
8 /**
9 * Run the migrations.
10 *
11 * @return void
12 */
13 public function up()
14 {
15 Schema::create('fileentries', function(Blueprint $table)
16 {
17 $table->increments('id');
18 $table->string('filename');
19 $table->string('mime');
20 $table->string('original_filename');
21 $table->timestamps();
22 });
23 }
24
25 /**
26 * Reverse the migrations.
27 *
28 * @return void
29 */
30 public function down()
31 {
32 Schema::drop('fileentries');
33 }
34 }
1 <?php
2 @extends('app')
3 @section('content')
4
5 <form action="{{route('addentry', [])}}" method="post" enctype="multipart/form-data">
6 <input type="file" name="filefield">
7 <input type="submit">
8 </form>
9
10 <h1> Pictures list</h1>
11 <div class="row">
12 <ul class="thumbnails">
13 @foreach($entries as $entry)
14 <div class="col-md-2">
15 <div class="thumbnail">
16 <img src="{{route('getentry', $entry->filename)}}" alt="ALT NAME" class="img-responsive" />
17 <div class="caption">
18 <p>{{$entry->original_filename}}</p>
19 </div>
20 </div>
21 </div>
22 @endforeach
23 </ul>
24 </div>
25
26 @endsection
...\ No newline at end of file ...\ No newline at end of file