2b149560 by Jeff Balicki

filestorage

1 parent 824521f7
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Fileentry extends Model
{
//
}
<?php namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Fileentry;
use Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
use Illuminate\Http\Response;
class FileEntryController extends Controller {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$entries = Fileentry::all();
return view('fileentries.index', compact('entries'));
}
public function add() {
$file = Request::file('filefield');
$extension = $file->getClientOriginalExtension();
Storage::disk('local')->put($file->getFilename().'.'.$extension, File::get($file));
$entry = new Fileentry();
$entry->mime = $file->getClientMimeType();
$entry->original_filename = $file->getClientOriginalName();
$entry->filename = $file->getFilename().'.'.$extension;
$entry->save();
return redirect('fileentry');
}
public function get($filename){
$entry = Fileentry::where('filename', '=', $filename)->firstOrFail();
$file = Storage::disk('local')->get($entry->filename);
return (new Response($file, 200))
->header('Content-Type', $entry->mime);
}
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers;
use URL;
use Storage;
use Response;
use File;
Use PDF;
use Illuminate\Support\Facades\Input;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Facade;
use League\Flysystem\Filesystem;
class FilesController extends Controller
{
/**
* Previews an image if possible.
*
* @param File $file
* @return mixed
* @internal param $id
*/
public function preview(File $file)
{
$path = storage_path('app/') . $file->path . $file->name_thumbnail;
$handler = new File($path);
$lifetime = 31556926; // One year in seconds
/**
* Prepare some header variables
*/
$file_time = $handler->getMTime(); // Get the last modified time for the file (Unix timestamp)
$header_content_type = $handler->getMimeType();
$header_content_length = $handler->getSize();
$header_etag = md5($file_time . $path);
$header_last_modified = gmdate('r', $file_time);
$header_expires = gmdate('r', $file_time + $lifetime);
$headers = array(
'Content-Disposition' => 'inline; filename="' . $file->name_thumbnail . '"',
'Last-Modified' => $header_last_modified,
'Cache-Control' => 'must-revalidate',
'Expires' => $header_expires,
'Pragma' => 'public',
'Etag' => $header_etag
);
/**
* Is the resource cached?
*/
$h1 = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $_SERVER['HTTP_IF_MODIFIED_SINCE'] == $header_last_modified;
$h2 = isset($_SERVER['HTTP_IF_NONE_MATCH']) && str_replace('"', '', stripslashes($_SERVER['HTTP_IF_NONE_MATCH'])) == $header_etag;
if ($h1 || $h2) {
return Response::make('', 304, $headers); // File (image) is cached by the browser, so we don't have to send it again
}
$headers = array_merge($headers, array(
'Content-Type' => $header_content_type,
'Content-Length' => $header_content_length
));
return Response::make(file_get_contents($path), 200, $headers);
}
}
\ No newline at end of file
......@@ -29,10 +29,9 @@ Route::group(array('prefix' => 'api/v1'), function($json)
});
Route::get('files/{file}', 'ApiController@view');
Route::get('fileentry', 'FileEntryController@index');
Route::get('fileentry/get/{filename}', [
'as' => 'getentry', 'uses' => 'FileEntryController@get']);
Route::post('fileentry/add',[
'as' => 'addentry', 'uses' => 'FileEntryController@add']);
\ No newline at end of file
//Route::get('files/{file}', ['as' => 'file_preview', 'uses' => 'FApiController@getApi']);
//Route::resource('js/interface.js', function () {
// return asset('js/interface.js');
//});
......
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFileentriesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('fileentries', function(Blueprint $table)
{
$table->increments('id');
$table->string('filename');
$table->string('mime');
$table->string('original_filename');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('fileentries');
}
}
<?php
@extends('app')
@section('content')
<form action="{{route('addentry', [])}}" method="post" enctype="multipart/form-data">
<input type="file" name="filefield">
<input type="submit">
</form>
<h1> Pictures list</h1>
<div class="row">
<ul class="thumbnails">
@foreach($entries as $entry)
<div class="col-md-2">
<div class="thumbnail">
<img src="{{route('getentry', $entry->filename)}}" alt="ALT NAME" class="img-responsive" />
<div class="caption">
<p>{{$entry->original_filename}}</p>
</div>
</div>
</div>
@endforeach
</ul>
</div>
@endsection
\ No newline at end of file