Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,30 @@
"type": "silverstripe-vendormodule",
"keywords": ["silverstripe", "upload", "uploader", "files", "forms", "cms"],
"license": "BSD-3-Clause",
"authors": [{
"name": "Uncle Cheese",
"email": "unclecheese@leftandmain.com"
}],
"require": {
"authors": [
{
"name": "Uncle Cheese",
"email": "unclecheese@leftandmain.com"
}
],
"require":
{
"silverstripe/framework": "4.*"
},
"autoload": {
"psr-4": {
"UncleCheese\\DropZone\\": "code/"
}
},
"minimum-stability": "dev",
"prefer-stable": true,
"extra": {
"installer-name": "dropzone",
"expose": [
"javascript",
"css",
"images"
]
},
"autoload": {
"psr-4": {
"UncleCheese\\Dropzone\\": "src/"
}
}
}
"javascript",
"images",
"templates"
]
}
}
49 changes: 23 additions & 26 deletions src/DropzoneFile.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
<?php

namespace UncleCheese\Dropzone;
namespace UncleCheese\DropZone;

use SilverStripe\Assets\Folder;
use SilverStripe\Assets\Image;
use SilverStripe\Control\Director;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Manifest\ModuleResourceLoader;
use SilverStripe\Assets\Folder;
use SilverStripe\ORM\DataExtension;

/**
* Adds helper methods to the core {@link File} object
*
* @package unclecheese/dropzone
* @package unclecheese/dropzone
* @author Uncle Cheese <unclecheese@leftandmain.com>
*/
class DropzoneFile extends DataExtension
Expand All @@ -22,7 +21,7 @@ class DropzoneFile extends DataExtension
/**
* Helper method for determining if this is an Image
*
* @return boolean
* @return boolean
*/
public function IsImage()
{
Expand All @@ -34,41 +33,38 @@ public function IsImage()
* Gets a thumbnail for this file given a size. If it's an Image,
* it will render the actual file. If not, it will provide an icon based
* on the extension.
*
* @param int $w The width of the image
* @param int $h The height of the image
* @return Image_Cached
*/
public function getPreviewThumbnail($w = null, $h = null)
{
if(!$w) { $w = $this->owner->config()->grid_thumbnail_width;
if (!$w) {
$w = $this->owner->config()->grid_thumbnail_width;
}
if(!$h) { $h = $this->owner->config()->grid_thumbnail_height;
if (!$h) {
$h = $this->owner->config()->grid_thumbnail_height;
}

if($this->IsImage() && Director::fileExists($this->owner->Filename)) {
if ($this->IsImage() && Director::fileExists($this->owner->Filename)) {
return $this->owner->CroppedImage($w, $h);
}

$sizes = Config::inst()->forClass(FileAttachmentField::class)->icon_sizes;
$sizes = Config::inst()->forClass('FileAttachmentField')->icon_sizes;
sort($sizes);

foreach($sizes as $size) {
if($w <= $size) {
if($this->owner instanceof Folder) {
foreach ($sizes as $size) {
if ($w <= $size) {
if ($this->owner instanceof Folder) {
$file = $this->getFilenameForType('_folder', $size);
}
else {
} else {
$file = $this->getFilenameForType($this->owner->getExtension(), $size);
}
if(!file_exists(BASE_PATH.'/'.$file)) {
if (!file_exists(BASE_PATH . '/' . $file)) {
$file = $this->getFilenameForType('_blank', $size);
}

$image = Image::create();
$image->setFromLocalFile(Director::getAbsFile($file), basename($file));

return $image;
return new File(Director::makeRelative($file));
}
}
}
Expand All @@ -77,16 +73,17 @@ public function getPreviewThumbnail($w = null, $h = null)
/**
* Gets a filename based on the extension and the size
*
* @param string $ext The extension of the file, e.g. "pdf"
* @param int $size The size of the image
* @param string $ext The extension of the file, e.g. "pdf"
* @param int $size The size of the image
* @return string
*/
protected function getFilenameForType($ext, $size)
{
return ModuleResourceLoader::singleton()->resolveURL(sprintf(
'unclecheese/dropzone:images/file-icons/%spx/%s.png',
return sprintf(
'%s/images/file-icons/%spx/%s.png',
basename(dirname(__FILE__)),
$size,
strtolower($ext)
));
);
}
}
}
Loading