coding style
[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 mir.media.*;
11 import mir.entity.*;
12 import mir.misc.*;
13 import mir.storage.*;
14
15
16 /**
17  * Please note: this media handler produces 
18  * 3 media files, the raw .mp3, a .m3u which is 
19  * contains the URL for the mp3 and a .pls which
20  * contains the URL to the mp3 in shoutcast playlist
21  * format. What's important is that the web server (of 
22  * the media host) must recognize the .m3u and .pls file 
23  * extensions and send the proper "audio/x-mpegurl"
24  * and "audio/x-scpls" mime-types respectively.
25  * If the web server is apache, it's easy, just 
26  * add:
27  *
28  * audio/x-mpegurl                 m3u
29  * audio/x-scpl                    pls
30  *
31  * to the file pointed to by the "TypesConfig" 
32  * command in your apache config file. Or add 
33  * and equivalent AddType command to your httpd.conf.
34  * Of course this assumes that the mod_mime is loaded.
35  *
36  * If the web server is not apache, then your on your own.
37  * 
38  * @see mir.media.MirMedia
39  * @author mh <heckmann@hbe.ca>
40  * @version 01.12.2001
41  */
42
43 public class MediaHandlerMp3 extends MediaHandlerAudio implements MirMedia
44 {
45
46     public boolean set (byte[] uploadedData, Entity ent, Entity mediaTypeEnt )
47         throws MirMediaException {
48
49         String ext = mediaTypeEnt.getValue("name");
50         String dir = MirConfig.getProp("Producer.Media.Path");
51         String mediaHost = MirConfig.getProp("Producer.Media.Host");
52         String date = ent.getValue("date");
53         String datePath = StringUtil.webdbDate2path(date);
54         String baseName = ent.getId();
55         String mediaFname = baseName+"."+ext;
56         String mp3Pointer = mediaHost+datePath+mediaFname;
57         String mpegURLFile = baseName+".m3u"; 
58         String playlistFile = baseName+".pls"; 
59         Integer size = new Integer(uploadedData.length);
60         try {
61             FileUtil.write(dir+"/"+datePath+"/"+mediaFname, uploadedData);
62             //FileUtil.write(dir+"/"+mediaFname, uploadedData);
63             //were done with the data, dereference.
64             uploadedData=null;
65             
66             //write the "meta" files
67             //first the .m3u since it only contains one line
68             FileUtil.write(dir+"/"+datePath+"/"+mpegURLFile,mp3Pointer.getBytes());
69             //now the .pls file
70             FileUtil.write(dir+"/"+datePath+"/"+playlistFile,mp3Pointer.getBytes());
71             ent.setValueForProperty("is_produced", "1");
72             ent.setValueForProperty("icon_is_produced", "1");
73             ent.setValueForProperty("publish_path",datePath+"/"+mediaFname);
74             //ent.setValueForProperty("publish_path", mediaFname);
75             ent.setValueForProperty("publish_server", mediaHost);
76             ent.setValueForProperty("size", size.toString());
77             ent.update();
78         } catch (Exception e) {
79             theLog.printError(e.toString());
80             throw new MirMediaException(e.toString());
81         }
82
83         return true;
84     }
85
86     public String getURL(Entity ent, Entity mediaTypeEnt)
87     {
88       String stringSize = ent.getValue("size");
89       if (stringSize == null)
90         return null;
91       int size = Integer.parseInt(stringSize, 10)/1024;
92       String rawTitle = ent.getValue("title")+
93         " - "+mediaTypeEnt.getValue("name")+" "+
94         size+" KB "+"download";
95       String m3uTitle = ent.getValue("title")+
96         " - "+mediaTypeEnt.getValue("name")+" "+
97         "streaming URL";
98       String plsTitle = ent.getValue("title")+
99         " - "+mediaTypeEnt.getValue("name")+" "+
100         "playlist URL";
101
102       String basePath=StringUtil.regexpReplace(ent.getValue("publish_path"), ".mp3$","");
103
104       String m3uURL = StringUtil.createURLLinks(ent.getValue("publish_server")+"/"+basePath+".m3u", m3uTitle, imageRoot, getBigIcon());
105       String plsURL = StringUtil.createURLLinks(ent.getValue("publish_server")+"/"+basePath+".pls", plsTitle, imageRoot, getBigIcon());
106       String rawURL = StringUtil.createURLLinks(ent.getValue("publish_server")+"/"+ent.getValue("publish_path"), rawTitle, imageRoot, getBigIcon());
107     
108       return m3uURL+"\n<p>"+plsURL+"\n<p>"+rawURL;
109
110     }
111
112 }
113         
114         
115