Search in sources :

Example 16 with LocalImpl

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);
        }
    }
}
Also used : Versionable(org.olat.core.util.vfs.version.Versionable) AssertException(org.olat.core.logging.AssertException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) LocalImpl(org.olat.core.util.vfs.LocalImpl) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) FileInputStream(java.io.FileInputStream)

Example 17 with LocalImpl

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;
}
Also used : OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) ZipOutputStream(java.util.zip.ZipOutputStream) ZipOutputStream(java.util.zip.ZipOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) VFSItem(org.olat.core.util.vfs.VFSItem) IOException(java.io.IOException) LocalImpl(org.olat.core.util.vfs.LocalImpl) BufferedOutputStream(java.io.BufferedOutputStream)

Example 18 with LocalImpl

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;
}
Also used : HottextAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.HottextAssessmentItemBuilder) VFSLeaf(org.olat.core.util.vfs.VFSLeaf) ManifestBuilder(org.olat.ims.qti21.model.xml.ManifestBuilder) SingleChoiceAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.SingleChoiceAssessmentItemBuilder) QuestionItemImpl(org.olat.modules.qpool.model.QuestionItemImpl) VFSContainer(org.olat.core.util.vfs.VFSContainer) EssayAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.EssayAssessmentItemBuilder) ResolvedAssessmentItem(uk.ac.ed.ph.jqtiplus.resolution.ResolvedAssessmentItem) AssessmentItem(uk.ac.ed.ph.jqtiplus.node.item.AssessmentItem) HotspotAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.HotspotAssessmentItemBuilder) UploadAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.UploadAssessmentItemBuilder) FIBAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.FIBAssessmentItemBuilder) SingleChoiceAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.SingleChoiceAssessmentItemBuilder) HottextAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.HottextAssessmentItemBuilder) DrawingAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.DrawingAssessmentItemBuilder) EssayAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.EssayAssessmentItemBuilder) MatchAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.MatchAssessmentItemBuilder) AssessmentItemBuilder(org.olat.ims.qti21.model.xml.AssessmentItemBuilder) KPrimAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.KPrimAssessmentItemBuilder) MultipleChoiceAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.MultipleChoiceAssessmentItemBuilder) MultipleChoiceAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.MultipleChoiceAssessmentItemBuilder) MatchAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.MatchAssessmentItemBuilder) FIBAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.FIBAssessmentItemBuilder) KPrimAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.KPrimAssessmentItemBuilder) Translator(org.olat.core.gui.translator.Translator) AssessmentItemMetadata(org.olat.ims.qti21.model.xml.AssessmentItemMetadata) HotspotAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.HotspotAssessmentItemBuilder) UploadAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.UploadAssessmentItemBuilder) LocalImpl(org.olat.core.util.vfs.LocalImpl) File(java.io.File) DrawingAssessmentItemBuilder(org.olat.ims.qti21.model.xml.interactions.DrawingAssessmentItemBuilder)

Aggregations

LocalImpl (org.olat.core.util.vfs.LocalImpl)18 File (java.io.File)10 IOException (java.io.IOException)10 VFSItem (org.olat.core.util.vfs.VFSItem)10 VFSContainer (org.olat.core.util.vfs.VFSContainer)8 BufferedOutputStream (java.io.BufferedOutputStream)4 FileInputStream (java.io.FileInputStream)4 InputStream (java.io.InputStream)4 VFSLeaf (org.olat.core.util.vfs.VFSLeaf)4 BufferedInputStream (java.io.BufferedInputStream)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 ZipEntry (java.util.zip.ZipEntry)2 ZipInputStream (java.util.zip.ZipInputStream)2 ZipOutputStream (java.util.zip.ZipOutputStream)2 FileChoosenEvent (org.olat.core.commons.controllers.filechooser.FileChoosenEvent)2 Translator (org.olat.core.gui.translator.Translator)2 AssertException (org.olat.core.logging.AssertException)2 OLATRuntimeException (org.olat.core.logging.OLATRuntimeException)2 LocalFileImpl (org.olat.core.util.vfs.LocalFileImpl)2