Project

General

Profile

« Previous | Next » 

Revision 3420

Added by walbridge almost 17 years ago

Refactored upload code to allow arbitrary POST parameter ordering, and open the door to multiple files in one HTTP request. Two new methods to MetaCatUtil to facilitate the file processing: writeTempFile and copyFile.

View differences:

src/edu/ucsb/nceas/metacat/MetaCatUtil.java
27 27
package edu.ucsb.nceas.metacat;
28 28

  
29 29
import java.io.File;
30
import java.io.FileInputStream;
30 31
import java.io.FileOutputStream;
31 32
import java.io.FileWriter;
33
import java.io.IOException;
32 34
import java.io.PrintWriter;
33 35
import java.io.StringReader;
34 36
import java.net.MalformedURLException;
......
42 44

  
43 45
import org.apache.log4j.Logger;
44 46

  
47
import com.oreilly.servlet.multipart.FilePart;
48

  
45 49
import edu.ucsb.nceas.dbadapter.AbstractDatabase;
46 50
import edu.ucsb.nceas.utilities.Options;
47 51

  
......
900 904
    	}
901 905
    	
902 906
    }
907

  
908
		/**
909
     * Write the uploaded file to disk for temporary storage before moving it 
910
     * to its final Metacat location.
911
     *
912
     * @param  filePart          the FilePart object containing the file form element
913
     * @param  fileName          the name of the file to be written to disk
914
     * @return tempFilePath      a String containing location of temporary file
915
     */
916
    public static String writeTempFile (FilePart filePart, String fileName) {
917
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
918
        String tempFilePath = null;
919
        String tempDirPath = getOption("temp-dir");
920
        long fileSize;
921
        File tempFile;
922
        File tempDir;
923

  
924
        if ((fileName == null) || fileName.equals("")) {
925
            return tempFilePath;
926
        }
927
				
928
        // the tempfilepath token isn't set, use Java default
929
        if (tempDirPath == null) {
930
						String javaTempDir = System.getProperty("java.io.tempdir");
931
						if (javaTempDir == null) {
932
							  // no paths set, use unix default
933
								tempDirPath = "/tmp";
934
						} else {
935
								tempDirPath = javaTempDir;
936
						}
937
        }
938

  
939
        tempDir = new File(tempDirPath);
940

  
941
        // Create the temporary directory if it doesn't exist
942
        try {
943
            if (!tempDir.exists()) {
944
                tempDir.mkdirs();
945
            }
946
        } catch (SecurityException e) {
947
            logMetacat.error("Can't create directory: " + tempDir.getPath() +
948
                             ". Error: " + e.getMessage());
949
        }
950
        try {
951
            tempFile = new File(tempDirPath, fileName);
952
            fileSize = filePart.writeTo(tempFile);
953
            tempFilePath = tempDirPath + File.separator + fileName;
954

  
955
            if (fileSize == 0) {
956
                logMetacat.error("Uploaded file '" + fileName + "'is empty!");
957
            }
958
        } catch (IOException e) {
959
            logMetacat.error("IO exception which writing temporary file: " +
960
                             tempFilePath + " " + e.getMessage());
961
        }
962

  
963
        logMetacat.info("Temporary file is: " + tempFilePath);
964

  
965
        return tempFilePath;
966
    }
967
		
968
		/**
969
		 *
970
		 * Copy a file between two locations specified as strings.  Fails
971
     * if either path cannot be created. Based on the public domain
972
     * FileCopy class in _Java in a Nutshell_.
973
     *
974
		 * @param sourceName		the source file to read from disk
975
     * @param destName  		the destination file on disk
976
     */
977
    public static void copyFile(String sourceName, String destName) throws IOException {
978
        Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
979

  
980
        File sourceFile = new File(sourceName);
981
        File destFile = new File(destName);
982
        FileInputStream source = null;
983
        FileOutputStream dest = null;
984
        byte[] buffer;
985
        int bytesRead;
986

  
987
        try {
988
            if (!sourceFile.exists() || !sourceFile.isFile()) {
989
                logMetacat.error("File copy: no such source" +
990
                                 " file: " + sourceName);
991
            }
992
            if (!sourceFile.canRead()) {
993
                logMetacat.error("File copy: source file " +
994
                                 "is unreadable: " + sourceName);
995
            }
996

  
997
            if (destFile.exists()) {
998
                if (destFile.isFile()) {
999
                    if (!destFile.canWrite()) {
1000
                        logMetacat.error("File copy: destination " +
1001
                                         "file is unwriteable: " + destFile);
1002
                    }
1003
                } else {
1004
                    logMetacat.error("File copy: destination file " +
1005
                                     "is not a file: " + destFile);
1006
                }
1007
            } else {
1008
                File parentDir = parent(destFile);
1009

  
1010
                if (!parentDir.exists())
1011
                {
1012
                    logMetacat.error("File copy: destination diretory " +
1013
                                     " doesn't exist: " + destName);
1014
                }
1015
                if (!parentDir.canWrite()) {
1016
                    logMetacat.error("File copy: destination directory " +
1017
                                     " is unwritable: " + destName);
1018
                }
1019
            }
1020

  
1021
            // Verbose error checking done, copy the file object
1022
            source = new FileInputStream(sourceFile);
1023
            dest = new FileOutputStream(destFile);
1024
            buffer = new byte[1024];
1025

  
1026
            while (true) {
1027
                bytesRead = source.read(buffer);
1028
                if (bytesRead == -1) {
1029
                    break;
1030
                }
1031
                dest.write(buffer, 0, bytesRead);
1032
            }
1033
        }
1034
        finally {
1035
            if (source != null) {
1036
                try { source.close(); } catch (IOException e) { ; }
1037
            }
1038
            if (dest != null) {
1039
                try { dest.close(); } catch (IOException e) { ; }
1040
            }
1041
        }
1042
    }
