Skip to content
This repository was archived by the owner on Oct 6, 2020. It is now read-only.

Commit 2878884

Browse files
committed
Update to have consistent error messages and pass in the file types and max size.
1 parent fbfd807 commit 2878884

File tree

7 files changed

+109
-124
lines changed

7 files changed

+109
-124
lines changed

README

Lines changed: 20 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,14 @@ S3SwfUpload
22
===========
33

44
S3SwfUpload allow user uploading a file to S3 directly, so you can save the cost of uploading process in your app server.
5+
This version of the plugin features the ability to upload multiple files.
6+
It currently has some hard coded constrains on file type and size that are PRX specific, but those will be removed presently.
57

68
Install
79
=======
810

911
./script/plugin install git://github.com/PRX/s3-swf-upload-plugin.git
1012

11-
Example
12-
=======
13-
14-
There is an example app showing how to use this at
15-
16-
http://github.com/GreenAsJade/demo-s3-swf-upload/tree/master
17-
18-
It's live at
19-
20-
http://demo-s3-swf-upload.heroku.com/
21-
2213
Usage
2314
=======
2415

@@ -66,41 +57,27 @@ Usage
6657

6758
IE the default prefix is '' (no prefix)
6859

69-
7. If you want to customise its behavior, here's a more complex example:
70-
71-
<%= s3_swf_upload_tag(:width => 310,
72-
:height => 40,
73-
:success => 'alert("success");',
74-
:failed => 'alert("failed");',
75-
:selected => 'alert("selected");',
76-
:prefix => 'prefix_in_bucket',
77-
:initialMessage => 'Press this button to find the file to upload"
78-
:upload => 'Press Here To Activate Upload',
79-
:do_checks => '0')
80-
%>
81-
82-
In this case, files will be uploaded to http://bucket.s3.amazonasw.com/prefix_in_bucket/filename
83-
84-
8. If you need to check that the file they have selected is
85-
legitimate, before it gets uploaded, (for example, to make sure
86-
they aren't overwriting a file of the same name your database
87-
knows is already uploaded):
88-
89-
i) Supply a method s3_swf_upload_error? in your application.rb to
90-
check, like this:
60+
7. If you want to customize its behavior, here's a more complex example:
9161

92-
def s3_swf_upload_file_error?(filename)
93-
Mod.find_by_zip_name(File::basename(filename)) ?
94-
"Sorry, there is already a mod with that zip file name!" :
95-
nil
96-
end
62+
<%= s3_swf_upload_tag(
63+
:width => 550,
64+
:height => 175,
65+
:success => "jQuery.post('/file_upload', {'uuid':'#{@uuid}',
66+
'filename':filename,
67+
'filesize':filesize,
68+
'contenttype':contenttype},
69+
null, 'script');",
70+
:failed => 'alert("Failed: " + status);',
71+
:prefix => "prefix_in_bucket/#{@uuid}",
72+
:initial_message=> 'Click "Browse..." to find files to upload.')
73+
%>
9774

98-
Note that this method must return nil if there no error.
9975

100-
ii) Set the option
76+
In this case, files will be uploaded to:
77+
http://bucket.s3.amazonasw.com/prefix_in_bucket/uuid_value
78+
and when successful, will post the information about the file uploaded, including this UUID, to the file_upload controller.
10179

102-
:do_checks => '1'
10380

104-
in the s3_swf_upload_tag
81+
Original plugin is Copyright (c) 2008 elctech, released under the MIT license
10582

106-
Copyright (c) 2008 elctech, released under the MIT license
83+
Updates to plugin Copyright (c) 2010 PRX, released under the MIT license

flex_src/src/S3Uploader.as

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,20 @@ private function registerCallbacks():void {
1818
}
1919
}
2020

