-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a simple UI for file up-/download
- Loading branch information
Showing
7 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
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,12 @@ | ||
<h3>Upload a file</h3> | ||
<form action="{action}" enctype="multipart/form-data" method="POST"> | ||
<input type="file" name="{fileField}" accept="*/*"> | ||
<input type="submit" value="Upload"> | ||
</form> | ||
|
||
<h3>Uploaded files (click to download)</h3> | ||
<ol> | ||
<f:for each="{files}" as="file"> | ||
<li><a href="content.php?uuid={file.uuid}&dl">{file.name}</a>: {file.mimetype} ({file.size} bytes)</li> | ||
</f:for> | ||
</ol> |
Empty file.
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,12 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>{title}</title> | ||
</head> | ||
|
||
<body> | ||
<h2>{title}</h2> | ||
<f:render partial="{partial}" arguments="{_all}" /> | ||
</body> | ||
</html> |
Empty file.
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,33 @@ | ||
<?php | ||
|
||
use TYPO3Fluid\Fluid\View\TemplateView; | ||
use Entity\File; | ||
|
||
const UUID_PARAM = 'uuid'; | ||
|
||
require_once __DIR__.'/../vendor/autoload.php'; | ||
require_once __DIR__.'/../bootstrap.php'; | ||
|
||
try { | ||
// Get the requested file | ||
$query = $entityManager->createQueryBuilder() | ||
->select('f') | ||
->from('Entity\File', 'f') | ||
->where('f.uuid = :uuid') | ||
->setParameter('uuid', $_GET['uuid']) | ||
->getQuery(); | ||
$file = $query->getSingleResult(); | ||
|
||
// Build headers for inline or downloaded content | ||
header('Content-Type: ' . $file->getMimetype()); | ||
if (isset($_GET['dl'])) | ||
{ | ||
header('Content-Disposition: attachment; filename="' . $file->getName() . '"'); | ||
} | ||
|
||
// Serve the content | ||
fpassthru($file->getContent()); | ||
|
||
} catch (Exception $ex) { | ||
header('Content-Type: application/octet-stream', true, 404); | ||
} |
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,54 @@ | ||
<?php | ||
|
||
use TYPO3Fluid\Fluid\View\TemplateView; | ||
use Entity\File; | ||
|
||
require_once __DIR__.'/../vendor/autoload.php'; | ||
require_once __DIR__.'/../bootstrap.php'; | ||
|
||
const FILE_FIELD = 'file'; | ||
|
||
$view = new TemplateView(); | ||
$paths = $view->getTemplatePaths(); | ||
$paths->setPartialRootPaths([ __DIR__.'/../Resources/Private/Partials' ]); | ||
$paths->setTemplatePathAndFilename(__DIR__.'/../Resources/Private/Templates/Page.html'); | ||
|
||
switch ($_SERVER['REQUEST_METHOD']) { | ||
case 'POST': | ||
// Persist the uploaded file | ||
$file = $_FILES[FILE_FIELD]; | ||
$entity = new File($file); | ||
$entityManager->persist($entity); | ||
$entityManager->flush(); | ||
|
||
// Prevent re-POST if browser is refreshed | ||
header('Location: ' . $_SERVER['PHP_SELF']); | ||
exit; | ||
|
||
case 'GET': | ||
// Query all uploaded files | ||
$query = $entityManager->createQueryBuilder() | ||
->select('file') | ||
->from('Entity\File', 'file') | ||
->orderBy('file.name', 'ASC') | ||
->getQuery(); | ||
$files = $query->getArrayResult(); | ||
|
||
// Prepare the model for Fluid | ||
$model = [ | ||
'title' => 'File storage', | ||
'partial' => 'Files', | ||
'files' => $files, | ||
'action' => $_SERVER['PHP_SELF'], | ||
'fileField' => FILE_FIELD, | ||
]; | ||
break; | ||
|
||
default: | ||
// Bad request | ||
header('Content-Type: application/octet-stream', true, 400); | ||
exit; | ||
} | ||
|
||
$view->assignMultiple($model); | ||
echo $view->render(); |