]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/load/MediaHelper.java
Version 14, October 2012
[GpsPrune.git] / tim / prune / load / MediaHelper.java
1 package tim.prune.load;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.net.URL;
7 import java.util.zip.ZipEntry;
8 import java.util.zip.ZipFile;
9
10 import tim.prune.data.AudioClip;
11 import tim.prune.data.MediaObject;
12 import tim.prune.data.Photo;
13
14 /**
15  * Class to provide helper functions for loading media
16  */
17 public abstract class MediaHelper
18 {
19         /** File filters */
20         private static GenericFileFilter _jpegFilter = null, _audioFilter = null;
21
22
23         /**
24          * Construct a MediaObject for the given path
25          * @param inZipFile path to archive file (if any)
26          * @param inPath path to media file
27          * @param inSourceFile file from which data was loaded
28          * @return either Photo or AudioClip object as appropriate, or null
29          */
30         public static MediaObject createMediaObject(File inZipFile, String inPath, File inSourceFile)
31         {
32                 if (inPath == null || inPath.length() < 5) return null;
33                 InputStream is = null;
34                 ZipFile zf     = null;
35                 byte[] data    = null;
36                 String url     = null;
37                 try
38                 {
39                         // Check if path is a URL, in which case get an input stream from it
40                         if (inPath.substring(0, 5).toLowerCase().equals("http:"))
41                         {
42                                 url = inPath;
43                                 is = new URL(inPath).openStream();
44                                 data = ByteScooper.scoop(is);
45                         }
46                 }
47                 catch (IOException ioe) {
48                         System.err.println("Got ioe from url: " + ioe.getMessage());
49                 } // is stays null
50
51                 // Now see if file is in the zip file
52                 if (is == null && inZipFile != null && inZipFile.exists() && inZipFile.canRead())
53                 {
54                         try
55                         {
56                                 zf = new ZipFile(inZipFile);
57                                 ZipEntry entry = zf.getEntry(inPath);
58                                 if (entry != null && entry.getSize() > 0)
59                                 {
60                                         data = ByteScooper.scoop(zf.getInputStream(entry));
61                                         // System.out.println("Size of data " + (data.length == entry.getSize()?"matches":"DOESN'T match"));
62                                 }
63                         }
64                         catch (IOException ioe) {
65                                 System.err.println("Got ioe from zip file: " + ioe.getMessage());
66                         }
67                 }
68                 // Clean up input streams
69                 if (is != null) try {
70                         is.close();
71                 } catch (IOException ioe) {}
72                 if (zf != null) try {
73                         zf.close();
74                 } catch (IOException ioe) {}
75
76                 if (data != null)
77                 {
78                         // Create Photo or AudioClip using this entry
79                         String filename = new File(inPath).getName();
80                         initFilters();
81                         if (_jpegFilter.acceptFilename(inPath)) {
82                                 return new Photo(data, filename, url);
83                         }
84                         else if (_audioFilter.acceptFilename(inPath)) {
85                                 return new AudioClip(data, filename, url);
86                         }
87                         return null;
88                 }
89
90                 // If we haven't got a result by now, try to load plain file
91                 File file = new File(inPath);
92                 if (inSourceFile != null && !file.isAbsolute()) {
93                         file = new File(inSourceFile.getParent(), inPath);
94                 }
95                 // awkward construction because new File(startPath, absolutePath) doesn't work
96                 return createMediaObject(file);
97         }
98
99         /**
100          * Construct a MediaObject for the given file
101          * @param inFile file to load
102          * @return either Photo or AudioClip object as appropriate, or null
103          */
104         private static MediaObject createMediaObject(File inFile)
105         {
106                 if (inFile == null) {return null;}
107                 if (!inFile.exists() || !inFile.canRead() || !inFile.isFile()) {return null;}
108                 initFilters();
109                 // Check if filename looks like a jpeg
110                 if (_jpegFilter.acceptFilename(inFile.getName())) {
111                         return JpegLoader.createPhoto(inFile);
112                 }
113                 // Check if filename looks like an audio clip
114                 if (_audioFilter.acceptFilename(inFile.getName())) {
115                         return new AudioClip(inFile);
116                 }
117                 // Neither photo nor audio
118                 return null;
119         }
120
121         /**
122          * Initialise filters if necessary
123          */
124         private static void initFilters()
125         {
126                 if (_jpegFilter == null) {
127                         _jpegFilter = new JpegFileFilter();
128                         _audioFilter = new AudioFileFilter();
129                 }
130         }
131 }