use of org.olat.core.commons.services.image.Size in project openolat by klemens.
the class RepositoryManager method setImage.
public boolean setImage(VFSLeaf newImageFile, RepositoryEntry re) {
VFSLeaf currentImage = getImage(re);
if (currentImage != null) {
if (currentImage instanceof MetaTagged) {
MetaInfo info = ((MetaTagged) currentImage).getMetaInfo();
if (info != null) {
info.clearThumbnails();
}
}
currentImage.delete();
}
if (newImageFile == null || !newImageFile.exists() || newImageFile.getSize() <= 0) {
return false;
}
String targetExtension = ".png";
String extension = FileUtils.getFileSuffix(newImageFile.getName());
if ("jpg".equalsIgnoreCase(extension) || "jpeg".equalsIgnoreCase(extension)) {
targetExtension = ".jpg";
}
VFSContainer repositoryHome = new LocalFolderImpl(new File(FolderConfig.getCanonicalRepositoryHome()));
VFSLeaf repoImage = repositoryHome.createChildLeaf(re.getResourceableId() + targetExtension);
if (targetExtension.equals(".png") || targetExtension.equals(".jpg")) {
Size newImageSize = imageHelper.getSize(newImageFile, extension);
if (newImageSize != null && newImageSize.getWidth() <= PICTURE_WIDTH && newImageSize.getHeight() <= PICTURE_HEIGHT) {
boolean success = VFSManager.copyContent(newImageFile, repoImage);
return success;
}
}
Size size = imageHelper.scaleImage(newImageFile, repoImage, PICTURE_WIDTH, PICTURE_WIDTH, false);
return size != null;
}
use of org.olat.core.commons.services.image.Size in project openolat by klemens.
the class CatalogManager method setImage.
public boolean setImage(VFSLeaf newImageFile, CatalogEntryRef re) {
VFSLeaf currentImage = getImage(re);
if (currentImage != null) {
if (currentImage instanceof MetaTagged) {
MetaInfo info = ((MetaTagged) currentImage).getMetaInfo();
if (info != null) {
info.clearThumbnails();
}
}
currentImage.delete();
}
String extension = FileUtils.getFileSuffix(newImageFile.getName());
if (StringHelper.containsNonWhitespace(extension)) {
extension = extension.toLowerCase();
}
boolean ok = false;
VFSContainer catalogResourceHome = getCatalogResourcesHome();
try {
if ("jpeg".equals(extension) || "jpg".equals(extension)) {
VFSLeaf repoImage = catalogResourceHome.createChildLeaf(re.getKey() + ".jpg");
ok = VFSManager.copyContent(newImageFile, repoImage);
} else if ("png".equals(extension)) {
VFSLeaf repoImage = catalogResourceHome.createChildLeaf(re.getKey() + ".png");
ok = VFSManager.copyContent(newImageFile, repoImage);
} else {
// scale to default and png
VFSLeaf repoImage = catalogResourceHome.createChildLeaf(re.getKey() + ".png");
Size size = imageHelper.scaleImage(newImageFile, repoImage, 570, 570, true);
ok = size != null;
}
} catch (Exception e) {
log.error("", e);
}
return ok;
}
use of org.olat.core.commons.services.image.Size in project openolat by klemens.
the class FeedMediaDispatcher method deliverFile.
/**
* Dispatch and deliver the requested file given in the path.
*
* @param request
* @param response
* @param feed
* @param path
*/
private void deliverFile(HttpServletRequest request, HttpServletResponse response, OLATResourceable feed, Path path) {
// OLAT-5243 related: deliverFile can last arbitrary long, which can cause the open db connection to timeout and cause errors,
// hence we need to do an intermediateCommit here
DBFactory.getInstance().intermediateCommit();
// Create the resource to be delivered
MediaResource resource = null;
FeedManager manager = FeedManager.getInstance();
if (path.isFeedType()) {
// Only create feed if modified. Send not modified response else.
Identity identity = getIdentity(path.getIdentityKey());
long sinceModifiedMillis = request.getDateHeader("If-Modified-Since");
Feed feedLight = manager.loadFeed(feed);
long lastModifiedMillis = -1;
if (feedLight != null) {
lastModifiedMillis = feedLight.getLastModified().getTime();
}
if (sinceModifiedMillis >= (lastModifiedMillis / 1000L) * 1000L) {
// Send not modified response
response.setDateHeader("last-modified", lastModifiedMillis);
try {
response.sendError(HttpServletResponse.SC_NOT_MODIFIED);
return;
} catch (IOException e) {
// Send not modified failed
log.error("Send not modified failed", e);
return;
}
} else {
resource = manager.createFeedFile(feed, identity, path.getCourseId(), path.getNodeId());
}
} else if (path.isItemType()) {
resource = manager.createItemMediaFile(feed, path.getItemId(), path.getItemFileName());
} else if (path.isIconType()) {
Size thumbnailSize = null;
String thumbnail = request.getParameter("thumbnail");
if (StringHelper.containsNonWhitespace(thumbnail)) {
thumbnailSize = Size.parseString(thumbnail);
}
VFSLeaf resourceFile = manager.createFeedMediaFile(feed, path.getIconFileName(), thumbnailSize);
if (resourceFile != null) {
resource = new VFSMediaResource(resourceFile);
}
}
// Eventually deliver the requested resource
ServletUtil.serveResource(request, response, resource);
}
use of org.olat.core.commons.services.image.Size in project openolat by klemens.
the class ItemFormController method fileUploaded.
/**
* Helper to process a uploaded file. Adjust the filename and the file
* suffix. Guess the width and height and fill the appropriate fields.
*/
private void fileUploaded() {
file.clearError();
if (file.isUploadSuccess()) {
String newFilename = file.getUploadFileName().toLowerCase().replaceAll(" ", "_");
file.setUploadFileName(newFilename);
VFSLeaf movie = new LocalFileImpl(file.getUploadFile());
if (newFilename.endsWith("mov") || newFilename.endsWith("m4v")) {
// when uploading a video from an iOS device.
if (movieService.isMP4(movie, newFilename)) {
newFilename = newFilename.substring(0, newFilename.length() - 3) + "mp4";
file.setUploadFileName(newFilename);
}
}
if (validateFilename(newFilename)) {
// try to detect width and height for movies, prefill for user if possible
Size size = movieService.getSize(movie, FileUtils.getFileSuffix(newFilename));
if (size != null) {
if (size.getWidth() > 1) {
widthEl.setValue(Integer.toString(size.getWidth()));
}
if (size.getHeight() > 1) {
heightEl.setValue(Integer.toString(size.getHeight()));
}
}
}
}
}
use of org.olat.core.commons.services.image.Size in project openolat by klemens.
the class OLATUpgrade_11_3_0 method processVideoResource.
private boolean processVideoResource(RepositoryEntry entry) {
try {
OLATResource videoResource = entry.getOlatResource();
if (!videoManager.hasMasterContainer(videoResource)) {
log.error("RepoEntry: " + entry.getKey() + " has no valid master container.");
// log error but return true to proceed
return true;
}
// update track files on file system
VFSContainer masterContainer = videoManager.getMasterContainer(videoResource);
if (videoManager.isMetadataFileValid(videoResource)) {
VideoMetadata metafromXML = videoManager.readVideoMetadataFile(videoResource);
for (Entry<String, String> track : metafromXML.getAllTracks().entrySet()) {
VFSItem item = masterContainer.resolve(track.getValue());
if (item != null && item instanceof VFSLeaf) {
String path = VideoManagerImpl.TRACK + track.getKey() + VideoManagerImpl.DOT + FilenameUtils.getExtension(track.getValue());
// check if modified track file already exists
if (masterContainer.resolve(path) == null) {
VFSLeaf target = masterContainer.createChildLeaf(path);
VFSManager.copyContent((VFSLeaf) item, target);
}
}
}
} else {
log.error("RepoEntry: " + entry.getKey() + " has no valid Video Metadata XML file.");
}
// create meta data entries on database
if (videoManager.hasVideoFile(videoResource)) {
File videoFile = videoManager.getVideoFile(videoResource);
String fileName = videoFile.getName();
long size = videoFile.length();
String format = FilenameUtils.getExtension(fileName);
if (videoManager.hasVideoMetadata(videoResource)) {
VideoMetaImpl entity = new VideoMetaImpl();
entity.setVideoResource(videoResource);
entity.setFormat(format);
entity.setCreationDate(new Date());
entity.setLastModified(new Date());
Size resolution = videoManager.getVideoResolutionFromOLATResource(videoResource);
entity.setHeight(resolution.getHeight());
entity.setWidth(resolution.getWidth());
entity.setSize(size);
entity.setLength(entry.getExpenditureOfWork());
dbInstance.getCurrentEntityManager().persist(entity);
}
} else {
log.error("RepoEntry: " + entry.getKey() + " has no valid resource.");
}
return true;
} catch (Exception e) {
log.error("Update Metadata failed", e);
return false;
}
}
Aggregations