Project

General

Profile

« Previous | Next » 

Revision 8026

use custom FileInputStream subclass to delete the temporary bagit zip when the inputstream is closed (after someone has downloaded the zip).

View differences:

src/edu/ucsb/nceas/metacat/util/DeleteOnCloseFileInputStream.java
1
package edu.ucsb.nceas.metacat.util;
2

  
3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.FileNotFoundException;
6
import java.io.IOException;
7

  
8
/**
9
 * Extension of FileInputSteam that deletes the sourcefile when the
10
 * InputStream is closed. Should typically be used with temporary files
11
 * when a more immediate deletion should be performed than is offered
12
 * by the File.deleteOnExit() method.
13
 * @author leinfelder
14
 *
15
 */
16
public class DeleteOnCloseFileInputStream extends FileInputStream {
17
	private File file;
18

  
19
	public DeleteOnCloseFileInputStream(String name) throws FileNotFoundException {
20
		this(new File(name));
21
	}
22

  
23
	public DeleteOnCloseFileInputStream(File file) throws FileNotFoundException {
24
		super(file);
25
		this.file = file;
26
	}
27
	/**
28
	 * Delete the file when the InputStream is closed
29
	 */
30
	public void close() throws IOException {
31
		try {
32
			super.close();
33
		} finally {
34
			if (file != null) {
35
				file.delete();
36
				file = null;
37
			}
38
		}
39
	}
40

  
41
}
0 42

  
src/edu/ucsb/nceas/metacat/dataone/MNodeService.java
132 132
import edu.ucsb.nceas.metacat.index.MetacatSolrIndex;
133 133
import edu.ucsb.nceas.metacat.properties.PropertyService;
134 134
import edu.ucsb.nceas.metacat.shared.MetacatUtilException;
135
import edu.ucsb.nceas.metacat.util.DeleteOnCloseFileInputStream;
135 136
import edu.ucsb.nceas.metacat.util.DocumentUtil;
136 137
import edu.ucsb.nceas.metacat.util.SystemUtil;
137 138
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
......
1731 1732
			File pidMappingFile = File.createTempFile("pid-mapping.", ".txt");
1732 1733
			IOUtils.write(pidMapping.toString(), new FileOutputStream(pidMappingFile));
1733 1734
			bag.addFileAsTag(pidMappingFile);
1734
	
1735
			tempFiles.add(pidMappingFile);
1736
			
1735 1737
			bag = bag.makeComplete();
1736 1738
			File bagFile = File.createTempFile("bag.", ".zip");
1737
			// TODO: delete more confidently
1738
			bagFile.deleteOnExit();
1739
			
1739 1740
			bag.setFile(bagFile);
1740 1741
			ZipWriter zipWriter = new ZipWriter(bagFactory);
1741 1742
			bag.write(zipWriter, bagFile);
1742 1743
			bagFile = bag.getFile();
1743
			bagInputStream = new FileInputStream(bagFile);
1744
			// use custom FIS that will delete the file when closed
1745
			bagInputStream = new DeleteOnCloseFileInputStream(bagFile);
1746
			// also mark for deletion on shutdown in case the stream is never closed
1747
			bagFile.deleteOnExit();
1744 1748
			
1745
			// clean up temp entry files
1749
			// clean up other temp files
1746 1750
			for (File tf: tempFiles) {
1747 1751
				tf.delete();
1748 1752
			}
......
1856 1860
		
1857 1861
		return resultInputStream;
1858 1862
		
1859
	}
1863
	}	
1860 1864
    
1861 1865
}

Also available in: Unified diff