Fixing upload bugs

This commit is contained in:
Dennis Eichhorn 2016-05-25 22:52:49 +02:00
parent 4557f2a0f8
commit 537b9291ab
6 changed files with 34 additions and 17 deletions

View File

@ -214,6 +214,7 @@ class Controller extends ModuleAbstract implements WebInterface
$path = '/Modules/Media/Files/' . $rndPath[0] . $rndPath[1] . '/' . $rndPath[2] . $rndPath[3]; $path = '/Modules/Media/Files/' . $rndPath[0] . $rndPath[1] . '/' . $rndPath[2] . $rndPath[3];
$upload->setOutputDir($path); $upload->setOutputDir($path);
$upload->setFileName(false); $upload->setFileName(false);
$status = $upload->upload($files); $status = $upload->upload($files);
$mediaCreated = $this->createDbEntries($status, $account); $mediaCreated = $this->createDbEntries($status, $account);
} }
@ -237,7 +238,7 @@ class Controller extends ModuleAbstract implements WebInterface
foreach ($status as $uFile) { foreach ($status as $uFile) {
if ($uFile['status'] === UploadStatus::OK) { if ($uFile['status'] === UploadStatus::OK) {
$media = new Media(); $media = new Media();
$media->setPath($uFile['path'] . '/' . $uFile['filename']); $media->setPath(trim($uFile['path'], '/') . '/' . $uFile['filename']);
$media->setName($uFile['filename']); $media->setName($uFile['filename']);
$media->setSize($uFile['size']); $media->setSize($uFile['size']);
$media->setCreatedBy($account); $media->setCreatedBy($account);

View File

@ -95,6 +95,7 @@
return false; return false;
} }
}); });
request.send(); request.send();
}; };
}(window.jsOMS = window.jsOMS || {})); }(window.jsOMS = window.jsOMS || {}));

View File

@ -15,6 +15,8 @@
*/ */
namespace Modules\Media\Models; namespace Modules\Media\Models;
use phpOMS\System\File\Directory;
/** /**
* Upload. * Upload.
@ -89,7 +91,7 @@ class UploadFile
} }
$this->findOutputDir($files); $this->findOutputDir($files);
$rpath = $this->outputDir; $path = ROOT_PATH . $this->outputDir;
foreach ($files as $key => $f) { foreach ($files as $key => $f) {
$result[$key] = []; $result[$key] = [];
@ -116,15 +118,13 @@ class UploadFile
} }
// TODO: do I need pecl fileinfo? // TODO: do I need pecl fileinfo?
if (!empty($this->allowedTypes) && false === $ext = array_search($f['type'], $this->allowedTypes, true)) { if (!empty($this->allowedTypes) && ($ext = array_search($f['type'], $this->allowedTypes, true)) === false) {
// wrong file format // wrong file format
$result[$key]['status'] = UploadStatus::WRONG_EXTENSION; $result[$key]['status'] = UploadStatus::WRONG_EXTENSION;
return $result; return $result;
} }
$path = __DIR__ . '/../../..' . $this->outputDir;
if ($this->preserveFileName) { if ($this->preserveFileName) {
$this->fileName = $f['name']; $this->fileName = $f['name'];
} }
@ -133,6 +133,8 @@ class UploadFile
$extension = count($split) > 1 ? $split[count($split) - 1] : ''; $extension = count($split) > 1 ? $split[count($split) - 1] : '';
$result[$key]['extension'] = $extension; $result[$key]['extension'] = $extension;
// ! and empty same?!
$result[$key]['filename'] = $this->fileName;
if (!$this->fileName || empty($this->fileName) || file_exists($path . '/' . $this->fileName)) { if (!$this->fileName || empty($this->fileName) || file_exists($path . '/' . $this->fileName)) {
$rnd = ''; $rnd = '';
@ -147,14 +149,12 @@ class UploadFile
} }
$this->fileName = $sha; $this->fileName = $sha;
$rnd = rand(); $rnd = mt_rand();
} while (file_exists($path . '/' . $this->fileName)); } while (file_exists($path . '/' . $this->fileName));
} }
$result[$key]['filename'] = $this->fileName;
if (!is_dir($path)) { if (!is_dir($path)) {
\mkdir($path, '0655', true); Directory::createPath($path, '0655', true);
} }
if (!is_uploaded_file($f['tmp_name'])) { if (!is_uploaded_file($f['tmp_name'])) {
@ -169,7 +169,7 @@ class UploadFile
return $result; return $result;
} }
$result[$key]['path'] = $rpath; $result[$key]['path'] = $this->outputDir;
} }
return $result; return $result;
@ -192,7 +192,7 @@ class UploadFile
$rndPath = str_pad(dechex(rand(0, 65535)), 4, '0', STR_PAD_LEFT); $rndPath = str_pad(dechex(rand(0, 65535)), 4, '0', STR_PAD_LEFT);
} while (file_exists($this->outputDir . '/' . $rndPath)); } while (file_exists($this->outputDir . '/' . $rndPath));
$this->outputDir = $this->outputDir . '/' . $rndPath; $this->outputDir = '/../../..' . $this->outputDir . '/' . $rndPath;
} }
} }

View File

@ -21,7 +21,7 @@ echo $this->getData('nav')->render(); ?>
<section class="box w-50"> <section class="box w-50">
<header><h1><?= $this->l11n->lang['Media']['Upload']; ?></h1></header> <header><h1><?= $this->l11n->lang['Media']['Upload']; ?></h1></header>
<div class="inner"> <div class="inner">
<form method="POST" action="<?= \phpOMS\Uri\UriFactory::build('/{/lang}/api/media/create'); ?>"> <form method="POST" id="media-uploader" action="<?= \phpOMS\Uri\UriFactory::build('/{/lang}/api/media'); ?>">
<table class="layout wf-100"> <table class="layout wf-100">
<tr><td><label for="iName"><?= $this->l11n->lang['Media']['Name']; ?></label> <tr><td><label for="iName"><?= $this->l11n->lang['Media']['Name']; ?></label>
<tr><td><input type="text" id="iName" name="name" placeholder="&#xf040;"> <tr><td><input type="text" id="iName" name="name" placeholder="&#xf040;">

View File

@ -17,9 +17,7 @@
* @var \phpOMS\Views\View $this * @var \phpOMS\Views\View $this
*/ */
$mediaMapper = new \Modules\Media\Models\MediaMapper($this->app->dbPool->get()); $media = \Modules\Media\Models\MediaMapper::getNewest(25);
$media = $mediaMapper->getNewest(25);
$footerView = new \Web\Views\Lists\PaginationView($this->app, $this->request, $this->response); $footerView = new \Web\Views\Lists\PaginationView($this->app, $this->request, $this->response);
$footerView->setTemplate('/Web/Templates/Lists/Footer/PaginationBig'); $footerView->setTemplate('/Web/Templates/Lists/Footer/PaginationBig');
$footerView->setPages(count($media) / 25); $footerView->setPages(count($media) / 25);

