public Entry postMedia()

in app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/MediaCollection.java [86:180]


    public Entry postMedia(AtomRequest areq, Entry entry) throws AtomException {
        log.debug("Entering");
        String[] pathInfo = StringUtils.split(areq.getPathInfo(),"/");

        try {
            // get incoming slug from HTTP header
            String slug = areq.getHeader("Slug");

            Content content = entry.getContents().get(0); 
            String contentType = content.getType();
            InputStream is = areq.getInputStream();
            String title = entry.getTitle() != null ? entry.getTitle() : slug;
            
            // authenticated client posted a weblog entry
            File tempFile = null;
            String handle = pathInfo[0];
            MediaFileManager fileMgr = roller.getMediaFileManager();
            Weblog website = WebloggerFactory.getWeblogger().getWeblogManager().getWeblogByHandle(handle);
            if (!RollerAtomHandler.canEdit(user, website)) {
                throw new AtomNotAuthorizedException("Not authorized to edit weblog: " + handle);
            }
            if (pathInfo.length > 1) {
                // Save to temp file
                String fileName = createFileName(website, 
                    (slug != null) ? slug : Utilities.replaceNonAlphanumeric(title,' '), contentType);
                try {
                    tempFile = File.createTempFile(fileName, "tmp");
                    FileOutputStream fos = new FileOutputStream(tempFile);
                    Utilities.copyInputToOutput(is, fos);
                    fos.close();
                                        
                    // Parse pathinfo to determine file path
                    String path = filePathFromPathInfo(pathInfo);
                    String justPath = path;
                    int lastSlash = path.lastIndexOf('/');
                    if (lastSlash > -1) {
                        justPath = path.substring(lastSlash);
                    }

                    MediaFileDirectory mdir =
                        fileMgr.getMediaFileDirectoryByName(website, justPath);

                    if (mdir.hasMediaFile(fileName)) {
                        throw new AtomException("Duplicate file name");
                    }

                    FileInputStream fis = new FileInputStream(tempFile);

                    MediaFile mf = new MediaFile();
                    mf.setDirectory(mdir);
                    mf.setWeblog(website);
                    mf.setName(fileName);
                    mf.setOriginalPath(justPath);
                    mf.setContentType(contentType);
                    mf.setInputStream(fis);
                    mf.setLength(tempFile.length());

                    RollerMessages errors = new RollerMessages();
                    fileMgr.createMediaFile(website, mf, errors);
                    if (errors.getErrorCount() > 0) {
                        throw new AtomException(errors.toString());
                    }

                    roller.flush();
                    
                    fis.close();
                                      
                    MediaFile stored = fileMgr.getMediaFile(mf.getId());
                    Entry mediaEntry = createAtomResourceEntry(website, stored);
                    for (Object objLink : mediaEntry.getOtherLinks()) {
                        Link link = (Link) objLink;
                        if ("edit".equals(link.getRel())) {
                            log.debug("Exiting");
                            return mediaEntry;
                        }
                    }
                    log.error("ERROR: no edit link found in saved media entry");
                    
                } catch (FileIOException fie) {
                    throw new AtomException(
                        "File upload disabled, over-quota or other error", fie);
                } finally {
                    if (tempFile != null) {
                        tempFile.delete();
                    }
                }
            }
            throw new AtomException("Error saving media entry");
        
        } catch (WebloggerException re) {
            throw new AtomException("Posting media", re);
        } catch (IOException ioe) {
            throw new AtomException("Posting media", ioe);
        }
    }