diff --git a/controllers/UploadMedia.php b/controllers/UploadMedia.php index 5c1e3ef..c1d9df1 100644 --- a/controllers/UploadMedia.php +++ b/controllers/UploadMedia.php @@ -14,45 +14,44 @@ class UploadMedia extends HTMLController if (!Registry::get('user')->isAdmin()) throw new NotAllowedException(); - $page = new MediaUploader(); - parent::__construct('Upload new media - ' . SITE_TITLE); + if (!isset($_REQUEST['tag'])) + throw new UserFacingException('No album tag provided.'); + + $tag = Tag::fromId($_REQUEST['tag']); + + $page = new MediaUploader($tag); + parent::__construct('Upload new photos - ' . SITE_TITLE); $this->page->adopt($page); // Are we saving something? - if (isset($_POST['save'])) + if (!empty($_FILES['uploads'])) { - if (empty($_FILES) || empty($_FILES['new_asset'])) - return; - - // Any tags? - $new_tags = []; - if (isset($_POST['tag']) && is_array($_POST['tag'])) - { - foreach ($_POST['tag'] as $id_tag => $bool) - if (is_numeric($id_tag)) - $new_tags[] = $id_tag; - } - -var_dump($_FILES); -var_dump($_POST); - - foreach ($_FILES['new_asset']['tmp_name'] as $num => $uploaded_file) + $files = isset($_FILES['uploads'][0]) ? $_FILES['uploads'] : [$_FILES['uploads']]; + $new_ids = []; + foreach ($files as $num => $uploaded_file) { if (empty($uploaded_file)) continue; $asset = Asset::createNew([ - 'filename_to_copy' => $uploaded_file, - 'preferred_filename' => $_FILES['new_asset']['name'][$num], - 'title' => !empty($_POST['title'][$num]) ? $_POST['title'][$num] : null, + 'filename_to_copy' => $uploaded_file['tmp_name'], + 'preferred_filename' => $uploaded_file['name'], ]); - $asset->linkTags($new_tags); + $new_ids[] = $asset->getId(); + $asset->linkTags([$tag->id_tag]); } - // Prevent uploading twice. - header('Location: ' . BASEURL . '/uploadmedia/'); - exit; + if (isset($_REQUEST['format']) && $_REQUEST['format'] === 'json') + { + echo json_encode(['success' => true, 'new_ids' => $new_ids]); + exit; + } + else + { + header('Location: ' . BASEURL . '/uploadmedia/'); + exit; + } } } } diff --git a/models/Asset.php b/models/Asset.php index f005020..d03f12a 100644 --- a/models/Asset.php +++ b/models/Asset.php @@ -194,7 +194,7 @@ class Asset unset($image); $exif = EXIF::fromFile($destination); - $date_captured = intval($exif->created_timestamp); + $date_captured = $exif->created_timestamp > 0 ? $exif->created_timestamp : time(); } $db = Registry::get('db'); diff --git a/public/css/default.css b/public/css/default.css index 6e9c887..d800fc1 100644 --- a/public/css/default.css +++ b/public/css/default.css @@ -694,6 +694,9 @@ a#previous_photo:hover, a#next_photo:hover { height: 5px; width: 0%; } +#upload_preview_area .progress .error { + background: #b22; +} /* Spinner animation ----------------------*/ diff --git a/public/js/upload_queue.js b/public/js/upload_queue.js index 21b8ffd..bdc3a70 100644 --- a/public/js/upload_queue.js +++ b/public/js/upload_queue.js @@ -2,6 +2,7 @@ function UploadQueue(options) { this.queue = options.queue_element; this.preview_area = options.preview_area; this.upload_progress = []; + this.upload_url = options.upload_url; this.submit = options.submit_button; this.addEvents(); } @@ -69,13 +70,45 @@ UploadQueue.prototype.process = function() { var files = this.queue.files; for (var i = 0; i < files.length; i++) { this.setSpinnerLabel("Uploading file " + (i + 1) + " out of " + files.length); - this.updateProgress(i, 0.5); var callback = (i !== files.length - 1) ? null : function() { this.hideSpinner(); }; + this.sendFile(files[i], i, callback); }; }; + +UploadQueue.prototype.sendFile = function(file, index, callback) { + // Prepare the request. + var that = this; + var request = new XMLHttpRequest(); + request.addEventListener('error', function(event) { + that.updateProgress(index, -1); + }); + request.addEventListener('progress', function(event) { + that.updateProgress(index, event.loaded / event.total); + }); + request.addEventListener('load', function(event) { + that.updateProgress(index, 1); + if (request.responseText !== null && request.status === 200) { + var obj = JSON.parse(request.responseText); + if (obj.error) { + alert(obj.error); + return; + } + else if (callback) { + callback.call(that, obj); + } + } + }); + + var data = new FormData(); + data.append('uploads', file, file.name); + + request.open('POST', this.upload_url, true); + request.send(data); +}; + UploadQueue.prototype.addProgressBar = function(index) { if (index in this.upload_progress) { return; @@ -99,7 +132,15 @@ UploadQueue.prototype.updateProgress = function(index, progress) { } var bar = this.upload_progress[index]; - bar.style.width = Math.ceil(progress * 100) + '%'; + + if (progress >= 0) { + bar.style.width = Math.ceil(progress * 100) + '%'; + } else { + bar.style.width = ""; + if (progress === -1) { + bar.className = "error"; + } + } }; UploadQueue.prototype.showSpinner = function(sibling, label) { diff --git a/templates/MediaUploader.php b/templates/MediaUploader.php index 3a3d97e..7df2c4e 100644 --- a/templates/MediaUploader.php +++ b/templates/MediaUploader.php @@ -8,11 +8,16 @@ class MediaUploader extends SubTemplate { + public function __construct(Tag $tag) + { + $this->tag = $tag; + } + protected function html_content() { echo ' -