Пример #1
0
  /** Upload content from the clipboard */
  public void clipboard() {
    try {
      final Object clipboard = ClipboardUtil.getClipboardContents();
      if (clipboard == null) {
        icon.displayMessage(
            Language.getString("invalidClipboard"),
            Language.getString("invalidClipboardTitle"),
            TrayIcon.MessageType.WARNING);
        return;
      }
      if (clipboard instanceof BufferedImage) {
        upload(new ImageUpload((BufferedImage) clipboard));
      } else if (clipboard instanceof File) {
        final File file = (File) clipboard;
        final String mime = FileUtils.getMimeType(file.getAbsolutePath());

        // A better way to upload images, it'll check the mime type!
        if (mime.startsWith("image")) {
          upload(new ImageUpload(ImageIO.read(file)));
        } else if (mime.startsWith("text") && configuration.getBoolean("plainTextUpload")) {
          upload(new TextUpload(FileUtils.readFile(file)));
        } else {
          upload(new FileUpload(file));
        }
      } else if (clipboard instanceof String) {
        final String string = clipboard.toString();
        if (string.matches("((mailto\\:|(news|(ht|f)tp(s?))\\://){1}\\S+)")) {
          upload(new URLUpload(clipboard.toString()));
        } else {
          upload(new TextUpload(string));
        }
      }
    } catch (final ClipboardException e) {
      logger.log(Level.SEVERE, "Unable to get clipboard contents", e);
      showException(e);
    } catch (final IOException e) {
      e.printStackTrace();
    }
  }
Пример #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);
      }
    }
  }