use of org.olat.core.commons.modules.bc.meta.MetaInfo in project openolat by klemens.
the class FileHandler method getThumbnail.
@Override
public VFSLeaf getThumbnail(MediaLight media, Size size) {
String storagePath = media.getStoragePath();
VFSLeaf thumbnail = null;
if (StringHelper.containsNonWhitespace(storagePath)) {
VFSContainer storageContainer = fileStorage.getMediaContainer(media);
VFSItem item = storageContainer.resolve(media.getRootFilename());
if (item instanceof VFSLeaf) {
VFSLeaf leaf = (VFSLeaf) item;
if (leaf instanceof MetaTagged) {
MetaInfo metaInfo = ((MetaTagged) leaf).getMetaInfo();
thumbnail = metaInfo.getThumbnail(size.getHeight(), size.getWidth(), true);
}
}
}
return thumbnail;
}
use of org.olat.core.commons.modules.bc.meta.MetaInfo in project openolat by klemens.
the class ImageHandler method getThumbnail.
@Override
public VFSLeaf getThumbnail(MediaLight media, Size size) {
String storagePath = media.getStoragePath();
VFSLeaf thumbnail = null;
if (StringHelper.containsNonWhitespace(storagePath)) {
VFSContainer storageContainer = fileStorage.getMediaContainer(media);
VFSItem item = storageContainer.resolve(media.getRootFilename());
if (item instanceof VFSLeaf) {
VFSLeaf leaf = (VFSLeaf) item;
if (leaf instanceof MetaTagged) {
MetaInfo metaInfo = ((MetaTagged) leaf).getMetaInfo();
thumbnail = metaInfo.getThumbnail(size.getHeight(), size.getWidth(), true);
}
}
}
return thumbnail;
}
use of org.olat.core.commons.modules.bc.meta.MetaInfo in project openolat by klemens.
the class CourseResourceFolderWebService method attachFileToCourseFolder.
private Response attachFileToCourseFolder(Long courseId, List<PathSegment> path, String filename, InputStream file, HttpServletRequest request) {
if (!isAuthor(request)) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
ICourse course = CoursesWebService.loadCourse(courseId);
if (course == null) {
return Response.serverError().status(Status.NOT_FOUND).build();
}
VFSContainer container = course.getCourseFolderContainer();
for (PathSegment segment : path) {
VFSItem item = container.resolve(segment.getPath());
if (item instanceof VFSContainer) {
container = (VFSContainer) item;
} else if (item == null) {
// create the folder
container = container.createChildContainer(segment.getPath());
}
}
VFSItem newFile;
UserRequest ureq = RestSecurityHelper.getUserRequest(request);
if (container.resolve(filename) != null) {
VFSItem existingVFSItem = container.resolve(filename);
if (existingVFSItem instanceof VFSContainer) {
// already exists
return Response.ok().build();
}
// check if it's locked
boolean locked = CoreSpringFactory.getImpl(VFSLockManager.class).isLockedForMe(existingVFSItem, ureq.getIdentity(), ureq.getUserSession().getRoles());
if (locked) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
if (existingVFSItem instanceof Versionable && ((Versionable) existingVFSItem).getVersions().isVersioned()) {
Versionable existingVersionableItem = (Versionable) existingVFSItem;
boolean ok = existingVersionableItem.getVersions().addVersion(ureq.getIdentity(), "REST upload", file);
if (ok) {
log.audit("");
}
newFile = (VFSLeaf) existingVersionableItem;
} else {
existingVFSItem.delete();
newFile = container.createChildLeaf(filename);
OutputStream out = ((VFSLeaf) newFile).getOutputStream(false);
FileUtils.copy(file, out);
FileUtils.closeSafely(out);
FileUtils.closeSafely(file);
}
} else if (file != null) {
newFile = container.createChildLeaf(filename);
OutputStream out = ((VFSLeaf) newFile).getOutputStream(false);
FileUtils.copy(file, out);
FileUtils.closeSafely(out);
FileUtils.closeSafely(file);
} else {
newFile = container.createChildContainer(filename);
}
if (newFile instanceof MetaTagged && ((MetaTagged) newFile).getMetaInfo() != null) {
MetaInfo infos = ((MetaTagged) newFile).getMetaInfo();
infos.setAuthor(ureq.getIdentity());
infos.write();
}
return Response.ok().build();
}
use of org.olat.core.commons.modules.bc.meta.MetaInfo in project openolat by klemens.
the class CmdServeResource method execute.
public Controller execute(FolderComponent folderComponent, UserRequest ureq, WindowControl wControl, Translator translator) {
VFSSecurityCallback inheritedSecCallback = VFSManager.findInheritedSecurityCallback(folderComponent.getCurrentContainer());
if (inheritedSecCallback != null && !inheritedSecCallback.canRead())
throw new RuntimeException("Illegal read attempt: " + folderComponent.getCurrentContainerPath());
// extract file
String path = ureq.getModuleURI();
MediaResource mr = null;
VFSItem vfsitem = folderComponent.getRootContainer().resolve(path);
if (vfsitem == null) {
// double decoding of ++
vfsitem = FolderCommandHelper.tryDoubleDecoding(ureq, folderComponent);
}
if (vfsitem == null) {
mr = new NotFoundMediaResource();
} else if (!(vfsitem instanceof VFSLeaf)) {
mr = new NotFoundMediaResource();
} else {
VFSLeaf vfsfile = (VFSLeaf) vfsitem;
boolean forceDownload = FolderManager.isDownloadForcedFileType(vfsfile.getName());
if (path.toLowerCase().endsWith(".html") || path.toLowerCase().endsWith(".htm")) {
// setCurrentURI(path);
// set the http content-type and the encoding
// try to load in iso-8859-1
InputStream is = vfsfile.getInputStream();
if (is == null) {
mr = new NotFoundMediaResource();
} else {
String page = FileUtils.load(is, DEFAULT_ENCODING);
// search for the <meta content="text/html; charset=utf-8"
// http-equiv="Content-Type" /> tag
// if none found, assume iso-8859-1
String enc = DEFAULT_ENCODING;
boolean useLoaded = false;
// <meta.*charset=([^"]*)"
Matcher m = PATTERN_ENCTYPE.matcher(page);
boolean found = m.find();
if (found) {
String htmlcharset = m.group(1);
enc = htmlcharset;
if (htmlcharset.equals(DEFAULT_ENCODING)) {
useLoaded = true;
}
} else {
useLoaded = true;
}
// set the new encoding to remember for any following .js file loads
g_encoding = enc;
if (useLoaded) {
StringMediaResource smr = new StringMediaResource();
String mimetype = forceDownload ? VFSMediaResource.MIME_TYPE_FORCE_DOWNLOAD : "text/html;charset=" + enc;
smr.setContentType(mimetype);
smr.setEncoding(enc);
smr.setData(page);
if (forceDownload) {
smr.setDownloadable(true, vfsfile.getName());
}
mr = smr;
} else {
// found a new charset other than iso-8859-1 -> let it load again
// as bytes (so we do not need to convert to string and back
// again)
VFSMediaResource vmr = new VFSMediaResource(vfsfile);
vmr.setEncoding(enc);
if (forceDownload) {
vmr.setDownloadable(true);
}
mr = vmr;
}
}
} else if (path.endsWith(".js")) {
// a javascript library
VFSMediaResource vmr = new VFSMediaResource(vfsfile);
// that loads the .js file
if (g_encoding != null) {
vmr.setEncoding(g_encoding);
}
if (forceDownload) {
vmr.setDownloadable(true);
}
mr = vmr;
} else if (path.endsWith(".txt")) {
// text files created in OpenOLAT are utf-8, prefer this encoding
VFSMediaResource vmr = new VFSMediaResource(vfsfile);
vmr.setEncoding("utf-8");
if (forceDownload) {
vmr.setDownloadable(true);
}
mr = vmr;
} else {
// binary data: not .html, not .htm, not .js -> treated as is
VFSMediaResource vmr = new VFSMediaResource(vfsfile);
if (forceDownload) {
vmr.setDownloadable(true);
}
mr = vmr;
}
}
ThreadLocalUserActivityLogger.log(FolderLoggingAction.BC_FILE_READ, getClass(), CoreLoggingResourceable.wrapBCFile(path));
ureq.getDispatchResult().setResultingMediaResource(mr);
// update download counter
if (vfsitem instanceof MetaTagged) {
MetaTagged itemWithMeta = (MetaTagged) vfsitem;
MetaInfo meta = itemWithMeta.getMetaInfo();
meta.increaseDownloadCount();
meta.write();
}
return null;
}
use of org.olat.core.commons.modules.bc.meta.MetaInfo in project openolat by klemens.
the class CmdServeThumbnailResource method execute.
@Override
public Controller execute(FolderComponent folderComponent, UserRequest ureq, WindowControl wControl, Translator translator) {
VFSSecurityCallback inheritedSecCallback = VFSManager.findInheritedSecurityCallback(folderComponent.getCurrentContainer());
if (inheritedSecCallback != null && !inheritedSecCallback.canRead())
throw new RuntimeException("Illegal read attempt: " + folderComponent.getCurrentContainerPath());
// extract file
String path = ureq.getModuleURI();
MediaResource mr = null;
VFSLeaf vfsfile = (VFSLeaf) folderComponent.getRootContainer().resolve(path);
if (vfsfile == null) {
// double decoding of ++
vfsfile = (VFSLeaf) FolderCommandHelper.tryDoubleDecoding(ureq, folderComponent);
}
if (vfsfile instanceof MetaTagged) {
MetaInfo info = ((MetaTagged) vfsfile).getMetaInfo();
if (info != null && info.isThumbnailAvailable()) {
VFSLeaf thumbnail = info.getThumbnail(200, 200, false);
if (thumbnail != null) {
mr = new VFSMediaResource(thumbnail);
}
}
}
if (mr == null) {
mr = new NotFoundMediaResource();
}
ureq.getDispatchResult().setResultingMediaResource(mr);
return null;
}
Aggregations