[LON-CAPA-cvs] cvs: loncom /homework inputtags.pm structuretags.pm /interface londocs.pm lonmsgdisplay.pm lonrss.pm lonsupportreq.pm portfolio.pm /javascriptlib file_upload.js /publisher lonpubdir.pm doc/loncapafiles loncapafiles.lpml

musolffc musolffc at source.lon-capa.org
Thu Jun 18 16:19:28 EDT 2015


musolffc		Thu Jun 18 20:19:28 2015 EDT

  Added files:                 
    /loncom/javascriptlib	file_upload.js 

  Modified files:              
    /doc/loncapafiles	loncapafiles.lpml 
    /loncom/homework	inputtags.pm structuretags.pm 
    /loncom/interface	londocs.pm lonmsgdisplay.pm lonrss.pm 
                     	lonsupportreq.pm portfolio.pm 
    /loncom/publisher	lonpubdir.pm 
  Log:
  File size checking for uploaded files
  
  A new javascript file, file_upload.js, handles client side file size checking.  
  The function, checkUploadSize(), accepts an html file input element and a maximum 
  file size (in bytes) as input.  If the size of the file to be uploaded exceeds the 
  size allowed, a message is displayed the the element is cleared.  Server side 
  checking is still used if it passes this test.
  
  Loading file_upload.js adds event handlers to file input elements with 
  class="flUpload".
      <input type="file" class="flUpload" />
  
  It also looks for a hidden element with id="free_space" that contains the maximum 
  upload size.
      <input type="hidden" id="free_space" value="$free_space" />
  
  The free space is usually calculated by subtracting a user's disk usage from their 
  quota allowance.  In some cases it is a fixed value.
  
  I have implemented this for every upload instance I could find.  These include:
  * Message attachments
  * Authoring space
  * Portfolio
  * Course editor
  * Essay response problems
  * Helpdesk request forms
  * RSS feed uploads
  
  
-------------- next part --------------
Index: doc/loncapafiles/loncapafiles.lpml
diff -u doc/loncapafiles/loncapafiles.lpml:1.919 doc/loncapafiles/loncapafiles.lpml:1.920
--- doc/loncapafiles/loncapafiles.lpml:1.919	Thu May 28 02:48:27 2015
+++ doc/loncapafiles/loncapafiles.lpml	Thu Jun 18 20:18:41 2015
@@ -2,7 +2,7 @@
  "http://lpml.sourceforge.net/DTD/lpml.dtd">
 <!-- loncapafiles.lpml -->
 
-<!-- $Id: loncapafiles.lpml,v 1.919 2015/05/28 02:48:27 raeburn Exp $ -->
+<!-- $Id: loncapafiles.lpml,v 1.920 2015/06/18 20:18:41 musolffc Exp $ -->
 
 <!--
 
@@ -4726,6 +4726,15 @@
   </description>
 </file>
 
+<file>
+  <source>loncom/javascriptlib/file_upload.js</source>
+  <target dist='default'>home/httpd/html/res/adm/includes/file_upload.js</target>
+  <categoryname>interface file</categoryname>
+  <description>
+    JavaScript dealing with file uploads
+  </description>
+</file>
+
 <fileglob>
   <glob>*.*</glob>
   <sourcedir>loncom/html/adm/jsMath/</sourcedir>
Index: loncom/homework/inputtags.pm
diff -u loncom/homework/inputtags.pm:1.335 loncom/homework/inputtags.pm:1.336
--- loncom/homework/inputtags.pm:1.335	Tue Apr 21 16:03:37 2015
+++ loncom/homework/inputtags.pm	Thu Jun 18 20:18:54 2015
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # input  definitons
 #
