use of org.olat.core.util.vfs.LocalImpl in project openolat by klemens.
the class FileUploadController method doUpload.
private void doUpload(UserRequest ureq) {
// check for available space
if (remainingQuotKB != -1 && (fileEl.getUploadFile().length() / 1024 > remainingQuotKB)) {
fileEl.setErrorKey("QuotaExceeded", null);
fileEl.getUploadFile().delete();
return;
}
String fileName = fileEl.getUploadFileName();
if (metaDataCtr != null && StringHelper.containsNonWhitespace(metaDataCtr.getFilename())) {
fileName = metaDataCtr.getFilename();
}
File uploadedFile = fileEl.getUploadFile();
if (resizeImg && fileName != null && imageExtPattern.matcher(fileName.toLowerCase()).find() && resizeEl.isSelected(0)) {
String extension = FileUtils.getFileSuffix(fileName);
File imageScaled = new File(uploadedFile.getParentFile(), "scaled_" + uploadedFile.getName() + "." + extension);
if (imageHelper.scaleImage(uploadedFile, extension, imageScaled, 1280, 1280, false) != null) {
// problem happen, special GIF's (see bug http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6358674)
// don't try to scale if not all ok
uploadedFile = imageScaled;
}
}
// check if such a filename does already exist
existingVFSItem = uploadVFSContainer.resolve(fileName);
if (existingVFSItem == null) {
uploadNewFile(ureq, uploadedFile, fileName);
} else {
// rename file and ask user what to do
if (!(existingVFSItem instanceof LocalImpl)) {
throw new AssertException("Can only LocalImpl VFS items, don't know what to do with file of type::" + existingVFSItem.getClass().getCanonicalName());
}
String renamedFilename = VFSManager.rename(uploadVFSContainer, existingVFSItem.getName());
newFile = uploadVFSContainer.createChildLeaf(renamedFilename);
// Copy content to tmp file
boolean success = false;
try (InputStream in = new FileInputStream(uploadedFile);
BufferedOutputStream out = new BufferedOutputStream(newFile.getOutputStream(false))) {
success = FileUtils.copy(in, out);
uploadedFile.delete();
} catch (IOException e) {
success = false;
}
if (success) {
boolean locked = vfsLockManager.isLockedForMe(existingVFSItem, getIdentity(), ureq.getUserSession().getRoles());
if (locked) {
// the file is locked and cannot be overwritten
lockedFileDialog(ureq, renamedFilename);
} else if (existingVFSItem instanceof Versionable && ((Versionable) existingVFSItem).getVersions().isVersioned()) {
uploadVersionedFile(ureq, renamedFilename);
} else {
askOverwriteOrRename(ureq, renamedFilename);
}
} else {
showError("failed");
status = FolderCommandStatus.STATUS_FAILED;
fireEvent(ureq, Event.FAILED_EVENT);
}
}
}
use of org.olat.core.util.vfs.LocalImpl in project openolat by klemens.
the class ZipUtil method zip.
// zip
public static boolean zip(List<VFSItem> vfsFiles, VFSLeaf target, boolean compress) {
boolean success = true;
String zname = target.getName();
if (target instanceof LocalImpl) {
zname = ((LocalImpl) target).getBasefile().getAbsolutePath();
}
OutputStream out = target.getOutputStream(false);
if (out == null) {
throw new OLATRuntimeException(ZipUtil.class, "Error getting output stream for file: " + zname, null);
}
long s = System.currentTimeMillis();
ZipOutputStream zipOut = new ZipOutputStream(new BufferedOutputStream(out, FileUtils.BSIZE));
if (vfsFiles.size() == 0) {
try {
zipOut.close();
} catch (IOException e) {
//
}
return true;
}
zipOut.setLevel(compress ? 9 : 0);
for (Iterator<VFSItem> iter = vfsFiles.iterator(); success && iter.hasNext(); ) {
success = addToZip(iter.next(), "", zipOut);
}
try {
zipOut.flush();
zipOut.close();
log.info("zipped (" + (compress ? "compress" : "store") + ") " + zname + " t=" + Long.toString(System.currentTimeMillis() - s));
} catch (IOException e) {
throw new OLATRuntimeException(ZipUtil.class, "I/O error closing file: " + zname, null);
}
return success;
}
use of org.olat.core.util.vfs.LocalImpl in project openolat by klemens.
the class QTI21QPoolServiceProvider method createItem.
public QuestionItem createItem(Identity identity, QTI21QuestionType type, String title, Locale locale) {
AssessmentItemBuilder itemBuilder = null;
Translator translator = Util.createPackageTranslator(AssessmentTestComposerController.class, locale);
switch(type) {
case sc:
itemBuilder = new SingleChoiceAssessmentItemBuilder(translator.translate("new.sc"), translator.translate("new.answer"), qtiService.qtiSerializer());
break;
case mc:
itemBuilder = new MultipleChoiceAssessmentItemBuilder(translator.translate("new.mc"), translator.translate("new.answer"), qtiService.qtiSerializer());
break;
case kprim:
itemBuilder = new KPrimAssessmentItemBuilder(translator.translate("new.kprim"), translator.translate("new.answer"), qtiService.qtiSerializer());
break;
case match:
itemBuilder = new MatchAssessmentItemBuilder(translator.translate("new.match"), QTI21Constants.CSS_MATCH_MATRIX, qtiService.qtiSerializer());
break;
case matchdraganddrop:
itemBuilder = new MatchAssessmentItemBuilder(translator.translate("new.match"), QTI21Constants.CSS_MATCH_DRAG_AND_DROP, qtiService.qtiSerializer());
break;
case matchtruefalse:
itemBuilder = new MatchAssessmentItemBuilder(translator.translate("new.match"), QTI21Constants.CSS_MATCH_TRUE_FALSE, translator.translate("match.unanswered"), translator.translate("match.true"), translator.translate("match.false"), qtiService.qtiSerializer());
break;
case fib:
itemBuilder = new FIBAssessmentItemBuilder(translator.translate("new.fib"), EntryType.text, qtiService.qtiSerializer());
break;
case numerical:
itemBuilder = new FIBAssessmentItemBuilder(translator.translate("new.fib.numerical"), EntryType.numerical, qtiService.qtiSerializer());
break;
case essay:
itemBuilder = new EssayAssessmentItemBuilder(translator.translate("new.essay"), qtiService.qtiSerializer());
break;
case upload:
itemBuilder = new UploadAssessmentItemBuilder(translator.translate("new.upload"), qtiService.qtiSerializer());
break;
case drawing:
itemBuilder = new DrawingAssessmentItemBuilder(translator.translate("new.drawing"), qtiService.qtiSerializer());
break;
case hotspot:
itemBuilder = new HotspotAssessmentItemBuilder(translator.translate("new.hotspot"), qtiService.qtiSerializer());
break;
case hottext:
itemBuilder = new HottextAssessmentItemBuilder(translator.translate("new.hottext"), translator.translate("new.hottext.start"), translator.translate("new.hottext.text"), qtiService.qtiSerializer());
break;
default:
return null;
}
AssessmentItem assessmentItem = itemBuilder.getAssessmentItem();
assessmentItem.setLabel(title);
assessmentItem.setTitle(title);
AssessmentItemMetadata itemMetadata = new AssessmentItemMetadata();
itemMetadata.setQuestionType(type);
QTI21ImportProcessor processor = new QTI21ImportProcessor(identity, locale);
QuestionItemImpl qitem = processor.processItem(assessmentItem, "", null, "OpenOLAT", Settings.getVersion(), itemMetadata);
VFSContainer baseDir = qpoolFileStorage.getContainer(qitem.getDirectory());
VFSLeaf leaf = baseDir.createChildLeaf(qitem.getRootFilename());
File itemFile = ((LocalImpl) leaf).getBasefile();
qtiService.persistAssessmentObject(itemFile, assessmentItem);
// create imsmanifest
ManifestBuilder manifest = ManifestBuilder.createAssessmentItemBuilder();
manifest.appendAssessmentItem(itemFile.getName());
manifest.write(new File(itemFile.getParentFile(), "imsmanifest.xml"));
return qitem;
}
Aggregations