source/mir/media/MirMedia.java
[mir.git] / source / mircoders / media / MediaHandlerMp3.java
1 /*
2  * put your module comment here
3  */
4
5
6 package  mircoders.media;
7
8 import java.util.*;
9
10 import freemarker.template.SimpleList;
11 import freemarker.template.SimpleHash;
12
13 import mir.media.*;
14 import mir.entity.*;
15 import mir.misc.*;
16 import mir.storage.*;
17
18
19 /**
20  * Please note: this media handler produces 
21  * 3 media files, the raw .mp3, a .m3u which is 
22  * contains the URL for the mp3 and a .pls which
23  * contains the URL to the mp3 in shoutcast playlist
24  * format. What's important is that the web server (of 
25  * the media host) must recognize the .m3u and .pls file 
26  * extensions and send the proper "audio/x-mpegurl"
27  * and "audio/x-scpls" mime-types respectively.
28  * If the web server is apache, it's easy, just 
29  * add:
30  *
31  * audio/x-mpegurl                 m3u
32  * audio/x-scpl                    pls
33  *
34  * to the file pointed to by the "TypesConfig" 
35  * command in your apache config file. Or add 
36  * and equivalent AddType command to your httpd.conf.
37  * Of course this assumes that the mod_mime is loaded.
38  *
39  * If the web server is not apache, then your on your own.
40  * 
41  * @see mir.media.MirMedia
42  * @author mh <heckmann@hbe.ca>
43  * @version 01.12.2001
44  */
45
46 public class MediaHandlerMp3 extends MediaHandlerAudio implements MirMedia
47 {
48
49     public void produce (Entity ent, Entity mediaTypeEnt )
50       throws MirMediaException {
51       
52       // first check if the file exists
53       super.produce(ent, mediaTypeEnt);
54
55       String baseName = ent.getId();
56       String date = ent.getValue("date");
57       String datePath = StringUtil.webdbDate2path(date);
58       String mp3Pointer = getPublishHost()+ent.getValue("publish_path");
59       String mpegURLFile = baseName+".m3u"; 
60       String playlistFile = baseName+".pls"; 
61
62       try {
63         //write the "meta" files
64         //first the .m3u since it only contains one line
65         FileUtil.write(getStoragePath()+"/"+datePath+"/"+mpegURLFile,
66                       mp3Pointer.getBytes());
67         //now the .pls file
68         FileUtil.write(getStoragePath()+"/"+datePath+"/"+playlistFile,
69                       mp3Pointer.getBytes());
70       } catch (Exception e) {
71           theLog.printError(e.toString()); 
72           throw new MirMediaException(e.toString());
73       }
74     }
75
76   public SimpleList getURL(Entity ent, Entity mediaTypeEnt)
77   {
78     SimpleList theList = new SimpleList();
79
80     //String stringSize = ent.getValue("size");
81     //int size = Integer.parseInt(stringSize, 10)/1024;
82     theList.add(ent);
83    
84     String basePath=StringUtil.regexpReplace(ent.getValue("publish_path"),
85                                             ".mp3$","");
86
87     // @todo the texts ("title") below urgently need to be sanely localizaeble
88     // somehow
89     SimpleHash m3uHash = new SimpleHash();
90     m3uHash.put("publish_path", basePath+".m3u");
91     m3uHash.put("publish_server", ent.getValue("publish_server"));
92     m3uHash.put("title", "stream URL");
93     theList.add(m3uHash);
94
95     SimpleHash plsHash = new SimpleHash();
96     plsHash.put("publish_path", basePath+".pls");
97     plsHash.put("publish_server", ent.getValue("publish_server"));
98     plsHash.put("title", "playlist URL");
99     theList.add(plsHash);
100
101     return theList;
102
103   }
104
105   public String getDescr()
106   {
107     return "Mp3 audio";
108   }
109
110 }
111         
112         
113