-# $Id: inputtags.pm,v 1.335 2015/04/21 16:03:37 raeburn Exp $
+# $Id: inputtags.pm,v 1.336 2015/06/18 20:18:54 musolffc Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -661,10 +661,12 @@
         $result .= $constraints.'<br />';
     }
     if ($which eq 'uploadonly' || $which eq 'both') { 
-	$result.=&mt('Submit a file: (only one file per submission)').
-	    ' <br /><input type="file" size="50" name="HWFILE'.
-	    $jspart.'_'.$id.'" id="HWFILE'.$jspart.'_'.$id.'" /><br />';
-    }
+        my $free_space = $maxfilesize * 1048576;
+        $result .= &mt('Submit a file: (only one file per submission)').
+            ' <br /><input type="file" size="50" name="HWFILE'.$jspart.'_'.$id.
+            '" id="HWFILE'.$jspart.'_'.$id.'" class="flUpload" /><br />'.
+            '<input type="hidden" id="free_space" value="'.$free_space.'" /><br />'
+        }
     if ( $which eq 'both') {
 	$result.='<br />'.'<strong>'.&mt('OR:').'</strong><br />';
     }
Index: loncom/homework/structuretags.pm
diff -u loncom/homework/structuretags.pm:1.537 loncom/homework/structuretags.pm:1.538
--- loncom/homework/structuretags.pm:1.537	Tue Apr 28 13:26:38 2015
+++ loncom/homework/structuretags.pm	Thu Jun 18 20:18:54 2015
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA 
 # definition of tags that give a structure to a document
 #
-# $Id: structuretags.pm,v 1.537 2015/04/28 13:26:38 raeburn Exp $
+# $Id: structuretags.pm,v 1.538 2015/06/18 20:18:54 musolffc Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -490,7 +490,9 @@
             }
         }
         if ($needs_upload) {
-            $extra_head .= &Apache::lonhtmlcommon::file_submissionchk_js();
+            $extra_head .= &Apache::lonhtmlcommon::file_submissionchk_js()
+                            .'<script type="text/javascript" 
+                                src="/res/adm/includes/file_upload.js"></script>';
         }
     }
 
Index: loncom/interface/londocs.pm
diff -u loncom/interface/londocs.pm:1.594 loncom/interface/londocs.pm:1.595
--- loncom/interface/londocs.pm:1.594	Tue Jun  9 21:22:56 2015
+++ loncom/interface/londocs.pm	Thu Jun 18 20:19:06 2015
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Documents
 #
-# $Id: londocs.pm,v 1.594 2015/06/09 21:22:56 damieng Exp $
+# $Id: londocs.pm,v 1.595 2015/06/18 20:19:06 musolffc Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -4769,7 +4769,9 @@
               .'// <![CDATA['."\n"
               .$script."\n"
               .'// ]]>'."\n"
-              .'</script>'."\n";
+              .'</script>'."\n"
+              .'<script type="text/javascript" 
+                src="/res/adm/includes/file_upload.js"></script>'."\n";
 
     # Breadcrumbs
     &Apache::lonhtmlcommon::clear_breadcrumbs();
@@ -4899,9 +4901,30 @@
                 'webctce4' => 'WebCT 4 Campus Edition',
         );
 # -----------------------------------------------------------------------------
+
+    # Calculate free quota space for a user or course. A javascript function checks
+    # file size to determine if upload should be allowed.
+    my $quotatype = 'unofficial';
+    if ($crstype eq 'Community') {
+        $quotatype = 'community';    
+    } elsif ($env{'course.'.$coursedom.'_'.$coursenum.'.internal.coursecode'}) {
+        $quotatype = 'official';
+    } elsif ($env{'course.'.$coursedom.'_'.$coursenum.'.internal.textbook'}) {
+        $quotatype = 'textbook';
+    }
+    my $disk_quota = &Apache::loncommon::get_user_quota($coursenum,$coursedom,
+                     'course',$quotatype); # expressed in MB
+    my $current_disk_usage = 0;
+    foreach my $subdir ('docs','supplemental') {
+        $current_disk_usage += &Apache::lonnet::diskusage($coursedom,$coursenum,
+                               "userfiles/$subdir",1); # expressed in kB
+    }
+    my $free_space = 1024 * ((1024 * $disk_quota) - $current_disk_usage);
+
 	my $fileupload=(<<FIUP);
 	$lt{'file'}:<br />
-	<input type="file" name="uploaddoc" size="40" />
+	<input type="file" name="uploaddoc" class="flUpload" size="40" />
+    <input type="hidden" id="free_space" value="$free_space" />
 FIUP
 
 	my $checkbox=(<<CHBO);
