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