/** * Convenience method. Calls format(long filesize). * * @param filesize The size of the file in bytes. * @return The size in human readable form. * @see #format(long) */ public static String format(Long filesize) { return format(filesize.longValue()); }
/** * Format the size of a file in human readable form. Anything less than a kilobyte * is presented in kilobytes to one decimal place. Anything between a kilobyte and a megabyte is * presented in kilobytes to zero decimal places. Anything greater than one megabyte is * presented in megabytes to two decimal places. * <p> * eg. * <ul> * <li>format(512) -> 0.5 kb * <li>format(1024) -> 1.0 kb * <li>format(2048) -> 2 kb * <li>format(1024 * 400) -> 400 kb * <li>format(1024 * 1024) -> 1024 kb * <li>format(1024 * 1024 * 1.2) -> 1.20 Mb * <li>format(1024 * 1024 * 20) -> 20.00 Mb * </ul> * * @param filesize The size of the file in bytes. * @return The size in human readable form. */ public static String format(long filesize) { // TODO: filesize = 1024 gives "1.0 kB", but filesize = 1025 gives "1 kB", this is kinda inconsistent. if (filesize > MB_SIZE) { return formatMB(filesize); } else if (filesize > KB_SIZE) { return formatKB(filesize); } else { return formatBytes(filesize); } }
/** * Convenience method. Calls format(long filesize). * @param filesize The size of the file in bytes. * @return The size in human readable form. * @see #format(long) */ public static String format(Long filesize) { return format(filesize.longValue()); }
return formatMB(filesize); return formatKB(filesize); return formatBytes(filesize);
private int recordProgress(int read) { nread += read; taskProgressSink.makeProgress(nread, subTask, MessageFormat.format(message, FileSize.format(nread))); return read; } }
@SuppressWarnings ("UnusedDeclaration") public String getMaxSizePretty() { final long maxSize = getMaxSize(); if (maxSize > 0) { return FileSize.format(maxSize); } else { return "Unknown?"; } }
public String getPrettyAttachmentSize() { return FileSize.format(new Long(Configuration.getString(APKeys.JIRA_ATTACHMENT_SIZE))); }
@Override public Map getContextMap(ApplicationUser user, JiraHelper jiraHelper) { ImmutableMap.Builder<String, Object> builder = ImmutableMap.builder(); // because it doesn't work via ApplicationProperties on OnDemand :( String attachmentSize = Configuration.getString(APKeys.JIRA_ATTACHMENT_SIZE); builder.put("jiraAttachmentSize", attachmentSize); builder.put("uploadLimit", FileSize.format(new Long(attachmentSize))); builder.put("formToken", tokenGenerator.generateToken()); return builder.build(); } }
private void validateAttachmentLimitSize(File file, String fileName) throws AttachmentException { long maxAttachmentSize = Long.parseLong(applicationProperties.getDefaultBackedString(APKeys.JIRA_ATTACHMENT_SIZE)); if (file.length() > maxAttachmentSize) { final String message = getI18nBean().getText("upload.too.big", fileName, FileSize.format(file.length()), FileSize.format(maxAttachmentSize)); throw new AttachmentExceedsLimitException(message); } }
@Override public String getEditHtml(final FieldLayoutItem fieldLayoutItem, final OperationContext operationContext, final Action action, final Issue issue, final Map displayParameters) { final Map<String, Object> velocityParams = getVelocityParams(fieldLayoutItem, action, issue, displayParameters); final Long maxSizeRaw = Long.valueOf(Configuration.getString(APKeys.JIRA_ATTACHMENT_SIZE)); velocityParams.put("maxSizeRaw", maxSizeRaw); velocityParams.put("maxSize", FileSize.format(maxSizeRaw)); final String formToken = (String) operationContext.getFieldValuesHolder().get(IssueFieldConstants.FORM_TOKEN); final Collection<?> tempFiles = (formToken != null) ? temporaryAttachmentUtil.getTemporaryWebAttachmentsByFormToken(formToken) : emptyList(); final Object checkedFiles = operationContext.getFieldValuesHolder().get(getId()); velocityParams.put("tempFiles", tempFiles); if (checkedFiles != null) { velocityParams.put("checkedFiles", checkedFiles); } return renderTemplate("attachment-edit.vm", velocityParams); }
@Override public Option<AttachmentError> get() { final long maxAttachmentSize = getMaxAttachmentSize(); if (size == 0) { return createAttachmentError( fileName, "Bad attachment size: 0", getI18n(user).getText("attachfile.error.io.bad.size.zero", fileName), VALIDATION_FAILED ); } else if (size > maxAttachmentSize) { return createAttachmentError( fileName, String.format("Attachment is too large. %s > %s", size, maxAttachmentSize), getI18n(user).getText("attachfile.error.file.large", fileName, FileSize.format(maxAttachmentSize)), VALIDATION_FAILED ); } else { return Option.none(); } } };
public String getCopyAttachmentsErrorMessageHtml() { if (!copyInfo.getAttachmentsEnabled()) { return getText("cpji.attachments.are.disabled"); } else if(!copyInfo.getHasCreateAttachmentPermission()) { return getText("cpji.not.permitted.to.create.attachments"); } else{ List<Attachment> attachments = getAttachmentsLargerThanAllowed(); if(attachments.size() == getAllAttachmentsCount()){ return getText("cpji.all.attachments.are.too.big", FileSize.format(copyInfo.getMaxAttachmentSize())); } else if(attachments.size() == 1) { return getText("cpji.one.attachment.is.too.big", TextUtils.htmlEncode(attachments.get(0).getFilename()), FileSize.format(copyInfo.getMaxAttachmentSize())); }else if(attachments.size() > 1){ String countText = getText("cpji.some.attachments.count", Integer.toString(attachments.size())); return getText("cpji.some.attachments.are.too.big", countText, FileSize.format(copyInfo.getMaxAttachmentSize())); } } return ""; }
inputStream = new ProgressMonitoringFileInputStream(stream, new StepTaskProgressSink(0, 20, stream.getSize(), taskProgressSink), i18n.getText("data.import.parse.xml"), i18n.getText("data.import.parse.progress", "{0}", FileSize.format(stream.getSize())));
private File createTempFileFromBoundedStream(final InputStream stream, final String fileName, final long size) throws ValidationException, IOException { if (0 >= size) { throw new ValidationException(getText("avatarpicker.upload.size.zero")); } try { return fileUtil.createTempFileFromBoundedStream(stream, size, TEMP_FILE_PREFIX); } catch (FileUtil.StreamSizeMismatchException sizeMismatch) { throw new ValidationException(getText("avatarpicker.upload.size.wrong")); } catch (LimitedOutputStream.TooBigIOException tooBigIOException) { throw new ValidationException(i18nHelper.getText("avatarpicker.upload.size.toobig", fileName, FileSize.format(tooBigIOException.getMaxSize()))); } catch (IOException ioException) { throw new IOException(i18nHelper.getText("attachfile.error.io.error", fileName, ioException.getMessage()), ioException); } }
return new Result<File>().addError(getText("avatarpicker.upload.size.toobig", fileName, FileSize.format(tooBigIOException.getMaxSize())), ErrorCollection.Reason.VALIDATION_FAILED);
filename, FileSize.format(largestAttachmentSize)));
private HumanReadableArchiveEntry convertEntry(final AttachmentArchiveEntry archiveEntry) { final Path path = new Path(archiveEntry.getName()); final long index = archiveEntry.getEntryIndex(); final String size = FileSize.format(archiveEntry.getSize()); final String mediaType = archiveEntry.getMediaType(); final String label = path.abbreviate(MAX_ENTRY_LABEL_LENGTH).toString(); return new HumanReadableArchiveEntry(path.toString(), index, size, mediaType, label); } }
private boolean exists(final File file, final String fileName, final int contentLength) throws AttachmentException { if (file == null) { if (fileName != null) { final long attachmentSize = getMaxAttachmentSize(); //now that we're only uploading a single file, the contentLength of the request is a pretty good //indicator if the attachment was too large. If it's bigger than the max size allowed we //return an error specific to this. if (contentLength > attachmentSize) { throw new AttachmentException(getI18n().getText("attachfile.error.file.large", fileName, FileSize.format(attachmentSize))); } else { throw new AttachmentException(getI18n().getText("attachfile.error.file.zero", fileName)); } } else { return false; } } else if (file.length() == 0) { throw new AttachmentException(getI18n().getText("attachfile.error.file.zero", fileName)); } return true; }