@@ -4948,7 +4971,7 @@
         <fieldset id="uploaddocform" style="display: none;">
         <legend>$lt{'upfi'}</legend>
 	<input type="hidden" name="active" value="aa" />
-	$fileupload
+    $fileupload
 	<br />
 	$lt{'title'}:<br />
 	<input type="text" size="60" name="comment" />
Index: loncom/interface/lonmsgdisplay.pm
diff -u loncom/interface/lonmsgdisplay.pm:1.181 loncom/interface/lonmsgdisplay.pm:1.182
--- loncom/interface/lonmsgdisplay.pm:1.181	Tue Jun  9 21:22:56 2015
+++ loncom/interface/lonmsgdisplay.pm	Thu Jun 18 20:19:06 2015
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Routines for messaging display
 #
-# $Id: lonmsgdisplay.pm,v 1.181 2015/06/09 21:22:56 damieng Exp $
+# $Id: lonmsgdisplay.pm,v 1.182 2015/06/18 20:19:06 musolffc Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1400,11 +1400,14 @@
                                        'to'   => 'To:',
                                       );
     my %attachmax = (
-                     text => &mt('(128 KB max size)'),
-                     num  => 131072,
+                     text => &mt('(1 MB max size)'),
+                     num  => 1048576,
                     );
     if (!$forwarding && !$multiforward) {
-        $attachrow = '<tr><td colspan="3"><b>'.$lt{'atta'}.'</b> '.$attachmax{'text'}.': <input type="file" name="attachment" /></td></tr>';
+        $attachrow = '<tr><td colspan="3"><b>'.$lt{'atta'}.'</b> '.$attachmax{'text'}
+            .': <input type="file" name="attachment" class="flUpload" multiple />'
+            .'</td></tr>'
+            .'<input type="hidden" id="free_space" value="'.$attachmax{'num'}.'" />';
     }
     if (&Apache::lonnet::allowed('srm',$env{'request.course.id'})
 	|| &Apache::lonnet::allowed('srm',$env{'request.course.id'}.
@@ -2667,6 +2670,8 @@
     if ($baseurl) {
 	$extra .= "<base href=\"".&Apache::lonnet::absolute_url()."/$baseurl\" />";
     }
+    $extra .= '<script type="text/javascript"
+                src="/res/adm/includes/file_upload.js"></script>';
     $r->print(&Apache::loncommon::start_page('Messages',
  					$extra));
     $r->print(&Apache::lonhtmlcommon::breadcrumbs
Index: loncom/interface/lonrss.pm
diff -u loncom/interface/lonrss.pm:1.53 loncom/interface/lonrss.pm:1.54
--- loncom/interface/lonrss.pm:1.53	Mon Jul 15 14:32:44 2013
+++ loncom/interface/lonrss.pm	Thu Jun 18 20:19:06 2015
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # RSS Feeder
 #
-# $Id: lonrss.pm,v 1.53 2013/07/15 14:32:44 bisitz Exp $
+# $Id: lonrss.pm,v 1.54 2015/06/18 20:19:06 musolffc Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -345,8 +345,10 @@
 #	my $title = $displayfeedname?$displayfeedname:"Available RSS Feeds and Blogs";
         my $title = "My Space";
 	my $rss_link = &Apache::lonrss::rss_link($uname,$udom);
+    my $head_extra = $rss_link.'<script type="text/javascript"
+                                src="/res/adm/includes/file_upload.js"></script>';
 	my $brcrumb = [{href=>$rss_link,text=>"Available RSS Feeds and Blogs"}];
-	$r->print(&Apache::loncommon::start_page($title,$rss_link,
+	$r->print(&Apache::loncommon::start_page($title,$head_extra,
 			 {'bread_crumbs'   => $brcrumb,
 			  'domain'         => $udom,
 			  'force_register' => $env{'form.register'}}).
@@ -534,11 +536,23 @@
 							   'title' => 'Title',
 							   'link' => 'Link',
 							   'description' => 'Description',
-                                                           'enc' => 'Podcasted enclosure');
-                        my $uploadlink;
-                        if ($entry==$newid) {
+                               'enc' => 'Podcasted enclosure');
+            my $uploadlink;
+            if ($entry==$newid) {
 # Generate upload link only for last (new) entry
-			    $uploadlink=&Apache::inputtags::file_selector(0,0,'*','both');
+                # Calculate the quota space available in the user's portfolio
+                my $disk_quota = &Apache::loncommon::get_user_quota($env{'user.name'},
+                                    $env{'user.domain'}); # expressed in MB
+                my $portfolio_root = '/userfiles/portfolio';
+                my $getpropath = 1;
+                my $current_disk_usage = &Apache::lonnet::diskusage(
+                        $env{'user.domain'}, $env{'user.name'}, 
+                        $portfolio_root, $getpropath); # Expressed in kB
+                # Convert to MB for use in file_selector()
+                my $free_space = $disk_quota - ($current_disk_usage / 1024.); 
+                # Format this number since it will be displayed onscreen
+                $free_space = sprintf("%.1f", $free_space);
+                $uploadlink=&Apache::inputtags::file_selector(0,0,'*','both','',$free_space);
 			} else {
 # Otherwise, display
                             $uploadlink='<tt>'.$newsfeed{$id.'_enclosureurl'}.'</tt>'.
Index: loncom/interface/lonsupportreq.pm
diff -u loncom/interface/lonsupportreq.pm:1.79 loncom/interface/lonsupportreq.pm:1.80
--- loncom/interface/lonsupportreq.pm:1.79	Tue Jun  9 21:22:57 2015
+++ loncom/interface/lonsupportreq.pm	Thu Jun 18 20:19:06 2015
@@ -1,5 +1,5 @@
 #
-# $Id: lonsupportreq.pm,v 1.79 2015/06/09 21:22:57 damieng Exp $
+# $Id: lonsupportreq.pm,v 1.80 2015/06/18 20:19:06 musolffc Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -145,7 +145,7 @@
                   subj => 'Subject',
                   detd => 'Detailed Description',
                   opfi => 'Optional file upload',
-                  uplf => 'Upload a file (e.g., a screenshot) relevant to your help request (128 KB max.)',
+                  uplf => 'Upload a file (e.g., a screenshot) relevant to your help request (1 MB max.)',
                   fini => 'Finish',
                   clfm => 'Clear Form',
     );
@@ -275,6 +275,7 @@
 $loaditems
 // ]]>
 </script>
+<script type="text/javascript" src="/res/adm/includes/file_upload.js"></script>
 ENDJS
     my %add_entries = (
                        style    => "margin-top:0px;margin-bottom:0px;",
@@ -506,10 +507,11 @@
     $i = $num%2; 
     if (($env{'user.name'} =~ /^$match_username$/) && (!$public)) {
         if ($homeserver) {
-            $output .= &Apache::lonhtmlcommon::row_title($html_lt{'opfi'},undef,$css[$i]).
-                       ' <input type="file" name="screenshot" size="20" /><br />'.
-                       "\n".$html_lt{'uplf'}."\n".
-                       &Apache::lonhtmlcommon::row_closure();
+            $output .= &Apache::lonhtmlcommon::row_title($html_lt{'opfi'},undef,$css[$i])
+                .' <input type="file" name="screenshot" class="flUpload" size="20" />'
+                .'<input type="hidden" id="free_space" value="1048576" />'
+                .'<br />'."\n".$html_lt{'uplf'}."\n"
+                .&Apache::lonhtmlcommon::row_closure();
             $num ++;
             $i = $num%2;
         }
@@ -843,9 +845,9 @@
     if ((defined($env{'user.name'})) && (!$public)) {
         if ($homeserver && $env{'form.screenshot.filename'}) {
             $attachmentsize = length($env{'form.screenshot'});
-            if ($attachmentsize > 131072) {
+            if ($attachmentsize > 1048576) {
                 $displaymsg .= '<br /><span class="LC_warning">'.
-                               &mt('The uploaded screenshot file ([_1] bytes) included with your request exceeded the maximum allowed size - 128 KB, and has therefore been discarded.',$attachmentsize).'</span>';
+                               &mt('The uploaded screenshot file ([_1] bytes) included with your request exceeded the maximum allowed size - 1 MB, and has therefore been discarded.',$attachmentsize).'</span>';
             } else {
                 $attachmentpath=&Apache::lonnet::userfileupload('screenshot',undef,'helprequests');
             }
Index: loncom/interface/portfolio.pm
diff -u loncom/interface/portfolio.pm:1.255 loncom/interface/portfolio.pm:1.256
--- loncom/interface/portfolio.pm:1.255	Sun Dec  7 19:45:19 2014
+++ loncom/interface/portfolio.pm	Thu Jun 18 20:19:06 2015
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # portfolio browser
 #
-# $Id: portfolio.pm,v 1.255 2014/12/07 19:45:19 raeburn Exp $
+# $Id: portfolio.pm,v 1.256 2015/06/18 20:19:06 musolffc Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -112,13 +112,17 @@
 END
         }
 
+        # Find space available before uploading
+        my $free_space = &free_space($group);
+
         # Upload File
         $r->print('<div class="LC_left_float">'
                  .'<form method="post" enctype="multipart/form-data" action="'.$escuri.'">'
                  .'<fieldset>'
                  .'<legend>'.$lt{'upload_label'}.'</legend>'
                  .$groupitem 
-                 .'<input name="uploaddoc" type="file" />'
+                 .'<input name="uploaddoc" type="file" class="flUpload" />'
+                 .'<input type="hidden" id="free_space" value="'.$free_space.'" />'
                  .'<input type="hidden" name="currentpath" value="'.$current_path.'" />'
                  .'<input type="hidden" name="action" value="'.$env{"form.action"}.'" />'
                  .'<input type="hidden" name="symb" value="'.$env{"form.symb"}.'" />'
@@ -2597,6 +2601,19 @@
     return $state;
 }
 
+# Find space available in a user's portfolio (convert to bytes)
+sub free_space {
+    my ($group) = @_;
+    my $disk_quota = &get_quota($group); # Expressed in kB
+    my ($uname,$udom) = &get_name_dom($group);
+    my $portfolio_root = &get_portfolio_root();
+    my $getpropath = 1;
+    my $current_disk_usage = &Apache::lonnet::diskusage($udom, $uname,
+                             $portfolio_root, $getpropath); # Expressed in kB
+    my $free_space = 1024 * ($disk_quota - $current_disk_usage);
+    return $free_space;
+}
+
 sub handler {
     # this handles file management
     my $r = shift;
@@ -2684,16 +2701,19 @@
     # Give the LON-CAPA page header
     my $brcrum = [{href=>"/adm/portfolio",text=>"Portfolio Manager"}];
 
+    my $js = '<script type="text/javascript"
+                src="/res/adm/includes/file_upload.js"></script>';
+    
     if ($env{"form.mode"} eq 'selectfile'){
-        $r->print(&Apache::loncommon::start_page($title,undef,
+        $r->print(&Apache::loncommon::start_page($title, $js,
 						 {'only_body' => 1}));
     } elsif ($env{'form.action'} eq 'rolepicker') {
-        $r->print(&Apache::loncommon::start_page('New role-based condition',undef,
+        $r->print(&Apache::loncommon::start_page('New role-based condition', $js,
                                                  {'no_nav_bar'  => 1, }));
     } elsif ($caller eq 'coursegrp_portfolio') {
-        $r->print(&Apache::loncommon::start_page($title));
+        $r->print(&Apache::loncommon::start_page($title, $js));
     } else {
-        $r->print(&Apache::loncommon::start_page($title,undef,
+        $r->print(&Apache::loncommon::start_page($title, $js,
                                                  {'bread_crumbs' => $brcrum}));
         if (!&Apache::lonnet::usertools_access($uname,$udom,'portfolio')) {
             $r->print('<h2>'.&mt('No user portfolio available') .'</h2>'.
Index: loncom/publisher/lonpubdir.pm
diff -u loncom/publisher/lonpubdir.pm:1.163 loncom/publisher/lonpubdir.pm:1.164
--- loncom/publisher/lonpubdir.pm:1.163	Mon Jan 19 15:36:11 2015
+++ loncom/publisher/lonpubdir.pm	Thu Jun 18 20:19:27 2015
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # Authoring Space Directory Lister
 #
-# $Id: lonpubdir.pm,v 1.163 2015/01/19 15:36:11 goltermann Exp $
+# $Id: lonpubdir.pm,v 1.164 2015/06/18 20:19:27 musolffc Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -67,7 +67,9 @@
             $r->send_http_header;
 
             &Apache::lonhtmlcommon::clear_breadcrumbs();
-            $r->print(&Apache::loncommon::start_page('Authoring Space').
+            my $js = '<script type="text/javascript" 
+                        src="/res/adm/includes/file_upload.js"></script>';
+            $r->print(&Apache::loncommon::start_page('Authoring Space', $js).
                       '<div class="LC_error">'.
                       '<br /><p>'.
                       &mt('Your Authoring Space is currently in the location used by LON-CAPA version 2.10 and older, but your domain is using a newer LON-CAPA version ([_1]).',$version).'</p>'.
@@ -87,7 +89,14 @@
 
     my %bombs=&Apache::lonmsg::all_url_author_res_msg($uname,$udom);
 
-    &startpage($r, $uname, $udom, $thisdisfn);  # Put out the start of page.
+    my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
+    my $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,
+                                "$londocroot/priv/$udom/$uname"); # expressed in kB
+    my $disk_quota = &Apache::loncommon::get_user_quota($uname,$udom,
+                                'author'); # expressed in MB
+
+    # Put out the start of page.
+    &startpage($r, $uname, $udom, $thisdisfn, $current_disk_usage, $disk_quota); 
 
     if (!-d $fn) {
         if (-e $fn) {
@@ -108,8 +117,8 @@
         return OK;
     }
 
-    &dircontrols($r,$uname,$udom,$thisdisfn);   # Put out actions for directory, 
-                                                # browse/upload + new file page.
+    # Put out actions for directory, browse/upload + new file page.
+    &dircontrols($r,$uname,$udom,$thisdisfn, $current_disk_usage, $disk_quota);
     &resourceactions($r,$uname,$udom,$thisdisfn); # Put out form used for printing/deletion etc.
 
     my $numdir = 0;
@@ -332,14 +341,16 @@
 #   - The HTML header 
 #   - The H1/H3  stuff which includes the directory.
 #
-#     startpage($r, $uame, $udom, $thisdisfn);
+#     startpage($r, $uame, $udom, $thisdisfn, $current_disk_usage, $disk_quota);
 #      $r     - The apache request object.
 #      $uname - User name.
 #      $udom  - Domain name the user is logged in under.
 #      $thisdisfn - Displayable version of the filename.
+#      $current_disk_usage - User's current disk usage (in kB).
+#      $disk_quota - Disk quota for user's authoring space (in MB).
 
 sub startpage {
-    my ($r, $uname, $udom, $thisdisfn) = @_;
+    my ($r, $uname, $udom, $thisdisfn, $current_disk_usage, $disk_quota) = @_;
     &Apache::loncommon::content_type($r,'text/html');
     $r->send_http_header;
 
@@ -356,12 +367,11 @@
     # in start_page->bodytag->innerregister
 
     $env{'request.noversionuri'}=$formaction;
-    $r->print(&Apache::loncommon::start_page('Authoring Space'));
+    my $js = '<script type="text/javascript" 
+                src="/res/adm/includes/file_upload.js"></script>';
+    $r->print(&Apache::loncommon::start_page('Authoring Space', $js));
 
-    my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
-    my $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,"$londocroot/priv/$udom/$uname");
-    my $disk_quota = &Apache::loncommon::get_user_quota($uname,$udom,'author'); #expressed in MB
-    $disk_quota = 1000 * $disk_quota; # convert from MB to kB
+    $disk_quota = 1024 * $disk_quota; # convert from MB to kB
 
     $r->print(&Apache::loncommon::head_subbox(
                      '<div style="float:right;padding-top:0;margin-top;0">'
@@ -483,7 +493,7 @@
 }
 
 sub dircontrols {
-    my ($r,$uname,$udom,$thisdisfn) = @_;
+    my ($r,$uname,$udom,$thisdisfn, $current_disk_usage, $disk_quota) = @_;
     my %lt=&Apache::lonlocal::texthash(
                                        cnpd => 'Cannot publish directory',
                                        cnrd => 'Cannot retrieve directory',
@@ -522,6 +532,9 @@
 				       pick => 'Please select an action to perform using the new filename',
                                       );
     my $mytype = $lt{'type'}; # avoid conflict with " and ' in javascript
+    # Calculate free space in bytes.
+    # $disk_quota is in MB and $current_disk_usage is in kB
+    my $free_space = 1024 * ((1024 * $disk_quota) - $current_disk_usage);
     $r->printf(<<END,&Apache::loncommon::help_open_topic('Quicksearch'));
 <div class="LC_columnSection">
   <div>
@@ -552,7 +565,8 @@
       <fieldset>
         <legend>$lt{'updc'}</legend>
         <input type="hidden" name="filename" value="/priv$thisdisfn/" />
-        <input type="file" name="upfile" size="20" />
+        <input type="file" name="upfile" class="flUpload testclass" size="20" />
+        <input type="hidden" id="free_space" value=$free_space />
         <input type="button" value="$lt{'uplo'}"  onclick="checkUpload(this.form)" />
       </fieldset>
     </form>

Index: loncom/javascriptlib/file_upload.js
+++ loncom/javascriptlib/file_upload.js
/* 
The LearningOnline Network with CAPA
JavaScript functions handling file uploading

$Id: file_upload.js,v 1.1 2015/06/18 20:19:18 musolffc Exp $

Copyright Michigan State University Board of Trustees

This file is part of the LearningOnline Network with CAPA (LON-CAPA).

LON-CAPA is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

LON-CAPA is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with LON-CAPA; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

/home/httpd/html/adm/gpl.txt

http://www.lon-capa.org/
*/



/*
This function accepts a file input element and a maximum upload size.  If the 
file(s) is too large, an alert is shown and the input is cleared.  It is better
to do this check on the client before uploading.

INPUT:
    fileInput -
        <input type="file" class="flUpload" />
        Using the class "flUpload" is needed to use the event handlers below.
    maxSize -
        Maximum upload size in bytes.  It is usually calculated from quota and 
        disk usage.
*/
function checkUploadSize (fileInput, maxSize) {
    try {
        var fileSize = 0;
        if ('files' in fileInput) {
            if (fileInput.files.length > 0) {
                for (var i = 0; i < fileInput.files.length; i++) {
                    fileSize += fileInput.files[i].size;
                }
                if (fileSize > maxSize) {
                    alert("File(s) too large to be attached");
                    clearFileInput(fileInput);
                }
            }
        } else { alert("no files in upFiles");}
    } catch (e) { alert("Error is: " + e); }
}

/* 
This function clears the contents of a file input element.

INPUT:
    ctrl -
        <input type="file" />
*/
function clearFileInput(ctrl) {
    try {
        ctrl.value = null;
    } catch(ex) { }
    if (ctrl.value) {
        ctrl.parentNode.replaceChild(ctrl.cloneNode(true), ctrl);
    }
}

/*
This block adds event listeners to file upload elements.  It looks for input
elements with class="flUpload".

    <input type="file" class="flUpload" />

It also looks for a hidden element with id="free_space" that contains the maximum
upload size.

    <input type="hidden" id="free_space" value="$free_space" />

When the contents of the input element change, the function checkUploadSize()
checks if it is allowed based on size.
*/
$( document ).ready(function() {
    var maxSize = $( "#free_space" ).val();
    var upload_elements = $( ".flUpload" );
    for (var i=0; i<upload_elements.length; i++) {
        upload_elements[i].addEventListener( "change", function(){
            checkUploadSize(this, maxSize);
        });
    }
});


More information about the LON-CAPA-cvs mailing list