exception + misc. cleanup
[mir.git] / source / mircoders / media / MediaHandlerMp3.java
1 /*\r
2  * Copyright (C) 2001, 2002  The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\r
6  * Mir is free software; you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation; either version 2 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * Mir is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with Mir; if not, write to the Free Software\r
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19  *\r
20  * In addition, as a special exception, The Mir-coders gives permission to link\r
21  * the code of this program with the com.oreilly.servlet library, any library\r
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
24  * the above that use the same license as the above), and distribute linked\r
25  * combinations including the two.  You must obey the GNU General Public\r
26  * License in all respects for all of the code used other than the above\r
27  * mentioned libraries.  If you modify this file, you may extend this exception\r
28  * to your version of the file, but you are not obligated to do so.  If you do\r
29  * not wish to do so, delete this exception statement from your version.\r
30  */\r
31 \r
32 package  mircoders.media;\r
33 \r
34 import java.io.StringReader;\r
35 \r
36 import mir.entity.Entity;\r
37 import mir.log.LoggerWrapper;\r
38 import mir.media.MirMedia;\r
39 import mir.media.MediaExc;\r
40 import mir.media.MediaFailure;\r
41 import mir.misc.FileUtil;\r
42 import mir.misc.StringUtil;\r
43 import freemarker.template.SimpleHash;\r
44 import freemarker.template.SimpleList;\r
45 \r
46 /**\r
47  * Please note: this media handler produces\r
48  * 3 media files, the raw .mp3, a .m3u which is\r
49  * contains the URL for the mp3 and a .pls which\r
50  * contains the URL to the mp3 in shoutcast playlist\r
51  * format. What's important is that the web server (of\r
52  * the media host) must recognize the .m3u and .pls file\r
53  * extensions and send the proper "audio/x-mpegurl"\r
54  * and "audio/x-scpls" mime-types respectively.\r
55  * If the web server is apache, it's easy, just\r
56  * add:\r
57  *\r
58  * audio/x-mpegurl                 m3u\r
59  * audio/x-scpl                    pls\r
60  *\r
61  * to the file pointed to by the "TypesConfig"\r
62  * command in your apache config file. Or add\r
63  * and equivalent AddType command to your httpd.conf.\r
64  * Of course this assumes that the mod_mime is loaded.\r
65  *\r
66  * If the web server is not apache, then your on your own.\r
67  *\r
68  * @see mir.media.MirMedia\r
69  * @author mh <mh@nadir.org>\r
70  * @version $Id: MediaHandlerMp3.java,v 1.13 2003/03/09 03:53:11 zapata Exp $\r
71  */\r
72 \r
73 public class MediaHandlerMp3 extends MediaHandlerAudio implements MirMedia\r
74 {\r
75   protected LoggerWrapper logger;\r
76 \r
77   public MediaHandlerMp3() {\r
78     logger = new LoggerWrapper("Media.Audio.Mp3");\r
79   }\r
80 \r
81   public void produce(Entity ent, Entity mediaTypeEnt) throws MediaExc, MediaFailure {\r
82 \r
83     // first check if the file exists\r
84     super.produce(ent, mediaTypeEnt);\r
85 \r
86     String baseName = ent.getId();\r
87     String date = ent.getValue("date");\r
88     String datePath = StringUtil.webdbDate2path(date);\r
89     String mp3Pointer = getPublishHost() + ent.getValue("publish_path");\r
90     String mpegURLFile = baseName + ".m3u";\r
91     String playlistFile = baseName + ".pls";\r
92 \r
93     try {\r
94       //write the "meta" files\r
95       //first the .m3u since it only contains one line\r
96       FileUtil.write(getStoragePath() + "/" + datePath + "/" + mpegURLFile,\r
97                      new StringReader(mp3Pointer), "US-ASCII");\r
98       //now the .pls file\r
99       FileUtil.write(getStoragePath() + "/" + datePath + "/" + playlistFile,\r
100                      new StringReader(mp3Pointer), "US-ASCII");\r
101     }\r
102     catch (Throwable e) {\r
103       logger.error("MediaHandlerMp3.produce: " + e.toString());\r
104 \r
105       throw new MediaFailure(e);\r
106     }\r
107   }\r
108 \r
109   public SimpleList getURL(Entity ent, Entity mediaTypeEnt) {\r
110     SimpleList theList = new SimpleList();\r
111 \r
112     //String stringSize = ent.getValue("size");\r
113     //int size = Integer.parseInt(stringSize, 10)/1024;\r
114     theList.add(ent);\r
115 \r
116     String basePath = StringUtil.regexpReplace(ent.getValue("publish_path"),\r
117                                                ".mp3$", "");\r
118 \r
119     // @todo the texts ("title") below urgently need to be sanely localizaeble\r
120     // somehow\r
121     SimpleHash m3uHash = new SimpleHash();\r
122     m3uHash.put("publish_path", basePath + ".m3u");\r
123     m3uHash.put("publish_server", ent.getValue("publish_server"));\r
124     m3uHash.put("title", "stream URL");\r
125     theList.add(m3uHash);\r
126 \r
127     SimpleHash plsHash = new SimpleHash();\r
128     plsHash.put("publish_path", basePath + ".pls");\r
129     plsHash.put("publish_server", ent.getValue("publish_server"));\r
130     plsHash.put("title", "playlist URL");\r
131     theList.add(plsHash);\r
132 \r
133     return theList;\r
134 \r
135   }\r
136 \r
137   public String getDescr(Entity mediaType) {\r
138     return "mp3";\r
139   }\r
140 }\r
141 \r
142 \r
143 \r