merge media InputStream changes from MIR_1_0 branch
[mir.git] / source / mircoders / media / MediaHandlerGeneric.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package  mircoders.media;
33
34 import java.util.*;
35 import java.io.*;
36
37 import freemarker.template.SimpleList;
38
39 import mir.media.*;
40 import mir.entity.*;
41 import mir.misc.*;
42 import mir.storage.*;
43
44
45 /**
46  * This is the Generic MediaHandler. It stores the media data on
47  * the filesystem and keeps basic metadata  (size, type...) in the
48  * DB. Usually only representation needs to be overridden.
49  * See the MediaHandlerAudio class to see an example of how one
50  * could override it.
51  * <p>
52  * Most media handlers should override this class.
53  * <p>
54  * In theory, it could be used to handle miscellaneous media that
55  * we don't have entered in the media_type table, (like RTF documents,
56  * PS, PDF, etc..)
57  * <p>
58  * Of course it implements the MirMedia interface.
59  *
60  * @see mir.media.MirMedia
61  * @author mh <mh@nadir.org>
62  * @version $Id: MediaHandlerGeneric.java,v 1.10 2002/11/04 04:35:22 mh Exp $
63  */
64
65 public class MediaHandlerGeneric implements MirMedia
66 {
67     protected static String imageHost = MirConfig.getProp("Producer.Image.Host");
68     protected static String imageRoot = MirConfig.getProp("Producer.ImageRoot");
69     protected static Logfile theLog = Logfile.getInstance(
70                                                    MirConfig.getProp("Home")+
71                                                   "log/media.log");
72     private final String sepChar = File.separator;
73     
74     public void set (InputStream in, Entity ent, Entity mediaTypeEnt )
75         throws MirMediaException {
76
77         String ext = mediaTypeEnt.getValue("name");
78         String mediaFname = ent.getId()+"."+ext;
79         String date = ent.getValue("date");
80         String datePath = StringUtil.webdbDate2path(date);
81         try {
82             long size = FileUtil.write(getStoragePath()+sepChar+datePath+
83                                       sepChar+mediaFname, in);
84             ent.setValueForProperty("publish_path",datePath+sepChar+mediaFname);
85             ent.setValueForProperty("size", new Long(size).toString());
86             ent.update();
87         } catch (Exception e) {
88             theLog.printError(e.toString());
89             throw new MirMediaException(e.toString());
90         }
91
92     }
93
94     public void produce (Entity ent, Entity mediaTypeEnt )
95       throws MirMediaException {
96
97       //check first if the media file exist since produced
98       //location is also the storage location
99       String date = ent.getValue("date");
100       String datePath = StringUtil.webdbDate2path(date);
101       String relPath = datePath+ent.getId()+"."+mediaTypeEnt.getValue("name");
102       String fname = getStoragePath()+relPath;
103       if(! new File(fname).exists())
104         throw new MirMediaException("error in MirMedia.produce(): "+relPath+
105                                     " does not exist!");
106     }
107
108     public InputStream getMedia (Entity ent, Entity mediaTypeEnt)
109       throws MirMediaException {
110       String publishPath = mediaTypeEnt.getValue("publish_path");
111       String fname = getStoragePath()+publishPath;
112       File f = new File(fname);
113       if(! f.exists())
114         throw new MirMediaException("error in MirMedia.getMedia(): "+fname+
115                                     "does not exist!");
116       FileInputStream in;
117       try {
118         in = new FileInputStream(f);
119       } catch (IOException e) {
120         throw new MirMediaException("getMedia(): "+e.toString());
121       }
122       return in;
123     }
124
125     public InputStream getIcon (Entity ent) {
126         return null;
127     }
128
129     public String getStoragePath()
130     {
131         return MirConfig.getProp("Producer.Media.Path");
132     }
133
134     public String getIconStoragePath()
135     {
136         return MirConfig.getProp("Producer.Image.IconPath");
137     }
138
139     public String getPublishHost()
140     {
141         return MirConfig.getProp("Producer.Media.Host");
142     }
143
144     public String getTinyIconName()
145     {
146         return MirConfig.getProp("Producer.Icon.TinyText");
147     }
148
149     public String getBigIconName()
150     {
151         return MirConfig.getProp("Producer.Icon.BigText");
152     }
153
154     public String getIconAltName()
155     {
156         return "Generic media";
157     }
158
159     public SimpleList getURL(Entity ent, Entity mediaTypeEnt)
160     {
161       SimpleList theList = new SimpleList();
162       theList.add(ent);
163       return theList;
164     }
165
166     public boolean isVideo()
167     {
168       return false;
169     }
170
171     public boolean isAudio()
172     {
173       return false;
174     }
175
176     public boolean isImage()
177     {
178       return false;
179     }
180
181     public String getDescr( Entity mediaType)
182     {
183       return mediaType.getValue("mime_type");
184     }
185
186 }
187
188
189