1043

  
1044
    private static File parent(File f) {
1045
        String dirname = f.getParent();
1046
        if (dirname == null) {
1047
            if (f.isAbsolute()) return new File(File.separator);
1048
            else return new File(System.getProperty("user.dir"));
1049
        }
1050
        return new File(dirname);
1051
    }
1052

  
903 1053
}
src/edu/ucsb/nceas/metacat/MetaCatServlet.java
2814 2814
            MultipartParser mp = new MultipartParser(request,
2815 2815
                    sizeLimit * 1024 * 1024);
2816 2816
            Part part;
2817

  
2817 2818
            while ((part = mp.readNextPart()) != null) {
2818 2819
                String name = part.getName();
2819 2820

  
......
2828 2829
                } else if (part.isFile()) {
2829 2830
                    // it's a file part
2830 2831
                    FilePart filePart = (FilePart) part;
2831
                    fileList.put(name, filePart);
2832

  
2833
                    // Stop once the first file part is found, otherwise going
2834
                    // onto the
2835
                    // next part prevents access to the file contents. So...for
2836
                    // upload
2837
                    // to work, the datafile must be the last part
2838
                    break;
2832
                    String fileName = filePart.getFileName();
2833
                    String fileTempLocation;
2834
                    
2835
                    // the filePart will be clobbered on the next loop, save to disk
2836
                    fileTempLocation = MetaCatUtil.writeTempFile(filePart, fileName);
2837
                    fileList.put(name, fileTempLocation);
2838
                    fileList.put("filename", fileName);
2839
                    fileList.put("name", fileTempLocation);
2840
                } else {
2841
                    logMetacat.info("Upload name '" + name + "' was empty.");
2839 2842
                }
2840 2843
            }
2841 2844
        } catch (IOException ioe) {
......
2939 2942
        if (docid != null && fileList.containsKey("datafile")) {
2940 2943
            logMetacat.info("Uploading data docid: " + docid);
2941 2944
            // Get a reference to the file part of the form
2942
            FilePart filePart = (FilePart) fileList.get("datafile");
2943
            String fileName = filePart.getFileName();
2945
            //FilePart filePart = (FilePart) fileList.get("datafile");
2946
            String fileName = (String) fileList.get("filename");
2944 2947
            logMetacat.info("Uploading filename: " + fileName);
2945 2948
            // Check if the right file existed in the uploaded data
2946 2949
            if (fileName != null) {
......
2955 2958
                        File dataDirectory = new File(datafilepath);
2956 2959
                        dataDirectory.mkdirs();
2957 2960
                        File newFile = null;
2961
                        File tempFile = null;
2962
                        String tempFileName = (String) fileList.get("name");
2963
                        String newFileName = dataDirectory + File.separator + docid;
2958 2964
                        long size = 0;
2959 2965
                        boolean fileExists = false;
2960
                        try
2961
                        {
2962
                          newFile = new File(dataDirectory, docid);
2963
                          fileExists = newFile.exists();
2964
                          if ( fileExists == false ) {
2965
                            size = filePart.writeTo(newFile); 
2966
                          }
2967
                        
2968
//                        register the file in the database (which generates
2969
                          // an exception
2970
                          //if the docid is not acceptable or other untoward
2971
                          // things happen
2972
                          DocumentImpl.registerDocument(fileName, "BIN", docid,
2966

  
2967
                        try {
2968
                            newFile = new File(newFileName);
2969
                            fileExists = newFile.exists();
2970
                            logMetacat.error("new file status is: " + fileExists);
2971
                            if ( fileExists == false ) {
2972
                                // copy file to desired output location
2973
                                try {
2974
                                    MetaCatUtil.copyFile(tempFileName, newFileName);
2975
                                } catch (IOException ioe) {
2976
                                    logMetacat.error("IO Exception copying file: " +
2977
                                                      ioe.getMessage());
2978
                                }
2979
                                size = newFile.length();
2980
                                if (size == 0) {
2981
                                    throw new IOException("Uploaded file is 0 bytes!");
2982
                                }
2983
                            }
2984
                            logMetacat.info("Uploading the following to Metacat:" +
2985
                                            fileName + ", " + docid + ", " + 
2986
                                            username + ", " + groupnames);
2987
                            //register the file in the database (which generates
2988
                            // an exception
2989
                            //if the docid is not acceptable or other untoward
2990
                            // things happen
2991
                            DocumentImpl.registerDocument(fileName, "BIN", docid,
2973 2992
                                username, groupnames);
2974 2993
                        }
2975 2994
                        catch (Exception ee)
......
2980 2999
                            if ( fileExists == false ) {
2981 3000
                              newFile.delete();
2982 3001
                            }
2983
                            
2984
                            throw ee;
3002
                            logMetacat.info("in Exception: fileExists is " + fileExists); 
3003
                            //throw ee;
3004
                            logMetacat.error("Upload Error: " + ee.getMessage());
2985 3005
                        }
2986 3006

  
2987 3007
                        EventLog.getInstance().log(request.getRemoteAddr(),

Also available in: Unified diff