Changeset 3008


Ignore:
Timestamp:
Dec 7, 2006, 2:52:12 PM (16 years ago)
Author:
Nicklas Nordborg
Message:

References #454: File upload times out

Changed the way progress reporter works. It now requires less bytes to be sent since the whole
page no longer has to be reloaded.

Location:
branches/2.1.2
Files:
1 added
7 edited

Legend:

Unmodified
Added
Removed
  • branches/2.1.2/src/clients/web/net/sf/basedb/clients/web/fileupload/FileUpload.java

    r3003 r3008  
    6565    arrive.
    6666  */
    67   public static final long DEFAULT_TIMEOUT = 20000;
     67  public static final long DEFAULT_TIMEOUT = 60000;
    6868 
    6969  private static final String EMPTY_STRING = "";
     
    108108  /**
    109109    The timeout in milliseconds that we wait for more data to arrive.
    110     Default value is 20000 = 20 seconds.
     110    Default value is 60000 = 60 seconds.
    111111  */
    112112  private long timeout = DEFAULT_TIMEOUT;
     
    636636      try
    637637      {
    638         if (debugEnabled) log.debug("Calling in.readLine() for more data");
    639638        bytes = in.readLine(b, off, len);
    640         if (debugEnabled) log.debug("Got " + bytes + " bytes");
    641639        gotData = true;
    642640      }
     
    648646        {
    649647          SocketTimeoutException ex2 =
    650             new SocketTimeoutException("No data for " + waitTime + " milliseconds");
     648            new SocketTimeoutException("No data for " + (waitTime / 1000) + " seconds");
    651649          ex2.initCause(ex);
    652           log.debug("Total timeout exceeded after" + waitTime + "ms", ex2);
     650          log.debug("Total timeout exceeded after " + waitTime + "ms", ex2);
    653651          throw ex2;
    654652        }
  • branches/2.1.2/src/clients/web/net/sf/basedb/clients/web/fileupload/FileUploadProgress.java

    r2691 r3008  
    2424package net.sf.basedb.clients.web.fileupload;
    2525
    26 import java.util.Date;
    27 
    28 import net.sf.basedb.core.ProgressReporter;
    29 
    3026/**
    3127  Objects of this class holds information about the progress
     
    4339*/
    4440public class FileUploadProgress
    45   implements ProgressReporter
    4641{
    4742  private final long totalBytes;
    4843  private final long startTime;
    49   private final Date lastTransfer;
    5044  private long transferredBytes;
    5145  private String currentFilename;
    5246  private Throwable exception;
    5347  private boolean abort = false;
    54   private boolean unpacking = false;
    55   private int percent = 0;
    56   private String message = null;
    5748
    5849  /**
     
    6859    this.totalBytes = totalBytes;
    6960    this.startTime = System.currentTimeMillis();
    70     this.lastTransfer = new Date();
    7161    this.transferredBytes = 0;
    7262  }
    73  
    74   /*
    75     From the ProgressReporter interface
    76     -------------------------------------------
    77   */
    78   public void display(int percent, String message)
    79   {
    80     this.unpacking = percent < 100;
    81     this.message = message;
    82     this.percent = percent;
    83   }
    84  
    85   public void append(String message)
    86   {
    87     this.message += message;
    88   }
    89   // -------------------------------------------
    9063 
    9164  /**
     
    11386  {
    11487    transferredBytes = bytes;
    115     lastTransfer.setTime(System.currentTimeMillis());
    11688  }
    11789  /**
     
    12294  {
    12395    transferredBytes += bytes;
    124     lastTransfer.setTime(System.currentTimeMillis());
    12596  }
    12697 
    12798  /**
    12899    Checks if the transfer is complete or not. A transfer is complete
    129     when the transferred bytes equals the total bytes and when optional
    130     unpacking of a zip file has been completed.
     100    when the transferred bytes equals the total bytes.
    131101    @return TRUE if the transfer has been completed, FALSE otherwise
    132102  */
    133103  public boolean transferIsComplete()
    134104  {
    135     return (getTotalBytes() == getTransferredBytes() && !unpacking);
     105    return (getTotalBytes() == getTransferredBytes());
    136106  }
    137107
     
    179149
    180150  /**
    181     Get the data and time for the last update to this object.
    182     This value is used by the a {@link FileUpload} object
    183     to decide when to remove old progress information from the cache.
    184     @return The data and time of the last update to this object.
    185   */
    186   Date getLastTransfer()
    187   {
    188     return lastTransfer;
    189   }
    190  
    191   /**
    192151    Request that the upload should be aborted. The associated {@link
    193152    FileUpload} object will check this property at regular intervals and if set
  • branches/2.1.2/src/core/net/sf/basedb/util/zip/ZipUnpacker.java

    r2691 r3008  
    4040import net.sf.basedb.core.Path;
    4141import net.sf.basedb.util.FileUtil;
     42import net.sf.basedb.util.Values;
    4243
    4344/**
     
    101102      {
    102103        progress.displayAbsolute(totalUnpacked,
    103           "Unpacking " + (isDirectory ? "directory: " : "file: ") + completePath);
     104          "Unpacking " + (isDirectory ? "directory: " : "file: ") + completePath +
     105          " (" +Values.formatBytes(entry.getSize()) + ")");
    104106      }
    105107      if (!isDirectory)
  • branches/2.1.2/www/filemanager/upload/index.jsp

    r2753 r3008  
    3434  @version 2.0
    3535--%>
    36 <%@ page
     36<%@ page session="false"
    3737  import="net.sf.basedb.core.SessionControl"
    3838  import="net.sf.basedb.core.DbControl"
     
    5252int directoryId = Values.getInt(request.getParameter("directory_id"));
    5353int fileId = Values.getInt(request.getParameter("file_id"));
    54 String thePage = "select.jsp";
    55 FileUploadProgress progress = (FileUploadProgress)sc.getSessionSetting("FileUploadProgress");
    56 if (progress != null && !progress.transferIsComplete() && progress.getException() == null)
    57 {
    58   thePage = "progress.jsp";
    59 }
    60 else
    61 {
    62   sc.setSessionSetting("FileUploadProgress", null);
    63 }
     54sc.setSessionSetting("FileUploadProgress", null);
     55sc.setSessionSetting("UnpackProgress", null);
    6456%>
    6557
    6658<base:page type="frameset" title="Upload file">
    6759  <base:head />
    68   <frameset rows="*,0" frameborder="no" border="0">
    69     <frame name="progress" src="<%=thePage%>?ID=<%=ID%>&directory_id=<%=directoryId%>&file_id=<%=fileId%>"
     60  <frameset rows="*,0,0" frameborder="yes" border="1">
     61    <frame name="select" src="select.jsp?ID=<%=ID%>&directory_id=<%=directoryId%>&file_id=<%=fileId%>"
    7062      scrolling="no" frameborder="0">
     63    <frame name="progress" scrolling="no" frameborder="0">
    7164    <frame name="upload" scrolling="no" frameborder="0">
    7265  </frameset>
  • branches/2.1.2/www/filemanager/upload/progress.jsp

    r2753 r3008  
    3535  @version 2.0
    3636--%>
    37 <%@ page
     37<%@ page session="false"
    3838  import="net.sf.basedb.core.SessionControl"
    39   import="net.sf.basedb.core.SimpleProgressReporter"
     39  import="net.sf.basedb.core.SimpleAbsoluteProgressReporter"
    4040  import="net.sf.basedb.clients.web.Base"
    4141  import="net.sf.basedb.util.Values"
     
    5151final boolean abort = Values.getBoolean(request.getParameter("abort"));
    5252final FileUploadProgress progress = (FileUploadProgress)sc.getSessionSetting("FileUploadProgress");
    53 final SimpleProgressReporter unpackProgress = (SimpleProgressReporter)sc.getSessionSetting("UnpackProgress");
     53final SimpleAbsoluteProgressReporter unpackProgress = (SimpleAbsoluteProgressReporter)sc.getSessionSetting("UnpackProgress");
     54
    5455boolean aborted = false;
    55 if (progress != null)
    56 {
    57   if (abort) progress.setAbort();
    58   Throwable t = progress.getException();
    59   if (t != null)
    60   {
    61     if (t instanceof UploadAbortedException)
    62     {
    63       aborted = true;
    64     }
    65     else
    66     {
    67       throw progress.getException();
    68     }
    69   }
    70 }
     56boolean transferIsComplete = false;
    7157long total = 0;
    7258long transferred = 0;
    73 boolean transferIsComplete = false;
    7459long seconds = 0;
    7560long transferRate = 0;
     
    7762String currentFileName = "";
    7863long percent = 0;
     64String unpackMessage = "";
    7965if (progress != null)
    8066{
    81   currentFileName = "Uploading " + progress.getCurrentFilename();
     67  if (abort) progress.setAbort();
     68  currentFileName = progress.getCurrentFilename();
    8269  total = progress.getTotalBytes();
    8370  transferred = progress.getTransferredBytes();
     
    8774  remain = transferRate != 0 ? (total-transferred)/transferRate : 0;
    8875  percent = (total==0 ? 0 : 100 * transferred / total);
     76  aborted = progress.getException() != null || abort;
    8977}
    90 final String onload = transferIsComplete ? "xdone();" : aborted ? null : "setTimeout('location.reload()', 1500);";
     78if (unpackProgress != null)
     79{
     80  unpackMessage = unpackProgress.getMessage();
     81}
     82
     83String onload = "";
    9184if (transferIsComplete)
    9285{
     86  onload = "displayProgress(true)";
    9387  sc.setSessionSetting("FileUploadProgress", null);
     88  sc.setSessionSetting("UnpackProgress", null);
     89}
     90else if (aborted)
     91{
     92  onload = "uploadAborted()";
     93}
     94else if (progress != null)
     95{
     96  onload = "displayProgress(false)";
     97}
     98else
     99{
     100  onload = "setTimeout('location.reload()', 1500)";
    94101}
    95102%>
    96 <base:page type="popup" title="Uploading file...">
    97   <base:head styles="progressbar.css">
    98   <script language="JavaScript">
    99   function done()
     103<html>
     104<head>
     105</head>
     106<body onload="<%=onload%>">
     107  <script language="javascript">
     108  function displayProgress(isComplete)
    100109  {
    101     var message = '<%=HTML.urlEncode(aborted ? "Upload aborted" : "File uploaded successfully")%>';
    102     location.href = getRoot()+'common/close_popup.jsp?refresh_opener=1&message='+(message);
    103   }
    104   function abortUpload()
    105   {
    106     if (confirm('Are you sure you want to abort the upload? It cannot be resumed.'))
     110    var fileName = '<%=HTML.javaScriptEncode(currentFileName)%>';
     111    var percentDone = '<%=percent%>';
     112    var transferredBytes = '<%=Values.formatBytes(transferred)%>';
     113    var totalBytes = '<%=Values.formatBytes(total)%>';
     114    var transferTime = '<%=Values.formatTime(seconds)%>';
     115    var transferRate = '<%=Values.formatBytes(transferRate)%>/s';
     116    var remainTime = '<%=Values.formatTime(remain)%>';
     117    var unpackMessage = '<%=HTML.javaScriptEncode(unpackMessage)%>';
     118    window.top.frames['select'].displayProgress(fileName, percentDone, transferredBytes, totalBytes, transferTime, transferRate, remainTime, unpackMessage);
     119    if (!isComplete)
    107120    {
    108       location.href = 'progress.jsp?ID=<%=ID%>&abort=1';
     121      setTimeout('location.reload()', 1500);
    109122    }
    110123  }
    111   <%
    112   if (abort && progress == null)
     124  function displayException()
    113125  {
    114     %>
    115     top.window.close();
    116     <%
     126    window.top.frames['select'].location = 'show_exception.jsp?ID=<%=ID%>';
    117127  }
    118   %>
     128  function uploadAborted()
     129  {
     130    window.top.location = '../../common/close_popup.jsp?refresh_opener=1&message=<%=HTML.urlEncode("The upload was aborted before it was completed.")%>';
     131  }
    119132  </script>
    120   </base:head>
    121   <base:body onload="<%=onload%>" >
    122 
    123   <%
    124   if (aborted)
    125   {
    126     %>
    127     <base:note type="info" title="Upload aborted">
    128       The upload was aborted before it was completed.
    129     </base:note>
    130     <table align="center">
    131     <tr>
    132       <td><base:button onclick="window.top.close()" title="Close" /></td>
    133     </tr>
    134     </table>
    135     <%
    136   }
    137   else
    138   {
    139     %>
    140     <h3 class="docked"><%=currentFileName%>...</h3>
    141     <div class="boxed" style="background: #E0E0E0">
    142       <br>
    143       <table width="90%" border=0 cellspacing=0 cellpadding=0 align="center">
    144       <tr>
    145       <td width="90%" >
    146         <table width="100%" class="progressbar" border=0 cellspacing=0 cellpadding=0>
    147         <tr>
    148           <%
    149           if (percent > 0)
    150           {
    151             %>
    152             <td width="<%=percent%>%" class="done">&nbsp;</td>
    153             <%
    154           }
    155           if (percent < 100)
    156           {
    157             %>
    158             <td width="<%=100-percent%>%" class="remain">&nbsp;</td>
    159             <%
    160           }
    161           %>
    162         </tr>
    163         </table>
    164       </td>
    165         <td width="10%">&nbsp;<%=percent%>%</td>
    166       </tr>
    167       <tr>
    168         <td colspan="2">
    169           <%
    170           if (progress == null)
    171           {
    172             %>
    173             Waiting for transfer to begin...
    174             <%
    175           }
    176           else
    177           {
    178             %>
    179             <b>Transferred:</b> <%=Values.formatBytes(transferred)%> of <%=Values.formatBytes(total)%><br>
    180             <b>Time:</b> <%=Values.formatTime(seconds)%> (<%=Values.formatBytes(transferRate)%>/s)<br>
    181             <b>Remain:</b> <%=Values.formatTime(remain)%> (estimated)<br>
    182             <%
    183             if (unpackProgress != null)
    184             {
    185               %>
    186               <b><%=unpackProgress.getMessage()%></b>
    187               <%
    188             }
    189           }
    190           %>
    191         </td>
    192       </tr>
    193       </table>
    194       <br>
    195     </div>
    196     <br>
    197     <table align="center">
    198     <tr>
    199       <td><base:button image="cancel.gif" onclick="abortUpload()" title="Abort..." /></td>
    200     </tr>
    201     </table>
    202     <%
    203   }
    204   %>
    205   </base:body>
    206 </base:page>
     133</body>
     134</html>
  • branches/2.1.2/www/filemanager/upload/select.jsp

    r2753 r3008  
    2626  @version 2.0
    2727--%>
    28 <%@ page
     28<%@ page session="false"
    2929  import="net.sf.basedb.core.SessionControl"
    3030  import="net.sf.basedb.core.DbControl"
     
    118118
    119119  <base:page type="popup" title="<%=title%>">
    120   <base:head scripts="tabcontrol.js" styles="tabcontrol.css">
     120  <base:head scripts="tabcontrol.js" styles="tabcontrol.css,progressbar.css">
    121121    <script language="JavaScript">
    122122    // Validate the "File" tab
     
    149149     
    150150        frm.submit();
    151         this.location.href = "progress.jsp?ID=<%=ID%>";
    152       }
    153     }
     151        window.top.frames['progress'].location.href = 'progress.jsp?ID=<%=ID%>';
     152        Main.hide('selectForm');
     153        Main.show('progressForm');
     154      }
     155    }
     156   
     157    function abortUpload()
     158    {
     159      if (confirm('Are you sure you want to abort the upload? It cannot be resumed.'))
     160      {
     161        window.top.frames['progress'].location.href = 'progress.jsp?ID=<%=ID%>&abort=1';
     162      }
     163    }
     164   
    154165    function fileOnChange()
    155166    {
     
    199210      }
    200211      %>
     212    }
     213   
     214    function displayProgress(fileName, percentDone, transferredBytes, totalBytes, transferTime, transferRate, remainTime, unpackMessage)
     215    {
     216      document.getElementById('fileName').innerHTML = 'Uploading ' + fileName + '...';
     217      document.getElementById('percent').innerHTML = '&nbsp;' + percentDone+'%';
     218      var doneElement = document.getElementById('percentDone');
     219      doneElement.style.width = percentDone+'%';
     220      var remainElement = document.getElementById('percentRemain');
     221      remainElement.style.width = (100-percentDone)+'%';
     222      if (percentDone == 100) Main.hide('percentRemain');
     223      document.getElementById('transferredBytes').innerHTML = transferredBytes;
     224      document.getElementById('totalBytes').innerHTML = totalBytes;
     225      document.getElementById('transferTime').innerHTML = transferTime;
     226      document.getElementById('transferRate').innerHTML = '(' + transferRate + ')';
     227      document.getElementById('remainTime').innerHTML = remainTime + ' (estimated)';
     228      document.getElementById('unpackMessage').innerHTML = unpackMessage.replace(/(.*):(.*)/, '<b>$1:</b> $2');
    201229    }
    202230   
     
    246274        return s;
    247275      }
    248      
    249     }
    250    
    251    
     276    }
    252277   
    253278    </script>
    254279  </base:head>
    255280  <base:body onload="initZipFormats()">
    256     <p>
    257    
     281  <p>
     282  <div id="selectForm">
    258283    <form action="upload.jsp?ID=<%=ID%>&directory_id=<%=directory.getId()%>"
    259284      method="post" name="file" enctype="multipart/form-data" onsubmit="return false;" target="upload">
     
    387412    </table>
    388413    </form>
     414  </div>
     415 
     416  <div id="progressForm" style="display:none;">
     417    <h3 class="docked" id="fileName">Preparing file upload...</h3>
     418    <div class="boxed" style="background: #E0E0E0">
     419      <br>
     420      <table width="90%" border=0 cellspacing=0 cellpadding=0 align="center">
     421      <tr>
     422      <td width="90%" >
     423        <table width="100%" class="progressbar" border=0 cellspacing=0 cellpadding=0>
     424        <tr>
     425          <td width="0%" class="done" id="percentDone">&nbsp;</td>
     426          <td width="100%" class="remain" id="percentRemain">&nbsp;</td>
     427        </tr>
     428        </table>
     429      </td>
     430        <td width="10%" id="percent">&nbsp;0%</td>
     431      </tr>
     432      <tr>
     433        <td colspan="2">
     434          <b>Transferred:</b> <span id="transferredBytes">0 bytes</span> of <span id="totalBytes">unknown</span><br>
     435          <b>Time:</b> <span id="transferTime">0s</span> <span id="transferRate"></span><br>
     436          <b>Remain:</b> <span id="remainTime">unknown</span><br>
     437          <span id="unpackMessage"></span>
     438        </td>
     439      </tr>
     440      </table>
     441      <br>
     442    </div>
     443    <br>
     444    <table align="center">
     445    <tr>
     446      <td><base:button image="cancel.gif" onclick="abortUpload()" title="Abort..." /></td>
     447    </tr>
     448    </table>
     449 
     450  </div>
    389451  </base:body>
    390452  </base:page>
  • branches/2.1.2/www/filemanager/upload/upload.jsp

    r3003 r3008  
    3131  @version 2.0
    3232--%>
    33 <%@ page
     33<%@ page session="false"
    3434  import="net.sf.basedb.core.SessionControl"
    3535  import="net.sf.basedb.core.DbControl"
     
    6868final int directoryId = Values.getInt(request.getParameter("directory_id"), SystemItems.getId(Directory.ROOT));
    6969final ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, null);
    70 
    7170String message = null;
     71String onload = "";
    7272DbControl dc = null;
    7373FileUpload upload = null;
     
    193193  }
    194194  dc.commit();
    195   dc.close();
    196195  message = unpackZip ? numUnpackedFiles + " file(s) unpacked successfully" : "File uploaded successfully";
     196  onload="done()";
    197197}
    198198catch (Throwable t)
    199199{
    200200  if (upload != null) upload.setException(t);
    201   throw t;
     201  if (t instanceof UploadAbortedException)
     202  {
     203    message = "The upload was aborted before it was completed.";
     204    onload="done()";
     205  }
     206  else
     207  {
     208    onload = "exception()";
     209  }
    202210}
    203211finally
     
    205213  if (dc != null) dc.close();
    206214}
    207 
    208215%>
    209 <base:page type="popup">
    210 <base:head>
     216<html>
     217<head>
    211218  <script language="JavaScript">
    212219  function done()
    213220  {
    214     window.top.location = getRoot() + 'common/close_popup.jsp?refresh_opener=1&message=<%=HTML.urlEncode(message)%>';
     221    window.top.location = '../../common/close_popup.jsp?refresh_opener=1&message=<%=HTML.urlEncode(message)%>';
     222  }
     223  function exception()
     224  {
     225    window.top.frames['select'].location = 'show_exception.jsp?ID=<%=ID%>';
    215226  }
    216227  </script>
    217 </base:head>
    218 <base:body onload="done()">
    219 </base:body>
    220 </base:page>
    221 
    222 
     228</head>
     229<body onload="<%=onload%>">
     230</body>
     231</html>
     232
     233
     234
Note: See TracChangeset for help on using the changeset viewer.