Changeset 5758


Ignore:
Timestamp:
Sep 23, 2011, 9:05:23 AM (10 years ago)
Author:
Nicklas Nordborg
Message:

Fixes #978: Unzipped files never inherit file type specified during upload

Added a parameter for specifying the source file to FileUnpacker.unpack(). The source file is always populated with the options from the form so the actual implementations can copy those values to the unpacked files.

Location:
trunk
Files:
1 added
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/doc/src/docbook/developer/migrate_2_3.xml

    r5738 r5758  
    160160      <listitem>
    161161        <para>
     162          Changes to the <interfacename docapi="net.sf.basedb.util.zip">FileUnpacker</interfacename>
     163          interface. The <methodname>unpack()</methodname> method signature has changed and
     164          any plug-ins that implement this interface need to be updated. The changes
     165          make it possible to get information about the main zip/tar file that is being
     166          unpacked. See <ulink
     167          url="http://base.thep.lu.se/ticket/978">ticket #978 (Unzipped files never inherit
     168          file type specified during upload)</ulink> for more information.
     169        </para>
     170      </listitem>
     171     
     172      <listitem>
     173        <para>
    162174          TODO -- more will probably be added to this list
    163175        </para>
  • trunk/src/clients/web/net/sf/basedb/clients/web/servlet/Upload.java

    r5643 r5758  
    182182            throw new WebException("popup", "File already exists", "Another file with the same name already exists: {1}", uploadedFile.getFilename());
    183183          }
    184          
    185           f.setName(uploadedFile.getFilename());
    186           String serverMimeType = getServletContext().getMimeType(uploadedFile.getFilename());
    187           String browserMimeType = uploadedFile.getMimeType();
    188           f.setMimeTypeAuto(serverMimeType != null ? serverMimeType : browserMimeType, null);
    189           if (subtypeId >= 0) // < 0 = denied or unchanged
    190           {
    191             ItemSubtype subtype = subtypeId == 0 ? null : ItemSubtype.getById(dc, subtypeId);
    192             f.setItemSubtype(subtype);
    193             if (subtype != null) cc.setRecent(subtype, maxRecent);
    194           }
    195           f.setDescription(description);
    196           f.setCharacterSet(characterSet);
    197           toFile = f.getUploadStream(checkMd5, compress);
    198           f.setWriteProtected(writeProtected);
    199         }
     184        }
     185       
     186        if (f == null)
     187        {
     188          // Create a dummy file that is not saved to the database
     189          // We use this so that options from the form can be sent
     190          // to the unpacker.unpack() method
     191          f = File.getNew(dc, directory);
     192        }
     193
     194        // Set various properties from the form on the file object
     195        f.setName(uploadedFile.getFilename());
     196        String serverMimeType = getServletContext().getMimeType(uploadedFile.getFilename());
     197        String browserMimeType = uploadedFile.getMimeType();
     198        f.setMimeTypeAuto(serverMimeType != null ? serverMimeType : browserMimeType, null);
     199        if (subtypeId >= 0) // < 0 = denied or unchanged
     200        {
     201          ItemSubtype subtype = subtypeId == 0 ? null : ItemSubtype.getById(dc, subtypeId);
     202          f.setItemSubtype(subtype);
     203          if (subtype != null) cc.setRecent(subtype, maxRecent);
     204        }
     205        f.setDescription(description);
     206        f.setCharacterSet(characterSet);
     207        if (!f.isDetached()) toFile = f.getUploadStream(checkMd5, compress);
     208        f.setWriteProtected(writeProtected);
    200209       
    201210        // Load plug-in for for unpacking (must implement FileUnpacker)
     
    230239          SimpleAbsoluteProgressReporter unpackProgress = new SimpleAbsoluteProgressReporter(null, fileSize);
    231240          sc.setSessionSetting("UnpackProgress", unpackProgress);
    232           numUnpackedFiles = unpacker.unpack(dc, directory, uploadStream, overwriteZip, unpackProgress);
     241          numUnpackedFiles = unpacker.unpack(dc, directory, uploadStream, f, overwriteZip, unpackProgress);
    233242        }
    234243        uploadStream.close();
  • trunk/src/core/net/sf/basedb/util/zip/AbstractFileUnpacker.java

    r5481 r5758  
    139139      permissions.add(new Permissions(Item.FILE, null, EnumSet.of(Permission.CREATE, Permission.WRITE)));
    140140      permissions.add(new Permissions(Item.DIRECTORY, null, EnumSet.of(Permission.CREATE, Permission.WRITE)));
     141      permissions.add(new Permissions(Item.ITEMSUBTYPE, EnumSet.of(Permission.USE), null));
    141142    }
    142143    return permissions;
     
    159160     
    160161      // Unpack the file
    161       int numFiles = unpack(dc, dir, in, overwrite, absoluteProgress);
     162      int numFiles = unpack(dc, dir, in, zipFile, overwrite, absoluteProgress);
    162163      dc.commit();
    163164     
  • trunk/src/core/net/sf/basedb/util/zip/FileUnpacker.java

    r4515 r5758  
    3030import net.sf.basedb.core.DbControl;
    3131import net.sf.basedb.core.Directory;
     32import net.sf.basedb.core.File;
    3233import net.sf.basedb.core.plugin.Plugin;
    3334
     
    7475 
    7576  /**
    76     Unpack the given input stream into a BASE directory.
     77    Unpack the given input stream into a BASE directory. If a source
     78    file is given it can be used as a template for setting certain
     79    properties on the unpcked files. See {@link
     80    PackUtil#copyProperties(File, File)}.
    7781   
    7882    @param dc The DbControl to use for database access
    7983    @param dir The directory to unpack the files to
    8084    @param in The input stream containing the packed data
     85    @param sourceFile The source packed file that the input stream is
     86      coming from or null if not known
    8187    @param overwrite If existing files should be overwritten or ignored
    8288    @param progress A optional progress reporter which expects the number
     
    8591    @throws IOException If there is an error reading the input stream
    8692    @throws BaseException If there is another error
     93    @since 3.0
    8794  */
    88   public int unpack(DbControl dc, Directory dir, InputStream in, boolean overwrite,
     95  public int unpack(DbControl dc, Directory dir, InputStream in, File sourceFile, boolean overwrite,
    8996    AbsoluteProgressReporter progress)
    9097    throws IOException, BaseException;
  • trunk/src/core/net/sf/basedb/util/zip/ZipUnpacker.java

    r4515 r5758  
    8080    @throws IOException If there is an error when unpacking the zip file
    8181    @throws BaseException If there is a BASE error
     82    @since 3.0
    8283  */
    83   public int unpackZipFile(Directory rootDir, ZipInputStream zip, boolean overwrite,
     84  public int unpackZipFile(Directory rootDir, ZipInputStream zip, File sourceFile, boolean overwrite,
    8485    AbsoluteProgressReporter progress)
    8586    throws IOException, BaseException
     
    119120          file.setMimeTypeAuto(null, null);
    120121          OutputStream upload = file.getUploadStream(false);
     122          PackUtil.copyProperties(sourceFile, file);
    121123          try
    122124          {
  • trunk/src/plugins/core/net/sf/basedb/plugins/TarFileUnpacker.java

    r5612 r5758  
    5151import net.sf.basedb.util.Values;
    5252import net.sf.basedb.util.zip.AbstractFileUnpacker;
     53import net.sf.basedb.util.zip.PackUtil;
    5354import net.sf.basedb.util.zip.TarUtil;
    5455
     
    9798    return mimeTypes;
    9899  }
     100  @Override
    99101  public int unpack(DbControl dc, Directory rootDir, InputStream in,
    100     boolean overwrite, AbsoluteProgressReporter progress)
     102    File sourceFile, boolean overwrite, AbsoluteProgressReporter progress)
    101103    throws IOException
    102104  {
     
    161163          file.setMimeTypeAuto(null, null);
    162164          OutputStream upload = file.getUploadStream(false);
     165          PackUtil.copyProperties(sourceFile, file);
    163166          try
    164167          {
  • trunk/src/plugins/core/net/sf/basedb/plugins/ZipFileUnpacker.java

    r5612 r5758  
    3232import net.sf.basedb.core.DbControl;
    3333import net.sf.basedb.core.Directory;
     34import net.sf.basedb.core.File;
    3435import net.sf.basedb.util.zip.AbstractFileUnpacker;
    3536import net.sf.basedb.util.zip.ZipUnpacker;
     
    7778    Use the {@link ZipUnpacker} to unpack the file.
    7879  */
     80  @Override
    7981  public int unpack(DbControl dc, Directory dir, InputStream in,
    80     boolean overwrite, AbsoluteProgressReporter progress)
     82    File sourceFile, boolean overwrite, AbsoluteProgressReporter progress)
    8183    throws IOException
    8284  {
    8385    ZipUnpacker unpacker = new ZipUnpacker(dc);
    8486    ZipInputStream zipStream = new ZipInputStream(in);
    85     return unpacker.unpackZipFile(dir, zipStream, overwrite, progress);
     87    return unpacker.unpackZipFile(dir, zipStream, sourceFile, overwrite, progress);
    8688  }
    8789  // -------------------------------------------
  • trunk/src/test/TestFile.java

    r5630 r5758  
    387387      AbsoluteProgressReporter progress = TestUtil.getSilent() ?
    388388        null : new SimpleAbsoluteProgressReporter(new ConsoleProgressReporter(true), fileToUpload.length());
    389       unpacker.unpackZipFile(dir, zipFile, true, progress);
     389      unpacker.unpackZipFile(dir, zipFile, keepZip, true, progress);
    390390      if (progress != null) progress.append("\n");
    391391
Note: See TracChangeset for help on using the changeset viewer.