Project

General

Profile

« Previous | Next » 

Revision 8436

Change the BagIt file names to the pid+resource type. Create new Files instead of createTempFile to avoid the randomly generated numbers in the name.

View differences:

src/edu/ucsb/nceas/metacat/dataone/MNodeService.java
1844 1844
				// just the lone pid in this package
1845 1845
				packagePids.add(pid);
1846 1846
			}
1847
	
1847
			
1848
			//Create a temp directory in the default temp directory
1849
			String defaultTempDir = System.getProperty("java.io.tmpdir");
1850
			File tempDir = new File(defaultTempDir + "/" + System.nanoTime());
1851
			tempFiles.add(tempDir);
1852
			tempDir.mkdir();
1853
			
1848 1854
			// track the pid-to-file mapping
1849 1855
			StringBuffer pidMapping = new StringBuffer();
1856
			
1850 1857
			// loop through the package contents
1851 1858
			for (Identifier entryPid: packagePids) {
1859
				//Get the system metadata for each item
1852 1860
				SystemMetadata entrySysMeta = this.getSystemMetadata(session, entryPid);
1861
				
1862
				//Create the temp file extension and prefix
1853 1863
				String extension = ObjectFormatInfo.instance().getExtension(entrySysMeta.getFormatId().getValue());
1854
		        String prefix = entryPid.getValue();
1855
		        prefix = "entry";
1856
				File tempFile = File.createTempFile(prefix + "-", extension);
1864
				String objectFormatType = ObjectFormatCache.getInstance().getFormat(entrySysMeta.getFormatId()).getFormatType();
1865
				String fileName = entryPid.getValue().replaceAll("\\W+", "_") + "-" + objectFormatType;			
1866
				
1867
		        //Create a new file for this item and add to the list
1868
				File tempFile = new File(tempDir, fileName+extension);
1857 1869
				tempFiles.add(tempFile);
1858
				InputStream entryInputStream = this.get(session, entryPid);
1870
				
1871
				InputStream entryInputStream = this.get(session, entryPid);			
1859 1872
				IOUtils.copy(entryInputStream, new FileOutputStream(tempFile));
1860 1873
				bag.addFileToPayload(tempFile);
1861 1874
				pidMapping.append(entryPid.getValue() + "\t" + "data/" + tempFile.getName() + "\n");
1862 1875
			}
1863 1876
			
1864 1877
			//add the the pid to data file map
1865
			File pidMappingFile = File.createTempFile("pid-mapping-", ".txt");
1878
			File pidMappingFile = new File(tempDir, "pid-mapping.txt");
1866 1879
			IOUtils.write(pidMapping.toString(), new FileOutputStream(pidMappingFile));
1867 1880
			bag.addFileAsTag(pidMappingFile);
1868 1881
			tempFiles.add(pidMappingFile);
1869 1882
			
1870 1883
			bag = bag.makeComplete();
1871 1884
			
1872
			// TODO: consider using mangled-PID for filename
1873
			File bagFile = File.createTempFile("dataPackage-", ".zip");
1885
			///Now create the zip file
1886
			//Use the pid as the file name prefix, replacing illegal characters with a hyphen
1887
			String zipName = pid.getValue().replaceAll("\\W+", "_");
1874 1888
			
1889
			File bagFile = new File(tempDir, zipName+".zip");
1890
			
1875 1891
			bag.setFile(bagFile);
1876 1892
			ZipWriter zipWriter = new ZipWriter(bagFactory);
1877 1893
			bag.write(zipWriter, bagFile);
......
1880 1896
			bagInputStream = new DeleteOnCloseFileInputStream(bagFile);
1881 1897
			// also mark for deletion on shutdown in case the stream is never closed
1882 1898
			bagFile.deleteOnExit();
1899
			tempFiles.add(bagFile);
1883 1900
			
1884 1901
			// clean up other temp files
1885
			for (File tf: tempFiles) {
1902
			for(int i=tempFiles.size()-1; i>=0; i--){
1903
				File tf = new File(tempFiles.get(i).getPath());
1886 1904
				tf.delete();
1887 1905
			}
1906
			
1888 1907
		} catch (IOException e) {
1889 1908
			// report as service failure
1890 1909
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());

Also available in: Unified diff