Example #1
0
  /**
   * Upload an object
   *
   * @param upload The upload object
   */
  public void upload(final Upload upload) {
    // Check associations
    if (!uploaderAssociations.containsKey(upload.getClass())) {
      icon.displayMessage(
          Language.getString("noUploaderTitle"),
          Language.getString("noUploader", upload.getClass().getName()),
          TrayIcon.MessageType.ERROR);
      return;
    }

    uploadService.execute(
        new Runnable() {
          @Override
          public void run() {
            icon.setImage(Resources.ICON_BUSY_IMAGE);
            executeUpload(upload);
            icon.setImage(Resources.ICON_IMAGE);
          }
        });
  }
Example #2
0
  /**
   * Execute an upload
   *
   * @param object The object to upload
   */
  @SuppressWarnings({"rawtypes", "unchecked"})
  public void executeUpload(Upload object) {
    // Run the object through the filters
    if (filters.containsKey(object.getClass())) {
      for (final UploadFilter filter : filters.get(object.getClass())) {
        try {
          object = filter.filter(object);
        } catch (final FilterException e) {
          // FilterExceptions when thrown should interrupt the upload.
          showException(e, e.getErrorMessage());
          return;
        }
      }
    }
    // Then upload it
    final Uploader uploader = uploaderAssociations.get(object.getClass());
    if (uploader != null) {
      try {
        String url = uploader.upload(object);
        if (url != null) {
          if (configuration.getBoolean("shortenurls")) {
            final Uploader shortener = uploaderAssociations.get(URL.class);
            if (shortener != null) {
              url = shortener.upload(new URLUpload(url));
            }
          }
          if (object instanceof ImageUpload) {
            if (configuration.getBoolean("savelocal")
                && !(uploader instanceof ImageLocalFileUploader)) {
              final FileOutputStream output =
                  new FileOutputStream(getLocalFile(DateUtil.getCurrentDate() + ".png"));
              try {
                ImageIO.write(((ImageUpload) object).getImage(), "png", output);
              } finally {
                output.close();
              }
            }
            ((ImageUpload) object).getImage().flush();
            ((ImageUpload) object).setImage(null);
          }
          url = url.trim();

          retries = 0;

          ClipboardUtil.setClipboard(url);

          lastUrl = url;
          history.addEntry(new HistoryEntry(url, uploader.getName()));
          icon.displayMessage(
              Language.getString("uploadComplete"),
              Language.getString("uploadedTo", url),
              TrayIcon.MessageType.INFO);
          logger.info("Upload completed, url: " + url);
        } else {
          icon.displayMessage(
              Language.getString("uploadFailed"),
              Language.getString("uploadFailedError"),
              TrayIcon.MessageType.ERROR);
          logger.severe("Upload failed to execute due to an unknown error");
        }
      } catch (final UploaderConfigurationException e) {
        icon.displayMessage(
            Language.getString("uploaderConfigError"),
            Language.getString("uploaderConfigErrorMessage"),
            TrayIcon.MessageType.ERROR);
        logger.log(Level.SEVERE, "Upload failed to execute", e);
      } catch (final Exception e) {
        // Retry until retries > max
        final StringBuilder msg = new StringBuilder("The upload failed to execute: ");
        msg.append(e.getMessage());
        final int max =
            configuration.getInteger("max_retries", Constants.Configuration.DEFAULT_MAX_RETRIES);
        if (retries++ < max) {
          logger.info("Retrying upload (" + retries + " of " + max + " retries)...");
          msg.append("\nRetrying...");
          upload(object);
        } else {
          msg.append("\nReached retry limit, upload aborted.");
          logger.log(Level.SEVERE, "Upload failed to execute, retries: " + retries, e);
          retries = 0;
        }
        icon.displayMessage(
            Language.getString("uploadFailed"), msg.toString(), TrayIcon.MessageType.ERROR);
      }
    }
  }