21-
private function init(signatureUrl:String, initialMessage:String="mxml", prefixPath:String="s3_swf_upload"):void {
21+
private function init(signatureUrl:String,
22+
initialMessage:String,
23+
prefixPath:String,
24+
maxFileSize:String,
25+
fileTypes:String,
26+
fileTypeDescs:String
27+
):void {
2228

23-
_multipleFileUploader = new MultipleFileS3Uploader(signatureUrl,
29+
_multipleFileUploader = new MultipleFileS3Uploader(signatureUrl,
30+
initialMessage,
2431
prefixPath,
25-
userMessage,
32+
maxFileSize,
33+
fileTypes,
34+
fileTypeDescs,
2635
browseButton,
2736
uploadButton,
2837
removeSelectedButton,

flex_src/src/S3Uploader.mxml

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
33
backgroundAlpha="0"
4-
backgroundColor="#C0C0C0"
4+
backgroundColor="#FFFFFF"
55
creationComplete="registerCallbacks();"
66
layout="absolute">
77

@@ -41,16 +41,6 @@
4141
</mx:HBox>
4242
</mx:ControlBar>
4343

44-
<mx:Label id="userMessage"
45-
left="0"
46-
right="0"
47-
bottom="0"
48-
top="0"
49-
color="#000000"
50-
fontSize="14"
51-
text="Click 'Browse...' to select an audio file for upload."
52-
paddingBottom="0"/>
53-
5444
</mx:VBox>
5545

5646
</mx:Application>

flex_src/src/org/prx/uploader/MultipleFileS3Uploader.as

Lines changed: 42 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
/*
2-
ToDos:
3-
get hard coded values out of the colors and styles for error messages - a least make it a function
4-
5-
*/
6-
71
package org.prx.uploader {
82

93
import com.adobe.net.MimeTypeMap;
@@ -38,7 +32,6 @@ package org.prx.uploader {
3832
//UI Vars
3933
private var _signatureUrl:String;
4034
private var _prefixPath:String;
41-
private var _userMessage:Label;
4235
private var _browseButton:Button;
4336
private var _removeSelectedButton:Button;
4437
private var _removeAllButton:Button;
@@ -66,16 +59,20 @@ package org.prx.uploader {
6659
private var _maxFileSize:Number; //bytes
6760

6861
private var _mimeMap:MimeTypeMap;
69-
private var _mp2Filter:FileFilter;
62+
private var _fileFilter:FileFilter;
7063

7164
public var s3onSuccessCall:String;
7265
public var s3onFailedCall:String;
7366
public var s3onSelectedCall:String;
67+
public var s3onInfoCall:String;
7468
public var s3onCancelCall:String;
7569

7670
public function MultipleFileS3Uploader(signatureUrl:String,
71+
initialMessage:String,
7772
prefixPath:String,
78-
userMessage:Label,
73+
maxFileSize:String,
74+
fileTypes:String,
75+
fileTypeDescs:String,
7976
browseButton:Button,
8077
uploadButton:Button,
8178
removeSelectedButton:Button,
@@ -85,7 +82,6 @@ package org.prx.uploader {
8582
// set up from args
8683
_prefixPath = prefixPath;
8784
_signatureUrl = signatureUrl;
88-
_userMessage = userMessage;
8985
_browseButton = browseButton;
9086
_uploadButton = uploadButton;
9187
_removeSelectedButton = removeSelectedButton;
@@ -96,8 +92,8 @@ package org.prx.uploader {
9692
// other defaults
9793
_maxFileCount = 10;
9894
_mimeMap = new MimeTypeMap();
99-
_mp2Filter = new FileFilter("MP2 Audio Files", "*.mp2");
100-
_maxFileSize = 524288000;
95+
_fileFilter = new FileFilter(fileTypeDescs, fileTypes);
96+
_maxFileSize = parseInt(maxFileSize);
10197
_dateTimeFormatter = new DateFormatter();
10298
_dateTimeFormatter.formatString = "MM/DD/YYYY L:NN A";
10399
_totalbytes = 0;
@@ -106,8 +102,11 @@ package org.prx.uploader {
106102
s3onSuccessCall = "s3_swf.onSuccess";
107103
s3onFailedCall = "s3_swf.onFailed";
108104
s3onSelectedCall = "s3_swf.onSelected";
105+
s3onInfoCall = "s3_swf.onInfo";
109106
s3onCancelCall = "s3_swf.onCancel";
110107

108+
ExternalInterface.call(s3onInfoCall, initialMessage);
109+
111110
init();
112111
}
113112

@@ -163,7 +162,7 @@ package org.prx.uploader {
163162

164163
//Browse for files
165164
private function browseFiles(event:Event):void{
166-
_fileref.browse([_mp2Filter]);
165+
_fileref.browse([_fileFilter]);
167166
}
168167

169168
// called after user selected files form the browse dialouge box.
@@ -181,22 +180,21 @@ package org.prx.uploader {
181180
getContentType(event.currentTarget.fileList[i].name));
182181
} else {
183182
dl.push(event.currentTarget.fileList[i]);
184-
trace(event.currentTarget.fileList[i].name + " too large");
185-
ExternalInterface.call(s3onFailedCall,
186-
event.currentTarget.fileList[i].name,
187-
event.currentTarget.fileList[i].size,
188-
getContentType(event.currentTarget.fileList[i].name));
183+
ExternalInterface.call(s3onFailedCall,
184+
event.currentTarget.fileList[i].name + " too large, max is " + Math.round(_maxFileSize / 1024) + " kb");
189185
}
190186
}
191-
if (dl.length > 0) {
192-
for (i=0;i<dl.length;i++) {
193-
msg += String(dl[i].name + " is too large. \n");
194-
}
195-
mx.controls.Alert.show(msg + "Max File Size is: " + Math.round(_maxFileSize / 1024) + " kb","File Too Large",4,null).clipContent;
196-
}
187+
188+
/* Don't do this here, just make the external interface call above. */
189+
/*if (dl.length > 0) {
190+
for (i=0;i<dl.length;i++) {
191+
msg += String(dl[i].name + " is too large. \n");
192+
}
193+
mx.controls.Alert.show(msg + "Max File Size is: " + Math.round(_maxFileSize / 1024) + " kb","File Too Large",4,null).clipContent;
194+
}*/
197195

198196
if(_files.length > 0) {
199-
_userMessage.text = "Click 'Upload' to start loading files, or 'Browse...' to select more.";
197+
ExternalInterface.call(s3onInfoCall, "Click 'Upload' to start loading files, or 'Browse...' to select more.");
200198
}
201199
}
202200

@@ -243,10 +241,7 @@ package org.prx.uploader {
243241
if (xml.errorMessage != "") {
244242
resetProgressBar();
245243
_uploadButton.enabled = true;
246-
_userMessage.visible = true;
247-
_userMessage.text = "Error! Please try again, or contact us for help.";
248-
_userMessage.setStyle("color","#770000");
249-
ExternalInterface.call(s3onFailedCall);
244+
ExternalInterface.call(s3onFailedCall, "Error! Please try again, or contact us for help.");
250245
return;
251246
}
252247

@@ -258,18 +253,17 @@ package org.prx.uploader {
258253
request.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, s3CompleteHandler);
259254

260255
try {
261-
_userMessage.text = "Upload started...";
256+
ExternalInterface.call(s3onInfoCall, "Upload started...");
262257
request.upload(_file);
263258
} catch(e:Error) {
264-
_userMessage.text = "Upload error!";
265-
_userMessage.setStyle("color","#770000");
259+
ExternalInterface.call(s3onFailedCall, "Upload error!");
266260
trace("An error occurred: " + e);
267261
}
268262
}
269263

270264
// called after the file is opened before upload
271265
private function s3OpenHandler(event:Event):void{
272-
_userMessage.text = "";
266+
ExternalInterface.call(s3onInfoCall, "");
273267
trace(event);
274268
trace('openHandler triggered');
275269
_files;
@@ -284,21 +278,19 @@ package org.prx.uploader {
284278
// only called if there is an error detected by flash player browsing or uploading a file
285279
private function s3IoErrorHandler(event:IOErrorEvent):void{
286280
//trace('And IO Error has occured:' + event);
287-
_userMessage.text = "Error! Please retry, or contact us for help.";
288281
trace(s3onFailedCall);
289-
ExternalInterface.call(s3onFailedCall);
290-
trace(event);
291-
mx.controls.Alert.show(String(event),"ioError",0);
282+
ExternalInterface.call(s3onFailedCall, "Error! Please retry, or contact us for help: " + String(event));
283+
/*trace(event);*/
284+
/*mx.controls.Alert.show(String(event),"ioError",0);*/
292285
}
293286

294287
// only called if a security error detected by flash player such as a sandbox violation
295288
private function s3SecurityErrorHandler(event:SecurityErrorEvent):void{
296289
//trace("securityErrorHandler: " + event);
297-
_userMessage.text = "Error, access denied.";
298290
trace(s3onFailedCall);
299-
ExternalInterface.call(s3onFailedCall);
300-
trace(event);
301-
mx.controls.Alert.show(String(event),"Security Error",0);
291+
ExternalInterface.call(s3onFailedCall, "Error, access denied: " + String(event));
292+
/*trace(event);*/
293+
/*mx.controls.Alert.show(String(event),"Security Error",0);*/
302294
}
303295
// This function its not required
304296
private function s3CancelHandler(event:Event):void{
@@ -311,15 +303,15 @@ package org.prx.uploader {
311303
trace(s3onSuccessCall);
312304
ExternalInterface.call(s3onSuccessCall, _options.FileName, _options.FileSize, _options.ContentType);
313305
trace(event);
314-
_userMessage.text = "Complete!";
315306
_files.removeItemAt(0);
316307
if (_files.length > 0){
317308
_totalbytes = 0;
318309
uploadFiles(null);
319310
} else {
320311
setupCancelButton(false);
321312
_uploadProgressBar.label = "Uploads complete";
322-
_userMessage.text = "Uploads complete";
313+
ExternalInterface.call(s3onInfoCall, "All uploads complete");
314+
/* not sure these next 2 lines are necessary... */
323315
var uploadCompleted:Event = new Event(Event.COMPLETE);
324316
dispatchEvent(uploadCompleted);
325317
}
@@ -338,30 +330,30 @@ package org.prx.uploader {
338330
}
339331

340332
private function sigOpenHandler(event:Event):void {
341-
_userMessage.text = "Preparing...";
333+
ExternalInterface.call(s3onInfoCall, "Preparing...");
342334
trace("openHandler: " + event);
343335
}
344336
private function sigProgressHandler(event:ProgressEvent):void {
345337
trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
346338
}
347339
private function sigSecurityErrorHandler(event:SecurityErrorEvent):void {
348340
trace("securityErrorHandler: " + event);
349-
_userMessage.text = "Security error!";
350-
ExternalInterface.call(s3onFailedCall);
341+
ExternalInterface.call(s3onFailedCall, "Security error!");
342+
/*mx.controls.Alert.show(String(event),"securityError",0);*/
351343
}
352344
private function sigHttpStatusHandler(event:HTTPStatusEvent):void {
353345
trace("httpStatusHandler: " + event);
354346
}
355347
private function sigIoErrorHandler(event:IOErrorEvent):void {
356348
trace("ioErrorHandler: " + event);
357349
trace(s3onFailedCall);
358-
_userMessage.text = "Network error!";
359-
ExternalInterface.call(s3onFailedCall);
350+
ExternalInterface.call(s3onFailedCall, "Network error!");
351+
/*mx.controls.Alert.show(String(event),"networkError",0);*/
360352
}
361353

362354
private function uploadFiles(event:Event):void{
363355
if (_files.length > 0){
364-
_userMessage.text = 'Initiating...';
356+
ExternalInterface.call(s3onInfoCall, 'Initiating...');
365357
_file = FileReference(_files.getItemAt(0));
366358
getSignature();
367359
setupCancelButton(true);
@@ -371,7 +363,7 @@ package org.prx.uploader {
371363
//Remove Selected File From Queue
372364
private function removeSelectedFileFromQueue(event:Event):void{
373365
if (_filesDataGrid.selectedIndex >= 0){
374-
_files.removeItemAt( _filesDataGrid.selectedIndex);
366+
_files.removeItemAt( _filesDataGrid.selectedIndex);
375367
}
376368
}
377369

0 commit comments

Comments
 (0)