Search in sources :

Example 1 with MCRContentInputStream

use of org.mycore.datamodel.ifs.MCRContentInputStream in project mycore by MyCoRe-Org.

the class MCRFile method setContent.

/**
 * Sets the content of this file.
 *
 * @param source
 *            the content to be read
 * @return the MD5 checksum of the stored content
 */
public String setContent(MCRContent source) throws IOException {
    MCRContentInputStream cis = source.getContentInputStream();
    source.sendTo(fo);
    String md5 = cis.getMD5String();
    data.setAttribute("md5", md5);
    getRoot().saveAdditionalData();
    return md5;
}
Also used : MCRContentInputStream(org.mycore.datamodel.ifs.MCRContentInputStream)

Example 2 with MCRContentInputStream

use of org.mycore.datamodel.ifs.MCRContentInputStream in project mycore by MyCoRe-Org.

the class MCRIFS2Commands method fixFileEntry.

private static void fixFileEntry(File node, String content_store, String derivate_id, String storage_base, boolean check_only) {
    LOGGER.debug("fixFileEntry : name = {}", node.getName());
    String storageid = node.getAbsolutePath().substring(storage_base.length()).replace("\\", "/");
    LOGGER.debug("fixFileEntry : storageid = {}", storageid);
    String id = "";
    String md5_old = "";
    long size_old = 0;
    boolean foundEntry = false;
    MCRSession mcrSession = MCRSessionMgr.getCurrentSession();
    boolean transactionActive = mcrSession.isTransactionActive();
    if (!transactionActive) {
        mcrSession.beginTransaction();
    }
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    try {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<MCRFSNODES> query = cb.createQuery(MCRFSNODES.class);
        Root<MCRFSNODES> nodes = query.from(MCRFSNODES.class);
        try {
            MCRFSNODES fsNode = em.createQuery(query.where(cb.equal(nodes.get(MCRFSNODES_.owner), derivate_id), cb.equal(nodes.get(MCRFSNODES_.storeid), content_store), cb.equal(nodes.get(MCRFSNODES_.storageid), storageid), cb.equal(nodes.get(MCRFSNODES_.type), "F"))).getSingleResult();
            LOGGER.debug("Found file entry for {}", storageid);
            foundEntry = true;
            id = fsNode.getId();
            md5_old = fsNode.getMd5();
            size_old = fsNode.getSize();
            em.detach(fsNode);
        } catch (NoResultException e) {
            LOGGER.error("Can't find file entry for {}", storageid);
            if (check_only)
                return;
        } catch (NonUniqueResultException e) {
            LOGGER.error("Non unique file entry for {}", storageid);
            return;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    // check fctid, size and MD5 of the file
    String fctid = "";
    String md5 = "";
    try (MCRContentInputStream cis = new MCRContentInputStream(new FileInputStream(node))) {
        byte[] header = cis.getHeader();
        fctid = MCRFileContentTypeFactory.detectType(node.getName(), header).getID();
        ByteStreams.copy(cis, ByteStreams.nullOutputStream());
        md5 = cis.getMD5String();
    } catch (MCRException | IOException e1) {
        e1.printStackTrace();
        return;
    }
    long size = node.length();
    LOGGER.debug("size old : {} <--> size : {}", Long.toString(size_old), Long.toString(size));
    LOGGER.debug("MD5 old : {} <--> MD5 : {}", md5_old, md5);
    if (size_old == size && md5_old.equals(md5)) {
        return;
    }
    if (foundEntry && size_old != size) {
        LOGGER.warn("Wrong file size for {} : {} <-> {}", storageid, size_old, size);
    }
    if (foundEntry && !md5.equals(md5_old)) {
        LOGGER.warn("Wrong file md5 for {} : {} <-> {}", storageid, md5_old, md5);
    }
    if (check_only)
        return;
    // fix entry
    LOGGER.info("Fix entry for file {}", storageid);
    if (!foundEntry) {
        MCRFileMetadataManager fmmgr = MCRFileMetadataManager.instance();
        id = fmmgr.createNodeID();
    }
    String pid = null;
    try {
        pid = getParentID(node, derivate_id);
    } catch (NoResultException e1) {
        LOGGER.error("Can't find parent id of directory for file {}", storageid);
    } catch (NonUniqueResultException e1) {
        LOGGER.error("The directory entry for {} and {} is not unique!", derivate_id, node.getParentFile().getName());
        return;
    }
    try {
        MCRFSNODES mcrfsnodes = new MCRFSNODES();
        mcrfsnodes.setId(id);
        mcrfsnodes.setPid(pid);
        mcrfsnodes.setType("F");
        mcrfsnodes.setOwner(derivate_id);
        mcrfsnodes.setName(node.getName());
        mcrfsnodes.setSize(size);
        mcrfsnodes.setDate(new Date(node.lastModified()));
        mcrfsnodes.setStoreid(content_store);
        mcrfsnodes.setStorageid(storageid);
        mcrfsnodes.setFctid(fctid);
        mcrfsnodes.setMd5(md5);
        em.merge(mcrfsnodes);
        mcrSession.commitTransaction();
        LOGGER.debug("Entry {} fixed.", node.getName());
    } catch (PersistenceException pe) {
        mcrSession.rollbackTransaction();
        pe.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) NonUniqueResultException(javax.persistence.NonUniqueResultException) MCRException(org.mycore.common.MCRException) NoResultException(javax.persistence.NoResultException) IOException(java.io.IOException) MCRFSNODES(org.mycore.backend.hibernate.tables.MCRFSNODES) NoResultException(javax.persistence.NoResultException) MCRException(org.mycore.common.MCRException) NonUniqueResultException(javax.persistence.NonUniqueResultException) IOException(java.io.IOException) PersistenceException(javax.persistence.PersistenceException) HibernateException(org.hibernate.HibernateException) FileInputStream(java.io.FileInputStream) MCRFileMetadataManager(org.mycore.datamodel.ifs.MCRFileMetadataManager) Date(java.util.Date) EntityManager(javax.persistence.EntityManager) MCRSession(org.mycore.common.MCRSession) MCRContentInputStream(org.mycore.datamodel.ifs.MCRContentInputStream) PersistenceException(javax.persistence.PersistenceException)

Example 3 with MCRContentInputStream

use of org.mycore.datamodel.ifs.MCRContentInputStream in project mycore by MyCoRe-Org.

the class MCRFile method repairMetadata.

/**
 * Repairs additional metadata of this file and all its children
 */
@Override
void repairMetadata() throws IOException {
    data.setName("file");
    data.setAttribute("name", getName());
    data.removeChildren("file");
    data.removeChildren("directory");
    MCRContentInputStream cis = getContent().getContentInputStream();
    IOUtils.copy(cis, DEV_NULL);
    cis.close();
    String md5 = cis.getMD5String();
    if (!md5.equals(data.getAttributeValue("md5"))) {
        LOGGER.warn("Fixed MD5 of {} to {}", getPath(), md5);
        data.setAttribute("md5", md5);
    }
}
Also used : MCRContentInputStream(org.mycore.datamodel.ifs.MCRContentInputStream)

Aggregations

MCRContentInputStream (org.mycore.datamodel.ifs.MCRContentInputStream)3 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 Date (java.util.Date)1 EntityManager (javax.persistence.EntityManager)1 NoResultException (javax.persistence.NoResultException)1 NonUniqueResultException (javax.persistence.NonUniqueResultException)1 PersistenceException (javax.persistence.PersistenceException)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 HibernateException (org.hibernate.HibernateException)1 MCRFSNODES (org.mycore.backend.hibernate.tables.MCRFSNODES)1 MCRException (org.mycore.common.MCRException)1 MCRSession (org.mycore.common.MCRSession)1 MCRFileMetadataManager (org.mycore.datamodel.ifs.MCRFileMetadataManager)1