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