The API response for insert / insert+crop should be identical

This is because the 'onInsert' callback is used for both actions, yet yielded different results causing breakages
Fixes #1281
Also improved code readability in some places
This commit is contained in:
Samuel Georges 2015-08-08 11:17:56 +10:00
parent fbb6a5f3f1
commit 2cf1d707ad
6 changed files with 88 additions and 51 deletions

View File

@ -467,8 +467,9 @@ class MediaLibrary
* S3 doesn't allow getting the last modified timestamp for folders,
* so this feature is disabled - folders timestamp is always NULL.
*/
$lastModified = $itemType == MediaLibraryItem::TYPE_FILE ?
$this->getStorageDisk()->lastModified($path) : null;
$lastModified = $itemType == MediaLibraryItem::TYPE_FILE
? $this->getStorageDisk()->lastModified($path)
: null;
/*
* The folder size (number of items) doesn't respect filters. That
@ -476,8 +477,9 @@ class MediaLibrary
* zero items for a folder that contains files not visible with a
* currently applied filter. -ab
*/
$size = $itemType == MediaLibraryItem::TYPE_FILE ?
$this->getStorageDisk()->size($path) : $this->getFolderItemCount($path);
$size = $itemType == MediaLibraryItem::TYPE_FILE
? $this->getStorageDisk()->size($path)
: $this->getFolderItemCount($path);
$publicUrl = $this->storagePath.$relativePath;
return new MediaLibraryItem($relativePath, $size, $lastModified, $itemType, $publicUrl);
@ -492,12 +494,14 @@ class MediaLibrary
{
$folderItems = array_merge(
$this->getStorageDisk()->files($path),
$this->getStorageDisk()->directories($path));
$this->getStorageDisk()->directories($path)
);
$size = 0;
foreach ($folderItems as $folderItem) {
if ($this->isVisible($folderItem))
if ($this->isVisible($folderItem)) {
$size++;
}
}
return $size;
@ -543,14 +547,14 @@ class MediaLibrary
usort($itemList, function($a, $b) use ($sortBy) {
switch ($sortBy) {
case self::SORT_BY_TITLE : return strcasecmp($a->path, $b->path);
case self::SORT_BY_SIZE :
case self::SORT_BY_TITLE: return strcasecmp($a->path, $b->path);
case self::SORT_BY_SIZE:
if ($a->size > $b->size)
return -1;
return $a->size < $b->size ? 1 : 0;
break;
case self::SORT_BY_MODIFIED :
case self::SORT_BY_MODIFIED:
if ($a->lastModified > $b->lastModified)
return -1;
@ -593,7 +597,8 @@ class MediaLibrary
return $this->storageDisk;
return $this->storageDisk = Storage::disk(
Config::get('cms.storage.media.disk', 'local'));
Config::get('cms.storage.media.disk', 'local')
);
}
/**

View File

@ -1,7 +1,7 @@
<?php namespace Cms\Classes;
use Config;
use File;
use Config;
/**
* Represents a file or folder in the Media Library.
@ -22,7 +22,7 @@ class MediaLibraryItem
/**
* @var string Specifies the item path relative to the Library root.
*/
public $path;
public $path;
/**
* @var integer Specifies the item size.

View File

@ -1092,56 +1092,89 @@ class MediaManager extends WidgetBase
$path = rtrim(dirname($path), '/').'/';
$fileName = basename($imageSrcPath);
if (strpos($fileName, '..') !== false || strpos($fileName, '/') !== false || strpos($fileName, '\\') !== false)
if (
strpos($fileName, '..') !== false ||
strpos($fileName, '/') !== false ||
strpos($fileName, '\\') !== false
) {
throw new SystemException('Invalid image file name.');
}
$selectionParams = ['x', 'y', 'w', 'h'];
foreach ($selectionParams as $paramName) {
if (!array_key_exists($paramName, $selectionData))
if (!array_key_exists($paramName, $selectionData)) {
throw new SystemException('Invalid selection data.');
}
if (!ctype_digit($selectionData[$paramName]))
if (!ctype_digit($selectionData[$paramName])) {
throw new SystemException('Invalid selection data.');
}
}
$sessionDirectoryPath = $this->getCropSessionDirPath($cropSessionKey);
$fullSessionDirectoryPath = temp_path($sessionDirectoryPath);
if (!File::isDirectory($fullSessionDirectoryPath))
if (!File::isDirectory($fullSessionDirectoryPath)) {
throw new SystemException('The image editing session is not found.');
}
// Find the image on the disk and resize it
/*
* Find the image on the disk and resize it
*/
$imagePath = $fullSessionDirectoryPath.'/'.$fileName;
if (!File::isFile($imagePath))
if (!File::isFile($imagePath)) {
throw new SystemException('The image is not found on the disk.');
}
$extension = pathinfo($originalFileName, PATHINFO_EXTENSION);
$targetImageName = basename($originalFileName, '.'.$extension).'-'.$selectionData['x']
.'-'.$selectionData['y'].'-'.$selectionData['w'].'-'.$selectionData['h'].'-';
$targetImageName = basename($originalFileName, '.'.$extension).'-'
.$selectionData['x'].'-'
.$selectionData['y'].'-'
.$selectionData['w'].'-'
.$selectionData['h'].'-';
$targetImageName .= time();
$targetImageName .= '.'.$extension;
$targetTmpPath = $fullSessionDirectoryPath.'/'.$targetImageName;
/*
* Crop the image, otherwise copy original to target destination.
*/
if ($selectionData['w'] == 0 || $selectionData['h'] == 0) {
// If cropping is not required, copy the oiginal image to the target destination.
File::copy($imagePath, $targetTmpPath);
}
else {
$resizer = Resizer::open($imagePath);
$resizer->resample($selectionData['x'], $selectionData['y'], $selectionData['w'], $selectionData['h'], $selectionData['w'], $selectionData['h']);
$resizer->resample(
$selectionData['x'],
$selectionData['y'],
$selectionData['w'],
$selectionData['h'],
$selectionData['w'],
$selectionData['h']
);
$resizer->save($targetTmpPath, 95);
}
// Upload the cropped file to the Library
$targetPath = $path.'cropped-images/'.$targetImageName;
/*
* Upload the cropped file to the Library
*/
$targetFolder = $path.'cropped-images';
$targetPath = $targetFolder.'/'.$targetImageName;
$library = MediaLibrary::instance();
$library->put($targetPath, file_get_contents($targetTmpPath));
return $library->getPathUrl($targetPath);
return [
'publicUrl' => $library->getPathUrl($targetPath),
'documentType' => MediaLibraryItem::FILE_TYPE_IMAGE,
'itemType' => MediaLibraryItem::TYPE_FILE,
'path' => $targetPath,
'title' => $targetImageName,
'folder' => $targetFolder
];
}
}

