예제 #1
0
    @Override
    public void run() {
      try {
        StringBuilder filePath = new StringBuilder();
        filePath.append(Constants.IO.imageBaseDir).append(File.separator);
        filePath
            .append(card.hashCode())
            .append(".")
            .append(card.getName().replace(":", "").replace("//", "-"))
            .append(".jpg");
        File temporaryFile = new File(filePath.toString());
        String imagePath = CardImageUtils.generateImagePath(card);
        TFile outputFile = new TFile(imagePath);
        if (!outputFile.exists()) {
          outputFile.getParentFile().mkdirs();
        }
        File existingFile = new File(imagePath.replaceFirst("\\w{3}.zip", ""));
        if (existingFile.exists()) {
          new TFile(existingFile).cp_rp(outputFile);
          synchronized (sync) {
            update(cardIndex + 1, count);
          }
          existingFile.delete();
          File parent = existingFile.getParentFile();
          if (parent != null && parent.isDirectory() && parent.list().length == 0) {
            parent.delete();
          }
          return;
        }
        BufferedOutputStream out;

        // Logger.getLogger(this.getClass()).info(url.toString());
        URLConnection httpConn = url.openConnection(p);
        httpConn.connect();
        if (((HttpURLConnection) httpConn).getResponseCode() == 200) {
          try (BufferedInputStream in =
              new BufferedInputStream(((HttpURLConnection) httpConn).getInputStream())) {
            // try (BufferedInputStream in = new
            // BufferedInputStream(url.openConnection(p).getInputStream())) {
            out = new BufferedOutputStream(new TFileOutputStream(temporaryFile));
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) != -1) {
              // user cancelled
              if (cancel) {
                in.close();
                out.flush();
                out.close();
                temporaryFile.delete();
                return;
              }
              out.write(buf, 0, len);
            }
          }
          out.flush();
          out.close();

          if (card.isTwoFacedCard()) {
            BufferedImage image = ImageIO.read(temporaryFile);
            if (image.getHeight() == 470) {
              BufferedImage renderedImage = new BufferedImage(265, 370, BufferedImage.TYPE_INT_RGB);
              renderedImage.getGraphics();
              Graphics2D graphics2D = renderedImage.createGraphics();
              if (card.isTwoFacedCard() && card.isSecondSide()) {
                graphics2D.drawImage(image, 0, 0, 265, 370, 313, 62, 578, 432, null);
              } else {
                graphics2D.drawImage(image, 0, 0, 265, 370, 41, 62, 306, 432, null);
              }
              graphics2D.dispose();
              writeImageToFile(renderedImage, outputFile);
            } else {
              new TFile(temporaryFile).cp_rp(outputFile);
            }
            temporaryFile.delete();
          } else {
            new TFile(temporaryFile).cp_rp(outputFile);
            temporaryFile.delete();
          }
        } else {
          Logger.getLogger(this.getClass())
              .error(convertStreamToString(((HttpURLConnection) httpConn).getErrorStream()));
        }

      } catch (Exception e) {
        log.error(e, e);
      }

      synchronized (sync) {
        update(cardIndex + 1, count);
      }
    }