Jump to:

7939 Posts in 1472 Topics by 944 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » set max filesize for ImageGallery possible?

Discuss the DataObjectManager module, and the related ImageGallery module.

Moderators: martimiz, UncleCheese, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2 3
Go to End
Author Topic: 3404 Views
  • theAlien
    Avatar
    Community Member
    131 Posts

    Re: set max filesize for ImageGallery possible? Link to this post

    Hi UC,
    Thanks for thinking along. It is quite frustrating indeed.
    Today I decided to post a feature request to the guys at SWFUpload.
    I guess it won't even make the next release, but at least it's giving me the feeling I'm doing something usefull.
    At least my UserForm0.2-extension is getting somewhere.

  • theAlien
    Avatar
    Community Member
    131 Posts

    Re: set max filesize for ImageGallery possible? Link to this post

    Hi UC,

    I finally got some answers from the guys at SWFUpload. It's a won't fix. They say:

    "You should be able to handle this manually in the fileQueued event handler. You get
    an object with information about the file. Just check its size, cancel the file (you
    can suppress the uploadError even) and call fileQueueError if you want to be consistent." (issue 176 at http://code.google.com/p/swfupload/)

    I'm a bit confused about the 'fileQueued event handler'. Is it the file 'handlers.js'?
    I guess you know a little bit more about the combination SS-SWFUpload than me.
    Do you have any clues about what should be done and where in the SS-version?

  • UncleCheese
    Avatar
    4085 Posts

    Re: set max filesize for ImageGallery possible? Link to this post

    Ahh, yeah, that makes sense. Give me some time to get my head back into this issue and I'll see if it's something I can work into the module. Having PHP talk to javascript functions is never pretty.

  • theAlien
    Avatar
    Community Member
    131 Posts

    Re: set max filesize for ImageGallery possible? Link to this post

    Hey man, thanks alot!
    I'll try to wait patiently ;-)

  • UncleCheese
    Avatar
    4085 Posts

    Re: set max filesize for ImageGallery possible? Link to this post

    So it's a little too much coding for me to do for a single user. Instead, I'll give you a basic idea of how to do it.

    First, extend your ImageGalleryPage class:

    class MyImageGalleryPage extends ImageGalleryPage
    {
    public function getCMSFields()
    {
    Requirements::javascript('mysite/javascript/my_handlers.js');
    SWFUploadConfig::set_var('file_queued_handler','myFileQueued');
    return parent::getCMSFields();
    }
    }

    my_handlers.js:

    function myFileQueued(file)
    {
       try {
          li = document.createElement("li");
          li.setAttribute('id', 'file-' + file.id);
          filename = new String(file.name);
          if(filename.length > 30)
             filename = filename.substr(0,29) + '...';
             
          li.innerHTML = "<div class='queue-file-name'>" + filename + "</div><div class='queue-remove-btn'><a href='javascript:void(0);' onclick='return removeFileFromQueue(\""+file.id+"\");'>remove</a></div>";
          txtFileNames.appendChild(li);
          btnSubmit.style.display = 'block';
          var progress = new FileProgress(file, 'file-' + file.id);
          progress.setProgress(0);
          meg = file.size > 1024*1024;
          size = meg ? file.size/1024/1024 : file.size/1024;
          rounded = Math.round(size*10)/10;
          formatted = meg ? rounded : addCommas(Math.ceil(rounded));
          suffix = meg ? 'M' : 'k';
          progress.setStatus('Queued ('+formatted+suffix+')');
          progress.fileProgressElement.childNodes[2].className = 'progressBarStatus queued';

    // new stuff

    // i'm not sure about the "type" property, but i'm sure there's something on the object with information about the file type. you'll have to sniff it out with alerts and so forth if this doesn't work. Try the swfupload forum, too.

    var size_limits = {
    'jpeg' : 2000000,
    'pdf' : 3000000,
    'mp3' : 4000000
    };

    for(type in size_limits) {
    if(file.type == type && file.size > size_limits[type]) {
    removeFileFromQueue(file.id);
    alert('The file you selected is too big. The largest allowed size for type ' + type + ' is ' + size_limits[type]);
    break;
    }
    }

       } catch (e) {
       }

    }

    That's the basic idea. Probably riddled with syntax errors.

  • theAlien
    Avatar
    Community Member
    131 Posts

    Re: set max filesize for ImageGallery possible? Link to this post

    You rock!

    It's partially working right now! (See below)
    You were right: file.type didn't work, so I had to bypass it.

    I pasted/wrote the code in swfupload/javascript/handlers.js, because I wasn't able to get the code working in mysite/javascript/myImageGallery_handlers.js working.
    Probably it has something to do with the way it is called?:

    class MyImageGalleryPage extends ImageGalleryPage {
    public function getCMSFields() {
    Requirements::javascript('mysite/javascript/myImageGallery_handlers.js');
    SWFUploadConfig::set_var('file_queued_handler','myFileQueued');
    return parent::getCMSFields(); }}

    On second thought however I suppose it is better to have SWFUpload or DOM extended. In my case there are problems with the GD-manipulation of images, that's why I need to restrict image-sizes. And as far as I know DOM is also using GD-manipulation - so it's likely there will be similar problems whereever I use DOM.

    What I'm wondering right now is: is it possible to make some extension to DOM (or SWFUpload) and have it working also in - let's say - the DOM in 'Files and Images', or does this need huge extensions to core? In that case it might be better to leave (for now) the code in swfupload/javascript/handlers.js .

    Well... that said: here is the working code (for the copy-pasters among you: paste it instead of the "// new stuff" part in UncleCheeses example):

    // new stuff
    var pos = filename.lastIndexOf(".");
    var strlen = filename.length;
    if(pos != -1 && strlen != pos+1) {
       var ext = filename.split(".");
       var len = ext.length;
       var extension = ext[len-1].toLowerCase();
    }
    var size_limits = {
    'jpg' : 2000000,
    'pdf' : 3000000,
    'mp3' : 4000000
    };
    for(type in size_limits) {
    if(extension == type && file.size > size_limits[type]) {
    removeFileFromQueue(file.id);
    alert('The file you selected is too big. The maximum size allowed for files of the type ' + type.toUpperCase() + ' is ' + (Math.round(size_limits[type]*10)/10000) + 'kB.');
    break;
    }
    }

    By the way: it is quite powerful. If the size limit is bigger than the size limit of the server, the cms defaults to the size limit of the server (or what is set in _config.php). If there is no size limit set for a file type, the cms also defaults to the size limit of the server (or what is set in _config.php).

  • UncleCheese
    Avatar
    4085 Posts

    Re: set max filesize for ImageGallery possible? Link to this post

    Glad it's working for you. I realized putting the Requirements call in the getCMSFields() function was useless after I posted that. Here's the super-slick, cleanest way to do this.

    mysite/_config.php

    Object::add_extension("LeftAndMain","ImageGalleryScriptInit");

    mysite/code/ImageGalleryScriptInit.php

    class ImageGalleryScriptInit extends Extension
    {
       public function augmentInit(){
          Requirements::javascript('mysite/javascript/image_gallery_handlers.js');
       }
    }

    Shazam!

  • UncleCheese
    Avatar
    4085 Posts

    Re: set max filesize for ImageGallery possible? Link to this post

    The above is better accomplished using:

    LeftAndMain::require_javascript('mysite/javascript/image_gallery_handlers.js');

    3404 Views
Page: 1 2 3
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.