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;
}
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();
}
}
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);
}
}
Aggregations