Example usage for java.io File getFreeSpace

List of usage examples for java.io File getFreeSpace

Introduction

In this page you can find the example usage for java.io File getFreeSpace.

Prototype

public long getFreeSpace() 

Source Link

Document

Returns the number of unallocated bytes in the partition named by this abstract path name.

Usage

From source file:edu.ucsd.library.dams.api.DAMSAPIServlet.java

public Map fileCharacterize(String objid, String cmpid, String fileid, boolean overwrite, FileStore fs,
        TripleStore ts, TripleStore es, Map<String, String[]> params) throws TripleStoreException {

    String sourceFile = null;/*from   w w w .  j  ava 2s  . c  o  m*/
    boolean srcDelete = false;
    StatementIterator sit = null;
    try {

        // both objid and fileid are required
        if (objid == null || objid.trim().equals("")) {
            return error(SC_BAD_REQUEST, "Object identifier required", null);
        }
        if (fileid == null || fileid.trim().equals("")) {
            return error(SC_BAD_REQUEST, "File identifier required", null);
        }

        Identifier oid = createID(objid, null, null);
        Identifier fid = createID(objid, cmpid, fileid);
        Identifier parent = oid;
        Identifier cid = null;
        if (cmpid != null && !cmpid.equals("0")) {
            cid = createID(objid, cmpid, null);
            parent = cid;
        }

        Identifier hasFile = Identifier.publicURI(prNS + "hasFile");
        Identifier hasComp = Identifier.publicURI(prNS + "hasComponent");
        Identifier cmpType = Identifier.publicURI(prNS + "Component");
        Identifier rdfType = Identifier.publicURI(rdfNS + "type");
        Identifier damsFile = Identifier.publicURI(prNS + "File");

        if (ts != null && !overwrite) {
            sit = ts.listStatements(parent, hasFile, fid);
            if (sit.hasNext()) {
                return error(SC_FORBIDDEN,
                        "Characterization for file " + fid.getId() + " already exists. Please use PUT instead",
                        null);
            }
        }

        Map<String, String> m = null;
        if (getParamString(params, "formatName", null) != null
                && getParamString(params, "mimeType", null) != null
                && (getParamString(params, "crc32checksum", null) != null
                        || getParamString(params, "md5checksum", null) != null
                        || getParamString(params, "sha1checksum", null) != null)) {
            // if formatName, mimeType and at least one checksum are
            // specified, skip jhove processing
            m = new HashMap<String, String>();
            m.put("size", getParamString(params, "size", null));
            m.put("formatName", getParamString(params, "formatName", null));
            m.put("formatVersion", getParamString(params, "formatVersion", null));
            m.put("mimeType", getParamString(params, "mimeType", null));
            m.put("crc32checksum", getParamString(params, "crc32checksum", null));
            m.put("md5checksum", getParamString(params, "md5checksum", null));
            m.put("sha1checksum", getParamString(params, "sha1checksum", null));
            String duration = getParamString(params, "duration", null);
            if (duration != null && duration.length() > 0)
                m.put("duration", duration);
            String quality = getParamString(params, "quality", null);
            if (quality != null && quality.length() > 0)
                m.put("quality", quality);
        } else {
            // extract technical metadata using jhove
            File localFile = getParamFile(params, "local", null);

            if (fs instanceof LocalStore) {
                // use localstore version if possible
                sourceFile = fs.getPath(objid, cmpid, fileid);
                log.info("Jhove extraction, source=localStore: " + sourceFile);
            } else if (localFile != null) {
                // otherwise, use staged source file
                sourceFile = localFile.getAbsolutePath();
                log.info("Jhove extraction, source=local file: " + sourceFile);
            } else {
                // if file not available locally, copy to local disk
                String fileExt = fileid.indexOf(".") > 0 ? fileid.substring(fileid.indexOf(".")) : "";
                File srcFile = File.createTempFile("jhovetmp", fileExt);
                long fileSize = fs.length(objid, cmpid, fileid);

                // make sure file is not too large to copy locally...
                if (jhoveMaxSize != -1L && fileSize > jhoveMaxSize) {
                    return error(SC_REQUEST_ENTITY_TOO_LARGE,
                            "File is too large to retrieve for local processing" + " (maxSize=" + jhoveMaxSize
                                    + "): " + fid.getId(),
                            null);
                } else if (fileSize > srcFile.getFreeSpace()) {
                    return error(SC_REQUEST_ENTITY_TOO_LARGE,
                            "There is not enough disk space to create a temp" + " file for " + fid.getId(),
                            null);
                }
                srcDelete = true;
                FileOutputStream fos = new FileOutputStream(srcFile);
                fs.read(objid, cmpid, fileid, fos);
                fos.close();
                sourceFile = srcFile.getAbsolutePath();
                log.info("Jhove extraction, source=cached file: " + sourceFile);
            }

            m = jhoveExtraction(sourceFile);
        }

        // User submitted properties: use, dateCreated, sourceFilename, and
        // sourcePath
        String[] input = params.get("use");
        String use = input != null ? input[0] : null;
        input = params.get("dateCreated");
        String dateCreated = input != null ? input[0] : null;
        input = params.get("sourceFileName");
        String sourceFilename = input != null ? input[0] : null;
        input = params.get("sourcePath");
        String sourcePath = input != null ? input[0] : null;

        // Output is saved to the triplestore.
        if (ts != null && es != null) {
            if (overwrite) {
                // delete existing metadata
                fileDeleteMetadata(objid, cmpid, fileid, ts, true);
            } else {
                // check for file metadata and complain if it exists
                if (cmpid != null && !cmpid.equals("0")) {
                    sit = ts.listStatements(cid, hasFile, fid);
                } else {
                    sit = ts.listStatements(oid, hasFile, fid);
                }
                if (sit.hasNext()) {
                    return error(SC_FORBIDDEN, "Characterization for file " + fid.getId()
                            + " already exists. Please use PUT instead", null);
                }
            }

            // Add/Replace properties submitted from user

            // Required use property
            if (use == null || use.length() == 0) {
                use = getFileUse(fileid);
            }
            m.put("use", use);

            if (dateCreated != null && dateCreated.length() > 0) {
                m.put("dateCreated", dateCreated);
            }
            if (sourceFilename != null && sourceFilename.length() > 0) {
                m.put("sourceFileName", sourceFilename);
            }
            if (sourcePath != null && sourcePath.length() > 0) {
                m.put("sourcePath", sourcePath);
            }

            // Add constant properties

            String compositionLevel = m.get("compositionLevel");
            if (compositionLevel == null) {
                compositionLevel = getDefaultCompositionLevel(sourceFilename);
            }
            m.put("preservationLevel", "full");
            m.put("objectCategory", "file");
            // # of decoding/extraction steps to get usable file
            m.put("compositionLevel", compositionLevel);

            String key = null;
            String value = null;
            for (Iterator it = m.keySet().iterator(); it.hasNext();) {
                key = (String) it.next();
                if (!key.equals("status")) {
                    value = m.get(key);
                    if (value != null && value.length() > 0) {
                        ts.addLiteralStatement(fid, Identifier.publicURI(prNS + key), "\"" + value + "\"", oid);
                    }
                }
            }

            // link from object/component to file record
            if (cmpid != null && !cmpid.equals("0")) {
                // make sure something links to this component
                sit = ts.listStatements(null, hasComp, cid);
                if (!sit.hasNext()) {
                    ts.addStatement(oid, hasComp, cid, oid);
                    ts.addStatement(cid, rdfType, cmpType, oid);
                }
                ts.addStatement(cid, hasFile, fid, oid);
            } else {
                ts.addStatement(oid, hasFile, fid, oid);
            }
            ts.addStatement(fid, rdfType, damsFile, oid);

            // Create event when required with es
            if (es != null) {
                //indexQueue(objid,"modifyObject");
                createEvent(ts, es, fs, objid, cmpid, fileid, Event.CHECKSUM_CALCULATED, true, m.get("status"));
            }
            return status("Jhove extracted and saved successfully");
        } else {
            // Output is displayed but not saved to the triplestore.
            Map info = new HashMap();
            info.put("characterization", m);
            return info;
        }
    } catch (Exception ex) {
        log.error("Error Jhove extraction", ex);
        return error("Error Jhove extraction", ex);
    } finally {
        if (srcDelete) {
            new File(sourceFile).delete();
        }
        if (sit != null) {
            sit.close();
            sit = null;
        }
    }
}