organizing imports
[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.MirMediaException;\r
40 import mir.misc.FileUtil;\r
41 import mir.misc.StringUtil;\r
42 import freemarker.template.SimpleHash;\r
43 import freemarker.template.SimpleList;\r
44 \r
45 /**\r
46  * Please note: this media handler produces\r
47  * 3 media files, the raw .mp3, a .m3u which is\r
48  * contains the URL for the mp3 and a .pls which\r
49  * contains the URL to the mp3 in shoutcast playlist\r
50  * format. What's important is that the web server (of\r
51  * the media host) must recognize the .m3u and .pls file\r
52  * extensions and send the proper "audio/x-mpegurl"\r
53  * and "audio/x-scpls" mime-types respectively.\r
54  * If the web server is apache, it's easy, just\r
55  * add:\r
56  *\r
57  * audio/x-mpegurl                 m3u\r
58  * audio/x-scpl                    pls\r
59  *\r
60  * to the file pointed to by the "TypesConfig"\r
61  * command in your apache config file. Or add\r
62  * and equivalent AddType command to your httpd.conf.\r
63  * Of course this assumes that the mod_mime is loaded.\r
64  *\r
65  * If the web server is not apache, then your on your own.\r
66  *\r
67  * @see mir.media.MirMedia\r
68  * @author mh <mh@nadir.org>\r
69  * @version $Id: MediaHandlerMp3.java,v 1.12 2003/03/05 19:23:16 idfx Exp $\r
70  */\r
71 \r
72 public class MediaHandlerMp3 extends MediaHandlerAudio implements MirMedia\r
73 {\r
74   protected LoggerWrapper logger;\r
75 \r
76   public MediaHandlerMp3() {\r
77     logger = new LoggerWrapper("Media.Audio.Mp3");\r
78   }\r
79 \r
80   public void produce(Entity ent, Entity mediaTypeEnt) throws MirMediaException {\r
81 \r
82     // first check if the file exists\r
83     super.produce(ent, mediaTypeEnt);\r
84 \r
85     String baseName = ent.getId();\r
86     String date = ent.getValue("date");\r
87     String datePath = StringUtil.webdbDate2path(date);\r
88     String mp3Pointer = getPublishHost() + ent.getValue("publish_path");\r
89     String mpegURLFile = baseName + ".m3u";\r
90     String playlistFile = baseName + ".pls";\r
91 \r
92     try {\r
93       //write the "meta" files\r
94       //first the .m3u since it only contains one line\r
95       FileUtil.write(getStoragePath() + "/" + datePath + "/" + mpegURLFile,\r
96                      new StringReader(mp3Pointer), "US-ASCII");\r
97       //now the .pls file\r
98       FileUtil.write(getStoragePath() + "/" + datePath + "/" + playlistFile,\r
99                      new StringReader(mp3Pointer), "US-ASCII");\r
100     }\r
101     catch (Throwable e) {\r
102       logger.error("MediaHandlerMp3.produce: " + e.toString());\r
103       throw new MirMediaException(e.toString());\r
104     }\r
105   }\r
106 \r
107   public SimpleList getURL(Entity ent, Entity mediaTypeEnt) {\r
108     SimpleList theList = new SimpleList();\r
109 \r
110     //String stringSize = ent.getValue("size");\r
111     //int size = Integer.parseInt(stringSize, 10)/1024;\r
112     theList.add(ent);\r
113 \r
114     String basePath = StringUtil.regexpReplace(ent.getValue("publish_path"),\r
115                                                ".mp3$", "");\r
116 \r
117     // @todo the texts ("title") below urgently need to be sanely localizaeble\r
118     // somehow\r
119     SimpleHash m3uHash = new SimpleHash();\r
120     m3uHash.put("publish_path", basePath + ".m3u");\r
121     m3uHash.put("publish_server", ent.getValue("publish_server"));\r
122     m3uHash.put("title", "stream URL");\r
123     theList.add(m3uHash);\r
124 \r
125     SimpleHash plsHash = new SimpleHash();\r
126     plsHash.put("publish_path", basePath + ".pls");\r
127     plsHash.put("publish_server", ent.getValue("publish_server"));\r
128     plsHash.put("title", "playlist URL");\r
129     theList.add(plsHash);\r
130 \r
131     return theList;\r
132 \r
133   }\r
134 \r
135   public String getDescr(Entity mediaType) {\r
136     return "mp3";\r
137   }\r
138 }\r
139 \r
140 \r
141 \r