Skip to content

Commit

Permalink
Add a simple UI for file up-/download
Browse files Browse the repository at this point in the history
  • Loading branch information
undecaf committed Jun 11, 2020
1 parent b0c1269 commit 50bbc70
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 0 deletions.
Empty file.
12 changes: 12 additions & 0 deletions Resources/Private/Partials/Files.html
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}&amp;dl">{file.name}</a>: {file.mimetype} ({file.size} bytes)</li>
</f:for>
</ol>
Empty file.
12 changes: 12 additions & 0 deletions Resources/Private/Templates/Page.html
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 removed public/.gitkeep
Empty file.
33 changes: 33 additions & 0 deletions public/content.php
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);
}
54 changes: 54 additions & 0 deletions public/files.php
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();

0 comments on commit 50bbc70

Please sign in to comment.