Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

set max filesize for ImageGallery possible?


Go to End


19 Posts   5770 Views

Avatar
theAlien

Community Member, 131 Posts

1 July 2009 at 11:47am

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.

Avatar
theAlien

Community Member, 131 Posts

8 July 2009 at 1:06pm

Edited: 08/07/2009 1:15pm

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?

Avatar
UncleCheese

Forum Moderator, 4102 Posts

8 July 2009 at 1:41pm

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. :)

Avatar
theAlien

Community Member, 131 Posts

8 July 2009 at 10:45pm

Edited: 08/07/2009 10:53pm

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

Avatar
UncleCheese

Forum Moderator, 4102 Posts

9 July 2009 at 5:18am

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. :)

Avatar
theAlien

Community Member, 131 Posts

10 July 2009 at 11:25am

Edited: 10/07/2009 11:57am

You rock!

It's partially working right now! (See below) :D
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).

Avatar
UncleCheese

Forum Moderator, 4102 Posts

11 July 2009 at 3:06am

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!

Avatar
UncleCheese

Forum Moderator, 4102 Posts

11 May 2010 at 7:59am

The above is better accomplished using:

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