View File

@ -16,6 +16,9 @@
/** /**
* @var \phpOMS\Views\View $this * @var \phpOMS\Views\View $this
*/ */
/**
* @var $media \Modules\Media\Models\Media
*/
$media = $this->getData('media'); $media = $this->getData('media');
echo $this->getData('nav')->render(); echo $this->getData('nav')->render();
?> ?>
@ -23,10 +26,24 @@ echo $this->getData('nav')->render();
<section class="box w-100"> <section class="box w-100">
<header><h1><?= $media->getName() ?></h1></header> <header><h1><?= $media->getName() ?></h1></header>
<div class="inner"> <div class="inner">
<table class="list w-100">
<tbody>
<tr><td>Size<td class="wf-100"><?= $media->getSize(); ?>
<tr><td>Created at<td><?= $media->getCreatedAt()->format('Y-m-d'); ?>
<tr><td>Created by<td><?= $media->getCreatedBy(); ?>
<tr><td>Description<td><?= $media->getDescription(); ?>
<tr><td colspan="2">Content
</table>
<?php if(in_array($media->getExtension(), ['gif', 'bmp', 'jpg', 'jpeg', 'png'])) : ?> <?php if(in_array($media->getExtension(), ['gif', 'bmp', 'jpg', 'jpeg', 'png'])) : ?>
<img src="<?= $media->getPath(); ?>"> <img src="<?= $this->request->getUri()->getBase() . $media->getPath(); ?>">
<?php else : ?> <?php else : ?>
<pre><?= htmlspecialchars(file_get_contents(ROOT_PATH . '/' . $media->getPath())); ?></pre> <pre>
<?php
$output = htmlspecialchars(file_get_contents(ROOT_PATH . '/' . $media->getPath()));
$output = str_replace(["\r\n", "\r"], "\n", $output);
$output = explode("\n", $output);
foreach($output as $line) : ?><span><?= $line; ?></span><?php endforeach; ?>
</pre>
<?php endif; ?> <?php endif; ?>
</div> </div>
</section> </section>