]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/load/MediaHelper.java
c55fc557ec6ee7bc5b58df0e19879b35935362ba
[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 = (inSourceFile == null ? new File(inPath) : new File(inSourceFile.getParent(), inPath));
92                 return createMediaObject(file);
93         }
94
95         /**
96          * Construct a MediaObject for the given file
97          * @param inFile file to load
98          * @return either Photo or AudioClip object as appropriate, or null
99          */
100         private static MediaObject createMediaObject(File inFile)
101         {
102                 if (inFile == null) {return null;}
103                 if (!inFile.exists() || !inFile.canRead() || !inFile.isFile()) {return null;}
104                 initFilters();
105                 // Check if filename looks like a jpeg
106                 if (_jpegFilter.acceptFilename(inFile.getName())) {
107                         return JpegLoader.createPhoto(inFile);
108                 }
109                 // Check if filename looks like an audio clip
110                 if (_audioFilter.acceptFilename(inFile.getName())) {
111                         return new AudioClip(inFile);
112                 }
113                 // Neither photo nor audio
114                 return null;
115         }
116
117         /**
118          * Initialise filters if necessary
119          */
120         private static void initFilters()
121         {
122                 if (_jpegFilter == null) {
123                         _jpegFilter = new JpegFileFilter();
124                         _audioFilter = new AudioFileFilter();
125                 }
126         }
127 }