1 37 38 package org.jahia.blogs.actions; 39 40 import org.jahia.blogs.model.MediaObject; 41 42 import org.jahia.blogs.ServletResources; 43 44 import org.jahia.services.webdav.JahiaWebdavBaseService; 45 import org.jahia.services.webdav.DAVFileAccess; 46 47 import org.jahia.services.usermanager.JahiaUser; 48 49 import org.jahia.exceptions.JahiaException; 50 51 import org.apache.log4j.Logger; 52 53 import java.util.Hashtable ; 54 55 import java.io.File ; 56 import java.io.FileOutputStream ; 57 import java.io.IOException ; 58 59 65 public class NewMediaObjectAction extends AbstractAction { 66 67 public static final String WEBDAV_CONTEXT = "/jahia/webdav"; 68 69 static Logger log = Logger.getLogger(NewMediaObjectAction.class); 71 72 private String blogID; 73 private Hashtable struct; 74 75 76 public NewMediaObjectAction(String blogID, String userName, 77 String password, Hashtable struct) { 78 79 super.userName = userName; 80 super.password = password; 81 this.blogID = blogID; 82 this.struct = struct; 83 } 84 85 90 public Object execute() throws JahiaException { 91 92 super.init(); 94 95 final JahiaUser user = super.checkLogin(); 97 98 if (!super.changePage(Integer.parseInt(blogID)).checkWriteAccess(user)) { 100 throw new JahiaException( 101 "You do not have write access to Blog: "+blogID, 102 "You do not have write access to Page: "+blogID, 103 JahiaException.ACL_ERROR, 104 JahiaException.WARNING_SEVERITY); 105 } 106 107 String dir = "/users/" + user.getUsername() + "/public"; 108 109 final DAVFileAccess dav = JahiaWebdavBaseService.getInstance(). 110 getDAVFileAccess(jParams, jParams.getSite(), user, dir); 111 112 dav.beginTransaction(); 113 114 try { 115 File f = File.createTempFile("blogUpload", ".tmp"); 116 FileOutputStream fos = new FileOutputStream (f); 117 fos.write((byte[])struct.get(MediaObject.BITS)); 118 fos.flush(); 119 fos.close(); 120 121 log.debug("blogUpload TmpFile: "+f.getName()); 122 123 final String contentType = (String )struct.get(MediaObject.TYPE); 124 final String finalName = dav.uploadFile((String )struct.get( 125 MediaObject.NAME), f, contentType, false); 126 127 dav.commitTransaction(); 128 129 StringBuffer buffer = new StringBuffer (); 130 buffer.append(ServletResources.getCurrentRequest(). 131 getScheme()); 132 buffer.append("://"); 133 buffer.append(ServletResources.getCurrentRequest(). 134 getServerName()); 135 buffer.append(":"); 136 buffer.append(ServletResources.getCurrentRequest(). 137 getServerPort()); 138 buffer.append(WEBDAV_CONTEXT); 139 buffer.append(dir); 140 buffer.append("/"); 141 buffer.append(finalName); 142 143 Hashtable result = new Hashtable (1); 144 result.put(MediaObject.URL, buffer.toString()); 145 146 log.debug("Media Object URL is: "+result); 147 f.delete(); 148 return result; 149 150 } catch (IOException e) { 151 throw new JahiaException(e.getMessage(), e.getMessage(), 152 JahiaException.APPLICATION_ERROR, 153 JahiaException.ERROR_SEVERITY, e); 154 } 155 } 156 } 157 | Popular Tags |