diff --git a/.gitignore b/.gitignore index a01bdcf71..26d89655c 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ Homestead.yaml .idea /public/plugins /public/css -/public/js/all* +/public/js +/public/uploads /public/bower /storage/images \ No newline at end of file diff --git a/app/Http/Controllers/ImageController.php b/app/Http/Controllers/ImageController.php index c8a13e48b..2ef5b1228 100644 --- a/app/Http/Controllers/ImageController.php +++ b/app/Http/Controllers/ImageController.php @@ -71,7 +71,7 @@ class ImageController extends Controller */ public function getAll($page = 0) { - $pageSize = 13; + $pageSize = 25; $images = DB::table('images')->orderBy('created_at', 'desc') ->skip($page*$pageSize)->take($pageSize)->get(); foreach($images as $image) { @@ -95,9 +95,9 @@ class ImageController extends Controller public function getThumbnail($image, $width = 220, $height = 220) { $explodedPath = explode('/', $image->url); - array_splice($explodedPath, 3, 0, ['thumbs-' . $width . '-' . $height]); + array_splice($explodedPath, 4, 0, ['thumbs-' . $width . '-' . $height]); $thumbPath = implode('/', $explodedPath); - $thumbFilePath = storage_path() . $thumbPath; + $thumbFilePath = public_path() . $thumbPath; // Return the thumbnail url path if already exists if(file_exists($thumbFilePath)) { @@ -105,7 +105,7 @@ class ImageController extends Controller } // Otherwise create the thumbnail - $thumb = ImageTool::make(storage_path() . $image->url); + $thumb = ImageTool::make(public_path() . $image->url); $thumb->fit($width, $height); // Create thumbnail folder if it does not exist @@ -127,17 +127,18 @@ class ImageController extends Controller { $imageUpload = $request->file('file'); $name = str_replace(' ', '-', $imageUpload->getClientOriginalName()); - $imagePath = '/images/' . Date('Y-m-M') . '/'; - $storagePath = storage_path(). $imagePath; - $fullPath = $storagePath . $name; + $storageName = substr(sha1(time()), 0, 10) . '-' . $name; + $imagePath = '/uploads/images/'.Date('Y-m-M').'/'; + $storagePath = public_path(). $imagePath; + $fullPath = $storagePath . $storageName; while(file_exists($fullPath)) { - $name = substr(sha1(rand()), 0, 3) . $name; - $fullPath = $storagePath . $name; + $storageName = substr(sha1(rand()), 0, 3) . $storageName; + $fullPath = $storagePath . $storageName; } - $imageUpload->move($storagePath, $name); + $imageUpload->move($storagePath, $storageName); // Create and save image object $this->image->name = $name; - $this->image->url = $imagePath . $name; + $this->image->url = $imagePath . $storageName; $this->image->created_by = Auth::user()->id; $this->image->updated_by = Auth::user()->id; $this->image->save(); diff --git a/gulpfile.js b/gulpfile.js index 654f78d51..6358835a7 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -13,5 +13,5 @@ var elixir = require('laravel-elixir'); elixir(function(mix) { mix.sass('styles.scss'); - mix.babel('image-manager.js'); + mix.babel('image-manager.js', 'public/js/image-manager.js'); }); diff --git a/public/js/image-manager.js b/public/js/image-manager.js deleted file mode 100644 index 45c26d8e1..000000000 --- a/public/js/image-manager.js +++ /dev/null @@ -1,117 +0,0 @@ - -// Dropzone config -Dropzone.options.imageUploadDropzone = { - uploadMultiple: false, - previewsContainer: '.image-manager-display .uploads', - init: function() { - this.on('success', function(event, image) { - $('.image-manager-display .uploads').empty(); - var newImage = $('').attr('data-image-id', image.id); - newImage.attr('title', image.name).attr('src', image.thumbnail); - newImage.data('imageData', image); - $('.image-manager-display .uploads').after(newImage); - }); - } -}; - -(function() { - - var isInit = false; - var elem; - var overlay; - var display; - var imageIndexUrl = '/images/all'; - var pageIndex = 0; - var hasMore = true; - var isGettingImages = true; - - var ImageManager = {}; - var action = false; - - ImageManager.show = function(selector, callback) { - if(isInit) { - showWindow(); - } else { - this.init(selector) - showWindow(); - } - - action = (typeof callback !== 'undefined') ? callback : false; - }; - - ImageManager.init = function(selector) { - elem = $(selector); - overlay = elem.closest('.overlay'); - display = elem.find('.image-manager-display').first(); - var uploads = display.find('.uploads'); - var images = display.find('images'); - var loadMore = display.find('.load-more'); - // Get recent images and show - $.getJSON(imageIndexUrl, showImages); - function showImages(data) { - var images = data.images; - hasMore = data.hasMore; - pageIndex++; - isGettingImages = false; - for(var i = 0; i < images.length; i++) { - var image = images[i]; - var newImage = $('').attr('data-image-id', image.id); - newImage.attr('title', image.name).attr('src', image.thumbnail); - loadMore.before(newImage); - newImage.data('imageData', image); - } - if(hasMore) loadMore.show(); - } - - loadMore.click(function() { - loadMore.hide(); - if(isGettingImages === false) { - isGettingImages = true; - $.getJSON(imageIndexUrl + '/' + pageIndex, showImages); - } - }); - - // Image grabbing on scroll - display.on('scroll', function() { - var displayBottom = display.scrollTop() + display.height(); - var elemTop = loadMore.offset().top; - if(elemTop < displayBottom && hasMore && isGettingImages === false) { - isGettingImages = true; - loadMore.hide(); - $.getJSON(imageIndexUrl + '/' + pageIndex, showImages); - } - }); - - elem.on('dblclick', '.image-manager-display img', function() { - var imageElem = $(this); - var imageData = imageElem.data('imageData'); - closeWindow(); - if(action) { - action(imageData); - } - }); - - elem.find('button[data-action="close"]').click(function() { - closeWindow(); - }); - - // Set up dropzone - elem.find('.image-manager-dropzone').first().dropzone({ - uploadMultiple: false - }); - - isInit = true; - }; - - function showWindow() { - overlay.closest('body').css('overflow', 'hidden'); - overlay.show(); - } - - function closeWindow() { - overlay.hide(); - overlay.closest('body').css('overflow', 'auto'); - } - - window.ImageManager = ImageManager; -})(); \ No newline at end of file diff --git a/resources/assets/js/image-manager.js b/resources/assets/js/image-manager.js index 0bece9894..2aaf02909 100644 --- a/resources/assets/js/image-manager.js +++ b/resources/assets/js/image-manager.js @@ -1,63 +1,140 @@ -class ImageList extends React.Component { +(function() { - constructor(props) { - super(props); - this.state = { - images: [], - hasMore: false, - page: 0 - }; - } - componentDidMount() { - $.getJSON('/images/all', data => { - this.setState({ - images: data.images, - hasMore: data.hasMore + class ImageManager extends React.Component { + + constructor(props) { + super(props); + this.state = { + images: [], + hasMore: false, + page: 0 + }; + } + + show(callback) { + $(React.findDOMNode(this)).show(); + this.callback = callback; + } + + hide() { + $(React.findDOMNode(this)).hide(); + } + + selectImage(image) { + if(this.callback) { + this.callback(image); + } + this.hide(); + } + + componentDidMount() { + var _this = this; + // Set initial images + $.getJSON('/images/all', data => { + this.setState({ + images: data.images, + hasMore: data.hasMore + }); }); - }); - } - - loadMore() { - this.state.page++; - $.getJSON('/images/all/' + this.state.page, data => { - this.setState({ - images: this.state.images.concat(data.images), - hasMore: data.hasMore + // Create dropzone + this.dropZone = new Dropzone(React.findDOMNode(this.refs.dropZone), { + url: '/upload/image', + init: function() { + var dz = this; + this.on("sending", function(file, xhr, data) { + data.append("_token", document.querySelector('meta[name=token]').getAttribute('content')); + }); + this.on("success", function(file, data) { + _this.state.images.unshift(data); + _this.setState({ + images: _this.state.images + }); + //$(file.previewElement).fadeOut(400, function() { + // dz.removeFile(file); + //}) + }); + } }); - }); - } + } - render() { - var images = this.state.images.map(function(image) { + loadMore() { + this.state.page++; + $.getJSON('/images/all/' + this.state.page, data => { + this.setState({ + images: this.state.images.concat(data.images), + hasMore: data.hasMore + }); + }); + } + + render() { + var loadMore = this.loadMore.bind(this); + var selectImage = this.selectImage.bind(this); return ( -