View Issue Details

IDProjectCategoryView StatusLast Update
0008780mantisbtjavascriptpublic2008-01-20 19:41
ReporterBoobaa Assigned To 
PrioritynormalSeveritytweakReproducibilityalways
Status newResolutionopen 
Product Version1.1.1 
Summary0008780: Save bandwidth with JS check when uploading files
Description

When hitting "Upload File" button on an issue's page, a JavaScript routine could check if a file already exists with that name without wasting the bandwidth with the upload itself.

Additional Information

Please find the attached patch (against 1.1.1) which implements this.

TagsNo tags attached.
Attached Files
check_filename_before_upload.diff (3,390 bytes)   
diff -Naur mantis-1.1.1/bug_file_upload_inc.php mantis/bug_file_upload_inc.php
--- mantis-1.1.1/bug_file_upload_inc.php	2007-10-14 00:36:41.000000000 +0200
+++ mantis/bug_file_upload_inc.php	2008-01-20 23:46:25.000000000 +0100
@@ -57,8 +57,37 @@
 	<td width="85%">
 		<input type="hidden" name="bug_id" value="<?php echo $f_bug_id ?>" />
 		<input type="hidden" name="max_file_size" value="<?php echo $t_max_file_size ?>" />
-		<input name="file" type="file" size="40" />
-		<input type="submit" class="button" value="<?php echo lang_get( 'upload_file_button' ) ?>" />
+		<input name="file" type="file" size="40" id="file" />
+		<script type="text/javascript">
+			function check_filename() {
+				filename = document.getElementById('file');
+				filename = filename.value;
+				if(filename == '') {
+					return false;
+				}
+				lastslash = filename.lastIndexOf('/');
+				if(lastslash == -1) {
+					lastslash = filename.lastIndexOf('\\');
+				}
+				if(lastslash == -1) {
+					lastslash = 0;
+				}
+				else {
+					filename = filename.substring(lastslash + 1);
+				}
+				upload_allowed = true;
+				for(i in fileArray) {
+					if(fileArray[i] == filename) {
+						upload_allowed = false;
+					}
+				}
+				if(upload_allowed == false) {
+					alert(filename + ' already attached');
+					return false;
+				}
+			}
+		</script>
+		<input type="submit" class="button" onclick="return check_filename();" value="<?php echo lang_get( 'upload_file_button' ) ?>" />
 	</td>
 </tr>
 </table>
diff -Naur mantis-1.1.1/bug_view_page.php mantis/bug_view_page.php
--- mantis-1.1.1/bug_view_page.php	2007-10-14 00:36:41.000000000 +0200
+++ mantis/bug_view_page.php	2008-01-20 23:43:02.000000000 +0100
@@ -427,7 +427,7 @@
 		<?php echo lang_get( 'attached_files' ) ?>
 	</td>
 	<td colspan="5">
-		<?php file_list_attachments ( $f_bug_id ); ?>
+		<?php file_list_attachments ( $f_bug_id, true ); ?>
 	</td>
 </tr>
 <?php
diff -Naur mantis-1.1.1/core/file_api.php mantis/core/file_api.php
--- mantis-1.1.1/core/file_api.php	2007-12-20 06:46:29.000000000 +0100
+++ mantis/core/file_api.php	2008-01-20 23:43:58.000000000 +0100
@@ -145,7 +145,7 @@
 	# --------------------
 	# List the attachments belonging to the specified bug.  This is used from within
 	# bug_view_page.php and bug_view_advanced_page.php
-	function file_list_attachments( $p_bug_id ) {
+	function file_list_attachments( $p_bug_id, $p_with_js = false ) {
 		$t_attachment_rows = bug_get_attachments( $p_bug_id );
 
 		$num_files = sizeof( $t_attachment_rows );
@@ -159,11 +159,15 @@
 		$t_preview_image_ext = config_get( 'preview_image_extensions' );
 
 		$image_previewed = false;
+		$js = 'var fileArray = new Array();';
 		for ( $i = 0 ; $i < $num_files ; $i++ ) {
 			$row = $t_attachment_rows[$i];
 			extract( $row, EXTR_PREFIX_ALL, 'v' );
 
 			$t_file_display_name = string_display_line( file_get_display_name( $v_filename ) );
+			if ( $p_with_js ) {
+				$js .= 'fileArray[' . $i . '] = \'' . $t_file_display_name . '\';';
+			}
 			$t_filesize		= number_format( $v_filesize );
 			$t_date_added	= date( config_get( 'normal_date_format' ), db_unixtimestamp( $v_date_added ) );
 
@@ -281,6 +285,9 @@
 				PRINT "<br />\n";
 			}
 		}
+		if ( $p_with_js ) {
+			PRINT '<script type="text/javascript">' . $js . '</script>';
+		}
 	}
 	# --------------------
 	# delete all files that are associated with the given bug

Activities

vboctor

vboctor

2008-01-20 19:41

manager   ~0016734

I've just thought of another option. That is to auto-generate a name if the same name already exists. For example, sample.diff, would be sample_2.diff or something like that.