View File

@ -321,8 +321,7 @@ if(selectedItems[0].getAttribute('data-document-type')!=='image'){alert(this.opt
return}
var path=selectedItems[0].getAttribute('data-path')
new $.oc.mediaManager.imageCropPopup(path,{alias:this.options.alias,onDone:callback})}
MediaManager.prototype.onImageCropped=function(imageUrl){var item={documentType:'image',publicUrl:imageUrl}
this.$el.trigger('popupcommand',['insert-cropped',item])}
MediaManager.prototype.onImageCropped=function(result){this.$el.trigger('popupcommand',['insert-cropped',result])}
MediaManager.prototype.clearSearchTrackInputTimer=function(){if(this.searchTrackInputTimer===null)
return
clearTimeout(this.searchTrackInputTimer)
@ -573,8 +572,7 @@ MediaManagerImageCropPopup.prototype.cropAndInsert=function(){var data={img:$(th
$.oc.stripeLoadIndicator.show()
this.$popupElement.find('form').request(this.options.alias+'::onCropImage',{data:data}).always(function(){$.oc.stripeLoadIndicator.hide()}).done(this.proxy(this.onImageCropped))}
MediaManagerImageCropPopup.prototype.onImageCropped=function(response){this.hide()
if(this.options.onDone!==undefined)
this.options.onDone(response.result)}
if(this.options.onDone!==undefined){this.options.onDone(response)}}
MediaManagerImageCropPopup.prototype.showResizePopup=function(){this.$popupElement.find('button[data-command=resize]').popup({content:this.$popupElement.find('[data-control="resize-template"]').html(),zIndex:1220})}
MediaManagerImageCropPopup.prototype.onResizePopupShown=function(ev,button,popup){var $popup=$(popup),$widthControl=$popup.find('input[name=width]'),$heightControl=$popup.find('input[name=height]'),imageWidth=this.fixDimensionValue(this.$popupElement.find('input[data-control=dimension-width]').val()),imageHeight=this.fixDimensionValue(this.$popupElement.find('input[data-control=dimension-height]').val())
$widthControl.val(imageWidth)
@ -635,7 +633,8 @@ this.initRulers()
this.initJCrop()}
MediaManagerImageCropPopup.prototype.onSelectionModeChanged=function(){var mode=this.getSelectionMode(),$widthInput=this.getWidthInput(),$heightInput=this.getHeightInput()
if(mode==='normal'){$widthInput.attr('disabled','disabled')
$heightInput.attr('disabled','disabled')}else{$widthInput.removeAttr('disabled')
$heightInput.attr('disabled','disabled')}
else{$widthInput.removeAttr('disabled')
$heightInput.removeAttr('disabled')
$widthInput.val(this.fixDimensionValue($widthInput.val()))
$heightInput.val(this.fixDimensionValue($heightInput.val()))}

View File

@ -236,20 +236,24 @@
}
$.oc.stripeLoadIndicator.show()
this.$popupElement.find('form').request(
this.options.alias+'::onCropImage', {
this.$popupElement
.find('form')
.request(this.options.alias+'::onCropImage', {
data: data
}
).always(function() {
$.oc.stripeLoadIndicator.hide()
}).done(this.proxy(this.onImageCropped))
})
.always(function() {
$.oc.stripeLoadIndicator.hide()
})
.done(this.proxy(this.onImageCropped))
}
MediaManagerImageCropPopup.prototype.onImageCropped = function(response) {
this.hide()
if (this.options.onDone !== undefined)
this.options.onDone(response.result)
if (this.options.onDone !== undefined) {
this.options.onDone(response)
}
}
MediaManagerImageCropPopup.prototype.showResizePopup = function() {
@ -409,7 +413,8 @@
if (mode === 'normal') {
$widthInput.attr('disabled', 'disabled')
$heightInput.attr('disabled', 'disabled')
} else {
}
else {
$widthInput.removeAttr('disabled')
$heightInput.removeAttr('disabled')
@ -449,13 +454,13 @@
var command = $(ev.currentTarget).data('command')
switch (command) {
case 'insert' :
case 'insert':
this.cropAndInsert()
break
case 'resize' :
case 'resize':
this.showResizePopup()
break
case 'undo-resizing' :
case 'undo-resizing':
this.undoResizing()
break
}

View File

@ -810,13 +810,8 @@
})
}
MediaManager.prototype.onImageCropped = function(imageUrl) {
var item = {
documentType: 'image',
publicUrl: imageUrl
}
this.$el.trigger('popupcommand', ['insert-cropped', item])
MediaManager.prototype.onImageCropped = function(result) {
this.$el.trigger('popupcommand', ['insert-cropped', result])
}
//