Changeset 4867


Ignore:
Timestamp:
Mar 31, 2009, 1:23:30 PM (13 years ago)
Author:
Nicklas Nordborg
Message:

Fixes #1270: Online editing of text files

Location:
trunk
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/util/FileUtil.java

    r4794 r4867  
    4949import java.io.OutputStream;
    5050import java.io.PushbackInputStream;
     51import java.io.Reader;
     52import java.io.Writer;
    5153import java.util.Arrays;
    5254import java.util.HashMap;
     
    114116  }
    115117
     118  /**
     119    Copy from the reader to the writer until end of file is reached.
     120    @param in The <code>Reader</code> to read from
     121    @param out The <code>Writer</code> to write to
     122    @return The number of characters copied
     123    @throws IOException This exception is thrown if there is an error
     124    @since 2.12
     125  */
     126  public static long copy(Reader in, Writer out)
     127    throws IOException
     128  {
     129    return copy(in, out, null);
     130  }
     131 
     132  /**
     133    Copy from the reader to the writer until end of file is reached.
     134    @param in The <code>Reader</code> to read from
     135    @param out The <code>Writer</code> to write to
     136    @param progress An optional progress reporter
     137    @return The number of characters copied
     138    @throws IOException This exception is thrown if there is an error
     139    @since 2.12
     140  */
     141  public static long copy(Reader in, Writer out, AbsoluteProgressReporter progress)
     142    throws IOException
     143  {
     144    int numRead = 0;
     145    long totalRead = 0;
     146    char[] buffer = new char[BUFFER_SIZE];
     147 
     148    while (numRead != -1) // -1 = end of stream
     149    {
     150      ThreadSignalHandler.checkInterrupted();
     151      numRead = in.read(buffer, 0, buffer.length);
     152      if (numRead > 0)
     153      {
     154        totalRead += numRead;
     155        out.write(buffer, 0, numRead);
     156        if (progress != null) progress.displayAbsolute(totalRead, null);
     157      }
     158    }
     159    return totalRead;
     160  }
    116161 
    117162  /**
  • trunk/www/filemanager/files/ajax.jsp

    r4522 r4867  
    2828  import="net.sf.basedb.core.File"
    2929  import="net.sf.basedb.util.Values"
     30  import="net.sf.basedb.util.FileUtil"
    3031  import="net.sf.basedb.clients.web.Base"
    3132  import="net.sf.basedb.clients.web.WebException"
     33  import="java.io.OutputStreamWriter"
     34  import="java.io.Writer"
     35  import="java.io.Reader"
    3236%>
    3337<%
     
    4953    dc.close();
    5054  }
     55  else if ("StoreFile".equals(cmd))
     56  {
     57    try
     58    {
     59      dc = sc.newDbControl();
     60      File f = File.getById(dc, itemId);
     61      Reader in = request.getReader();
     62      String charset = f.getCharacterSet();
     63      if (charset == null)
     64      {
     65        charset = Config.getCharset();
     66        f.setCharacterSet(charset);
     67      }
     68      Writer toFile = new OutputStreamWriter(
     69          f.getUploadStream(false, f.isCompressed()), charset);
     70      FileUtil.copy(in, toFile);
     71      toFile.flush();
     72      toFile.close();
     73      dc.commit();
     74      out.println("File saved successfully");
     75    }
     76    catch (Exception ex)
     77    {
     78      out.println("Failed: " + ex.getMessage());
     79      ex.printStackTrace();
     80    }
     81  }
    5182  else
    5283  {
     
    5889  if (dc != null) dc.close();
    5990}
    60 out.close();
    6191%>
  • trunk/www/filemanager/files/index.jsp

    r4587 r4867  
    498498    redirect = viewPage;
    499499  }
     500  else if ("EditFileData".equals(cmd))
     501  {
     502    // Display the edit page for a single item (should be opened in a popup)
     503    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
     504    redirect = "edit_file_data.jsp?ID=" + ID;
     505  }
    500506  else
    501507  {
  • trunk/www/filemanager/files/view_file.jsp

    r4865 r4867  
    8686  final boolean deletePermanentlyPermission = deletePermission && !isUsed;
    8787  final boolean isOwner = file.isOwner();
     88  final String mimeType = file.getMimeType();
     89  final boolean editable = writePermission && !file.isWriteProtected() &&
     90    mimeType != null &&  mimeType.startsWith("text/") &&
     91    file.getSize() < 102400 && file.getLocation() == Location.PRIMARY;
     92
    8893  Formatter<Date> dateTimeFormatter = FormatterFactory.getDateTimeFormatter(sc);
    8994  %>
     
    149154      location.replace('index.jsp?ID=<%=ID%>&cmd=DecompressItem&item_id=<%=itemId%>');
    150155    }
     156    function editFileData()
     157    {
     158      Main.openPopup('index.jsp?ID=<%=ID%>&cmd=EditFileData&item_id=<%=itemId%>', 'EditFileData', 900, 700);
     159    }
    151160    </script>
    152161  </base:head>
     
    160169        title="Edit&hellip;"
    161170        tooltip="<%=writePermission ? "Edit this file" : "You do not have permission to edit this file"%>"
     171      />
     172      <tbl:button
     173        visible="<%=editable%>"
     174        image="file_edit.png"
     175        onclick="editFileData()"
     176        title="Edit file&hellip;"
     177        tooltip="Edit the contents of this file"
    162178      />
    163179      <tbl:button
Note: See TracChangeset for help on using the changeset viewer.