Search in sources :

Example 1 with ERXUnitAwareDecimalFormat

use of er.extensions.formatters.ERXUnitAwareDecimalFormat in project wonder-slim by undur.

the class AjaxFileUpload method bytesReadSize.

public String bytesReadSize() {
    String bytesReadSize = null;
    AjaxUploadProgress progress = uploadProgress();
    if (progress != null) {
        NumberFormat formatter = new ERXUnitAwareDecimalFormat(ERXUnitAwareDecimalFormat.BYTE);
        formatter.setMaximumFractionDigits(2);
        bytesReadSize = formatter.format(progress.value());
    }
    return bytesReadSize;
}
Also used : ERXUnitAwareDecimalFormat(er.extensions.formatters.ERXUnitAwareDecimalFormat) NumberFormat(java.text.NumberFormat)

Example 2 with ERXUnitAwareDecimalFormat

use of er.extensions.formatters.ERXUnitAwareDecimalFormat in project wonder-slim by undur.

the class AjaxFileUploadRequestHandler method handleRequest.

@Override
public WOResponse handleRequest(WORequest request) {
    WOApplication application = WOApplication.application();
    application.awake();
    try {
        WOContext context = application.createContextForRequest(request);
        WOResponse response = application.createResponseInContext(context);
        String uploadIdentifier = null;
        String uploadFileName = null;
        InputStream uploadInputStream = null;
        long streamLength = -1L;
        try {
            String sessionIdKey = WOApplication.application().sessionIdKey();
            String sessionId = request.cookieValueForKey(sessionIdKey);
            WOMultipartIterator multipartIterator = request.multipartIterator();
            if (multipartIterator == null) {
                response.appendContentString("Already Consumed!");
            } else {
                WOMultipartIterator.WOFormData formData = null;
                while ((formData = multipartIterator.nextFormData()) != null) {
                    String name = formData.name();
                    if (sessionIdKey.equals(name)) {
                        sessionId = formData.formValue();
                    } else if ("id".equals(name)) {
                        uploadIdentifier = formData.formValue();
                    } else if (formData.isFileUpload()) {
                        uploadFileName = request.stringFormValueForKey(name + ".filename");
                        streamLength = multipartIterator.contentLengthRemaining();
                        uploadInputStream = formData.formDataInputStream();
                        break;
                    }
                }
                context._setRequestSessionID(sessionId);
                WOSession session = null;
                if (context._requestSessionID() != null) {
                    session = WOApplication.application().restoreSessionWithID(sessionId, context);
                }
                if (session == null) {
                    throw new Exception("No valid session!");
                }
                File tempFile = File.createTempFile("AjaxFileUpload", ".tmp", _tempFileFolder);
                tempFile.deleteOnExit();
                AjaxUploadProgress progress = new AjaxUploadProgress(uploadIdentifier, tempFile, uploadFileName, streamLength);
                try {
                    AjaxProgressBar.registerProgress(session, progress);
                } finally {
                    if (context._requestSessionID() != null) {
                        WOApplication.application().saveSessionForContext(context);
                    }
                }
                if (formData != null) {
                    NSArray<String> contentType = (NSArray<String>) formData.headers().valueForKey("content-type");
                    if (contentType != null) {
                        progress.setContentType(contentType.objectAtIndex(0));
                    }
                }
                try {
                    if (_maxUploadSize >= 0L && streamLength > _maxUploadSize) {
                        IOException e = new IOException("You attempted to upload a file larger than the maximum allowed size of " + new ERXUnitAwareDecimalFormat(ERXUnitAwareDecimalFormat.BYTE).format(_maxUploadSize) + ".");
                        progress.setFailure(e);
                        progress.dispose();
                        throw e;
                    }
                    try (FileOutputStream fos = new FileOutputStream(progress.tempFile())) {
                        progress.copyAndTrack(uploadInputStream, fos, _maxUploadSize);
                    }
                    if (!progress.isCanceled() && !progress.shouldReset()) {
                        downloadFinished(progress);
                    }
                } finally {
                    progress.setDone(true);
                }
            }
        } catch (Throwable t) {
            log.error("Upload failed", t);
            response.appendContentString("Failed: " + t.getMessage());
        } finally {
            if (uploadInputStream != null) {
                try {
                    uploadInputStream.close();
                } catch (IOException e) {
                // ignore
                }
            }
        }
        return response;
    } finally {
        application.sleep();
    }
}
Also used : NSArray(com.webobjects.foundation.NSArray) InputStream(java.io.InputStream) WOMultipartIterator(com.webobjects.appserver.WOMultipartIterator) IOException(java.io.IOException) ERXUnitAwareDecimalFormat(er.extensions.formatters.ERXUnitAwareDecimalFormat) IOException(java.io.IOException) FileOutputStream(java.io.FileOutputStream) WOContext(com.webobjects.appserver.WOContext) WOSession(com.webobjects.appserver.WOSession) WOResponse(com.webobjects.appserver.WOResponse) File(java.io.File) WOApplication(com.webobjects.appserver.WOApplication)

Example 3 with ERXUnitAwareDecimalFormat

use of er.extensions.formatters.ERXUnitAwareDecimalFormat in project wonder-slim by undur.

the class AjaxFileUpload method streamLengthSize.

public String streamLengthSize() {
    String streamLengthSize = null;
    AjaxUploadProgress progress = uploadProgress();
    if (progress != null) {
        NumberFormat formatter = new ERXUnitAwareDecimalFormat(ERXUnitAwareDecimalFormat.BYTE);
        formatter.setMaximumFractionDigits(2);
        streamLengthSize = formatter.format(progress.maximum());
    }
    return streamLengthSize;
}
Also used : ERXUnitAwareDecimalFormat(er.extensions.formatters.ERXUnitAwareDecimalFormat) NumberFormat(java.text.NumberFormat)

Aggregations

ERXUnitAwareDecimalFormat (er.extensions.formatters.ERXUnitAwareDecimalFormat)3 NumberFormat (java.text.NumberFormat)2 WOApplication (com.webobjects.appserver.WOApplication)1 WOContext (com.webobjects.appserver.WOContext)1 WOMultipartIterator (com.webobjects.appserver.WOMultipartIterator)1 WOResponse (com.webobjects.appserver.WOResponse)1 WOSession (com.webobjects.appserver.WOSession)1 NSArray (com.webobjects.foundation.NSArray)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1