Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development #12

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ Homestead.yaml
npm-debug.log
yarn-error.log
.env
.phpunit.result.cache
.phpunit.result.cache
39 changes: 39 additions & 0 deletions app/File.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;

class File extends Model
{
/** @inheritdoc */
protected $table = [
'file_id',
'cf_account_id',
'cdn_url',
'filename',
's3_path',
'cf_entry_id'
];

/** @inheritdoc */
public $timestamps = true;

/** @inheritdoc */
public function toArray()
{

$array = [
'id' => $this->id,
'account_id' => $this->getAccountId(),
's3_id' => $this->getS3Id(),
'cdn_url' => $this->getCdnUrl()
];

return $array;

}

}
145 changes: 145 additions & 0 deletions app/Http/Controllers/s3FilesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Responses\Factory as ResponseFactory;
use App\File as FileModel;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;


class s3FilesController extends Controller
{

/**
* Upload File
*
* @param Request $request
*
* @return \Illuminate\Http\Response
*/
public function upload( Request $request )
{

try {

if ($request->hasFile('file')) {

$filename = $request->filename;
$account = $request->account;
$entry_id = $request->entry_id;
$path = $request->file('file')->store( $account . '/' . $entry_id );
$url = Storage::url($path);
$file_id = sha1( $path . $request->file('file') );

DB::table('files')->insertGetId(
[
'file_id' => $file_id,
'cf_account_id' => $account,
's3_path' => $path,
'cdn_url' => $url,
'filename' => $filename,
'cf_entry_id' => $entry_id,
'created_at' => date("Y-m-d H:i:s")
]
);

return response()->json([
'file' => $file_id,
'filename' => $filename
]);

}


} catch ( Unauthorized $e ) {
return ResponseFactory::exceptionToResponse( $e );
}


}

/**
* Delete File
*
* @param Request $request $file_id
*
* @return \Illuminate\Http\Response
*/
public function delete( Request $request, $file_id )
{

try {

$s3_path = DB::table('files')->where('file_id', $file_id )->value('s3_path');

if ( $s3_path ) {

$db_deleted = DB::table('files')->where('file_id', $file_id )->delete();

$storage_deleted = Storage::delete( $s3_path );

return response()->json([
'db_deleted' => $db_deleted,
'storage_deleted' => $storage_deleted
]);

} else {
return response()->json([
'no_file' => 'Nothing Found'
]);
}


} catch ( Unauthorized $e ) {
return ResponseFactory::exceptionToResponse( $e );
}


}

/**
* Retrieve File
*
* @param Request $request $file_id
*
* @return \Illuminate\Http\Response
*/
public function retrieve( Request $request, $file_id )
{

try {

$s3_path = DB::table('files')->where('file_id', $file_id )->value('s3_path');

if ( $s3_path ) {

$filename = DB::table('files')->where('file_id', $file_id )->value('filename');
$url = DB::table('files')->where('file_id', $file_id )->value('cdn_url');
$cf_entry_id = DB::table('files')->where('file_id', $file_id )->value('cf_entry_id');


return response()->json([
'filename' => $filename,
'url' => $url,
'cf_entry_id' => $cf_entry_id
]);

} else {
return response()->json([
'no_file' => 'Nothing Found'
]);
}


} catch ( Unauthorized $e ) {
return ResponseFactory::exceptionToResponse( $e );
}


}


}
2 changes: 2 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Kernel extends HttpKernel
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\TrustProxies::class,
\App\Http\Middleware\Cors::class
];

/**
Expand Down Expand Up @@ -59,5 +60,6 @@ class Kernel extends HttpKernel
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'cors' => \App\Http\Middleware\Cors::class
];
}
26 changes: 26 additions & 0 deletions app/Http/Middleware/Cors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Http\Middleware;

use Closure;

class Cors
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$response = $next($request);
$response->headers->set('Access-Control-Allow-Origin' , 'http://localhost:3000');
$response->header( 'Access-Control-Allow-Headers', 'Authorization, Content-Type, X-Auth-Token, Origin, X-CS-TOKEN, X-CS-PUBLIC, X-HI-ROY, X-Requested-With, X-CSRF-TOKEN');
$response->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
$response->header('Access-Control-Allow-Credentials', 'true');
return $response;
}

}
62 changes: 62 additions & 0 deletions app/Http/Responses/Arrayed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php


namespace App\Http\Responses;
use Illuminate\Support\Collection;

/**
* Class Arrayed
*
* Acts as a recursive toArray() on collections
*
* @package App\Responses
*/
class Arrayed {

/** @var array */
public $responseData;

/** @var Collection */
public $collection;

/**
* Arrayed constructor.
*
* @param Collection $collection
*/
public function __construct( Collection $collection )
{
$this->collection = $collection;
$this->responseData = [];
}

/**
* Get response data (acts as lazy-collector)
*
* @return array
*/
public function getResponseData() : array
{
if( empty( $this->responseData ) ){
$this->setResponseData();
}

return $this->responseData;
}

/**
* Sets the responseData property
*/
public function setResponseData()
{
$this->collection->each( function ($item, $key) {
if ( method_exists( $item, 'toArray') ) {
$this->responseData[] = $item->toArray();
}else{
$this->responseData[] = $item;
}
});

}

}
Loading