Ejemplo n.º 1
0
 public int getPriority() {
   if (type == null) return 0;
   DocumentFormat df = DocumentFormat.byMimeType(type);
   if (rel != null
       && rel.indexOf("acquisition") < 0
       && df != DocumentFormat.FB2
       && df != DocumentFormat.EPUB
       && df != DocumentFormat.RTF
       && df != DocumentFormat.DOC) return 0;
   return df != null ? df.getPriority() : 0;
 }
Ejemplo n.º 2
0
 private File generateFileName(File outDir, String fileName, String type, boolean isZip) {
   DocumentFormat fmt = type != null ? DocumentFormat.byMimeType(type) : null;
   // DocumentFormat fmtext = fileName!=null ? DocumentFormat.byExtension(fileName) : null;
   if (fileName == null) fileName = "noname";
   String ext = null;
   if (fileName.lastIndexOf(".") > 0) {
     ext = fileName.substring(fileName.lastIndexOf(".") + 1);
     fileName = fileName.substring(0, fileName.lastIndexOf("."));
   }
   fileName = transcribeFileName(fileName);
   if (fmt != null) {
     if (fmt == DocumentFormat.FB2 && isZip) ext = ".fb2.zip";
     else ext = fmt.getExtensions()[0].substring(1);
   }
   for (int i = 0; i < 1000; i++) {
     String fn = fileName + (i == 0 ? "" : "(" + i + ")") + "." + ext;
     File f = new File(outDir, fn);
     if (!f.exists() && !f.isDirectory()) return f;
   }
   return null;
 }
Ejemplo n.º 3
0
    private void downloadBook(
        final String type,
        final String url,
        InputStream is,
        int contentLength,
        final String fileName,
        final boolean isZip)
        throws Exception {
      L.d("Download requested: " + type + " " + url + " " + contentLength);
      DocumentFormat fmt = DocumentFormat.byMimeType(type);
      if (fmt == null) {
        L.d("Download: unknown type " + type);
        throw new Exception("Unknown file type " + type);
      }
      final File outDir =
          BackgroundThread.instance()
              .callGUI(
                  new Callable<File>() {
                    @Override
                    public File call() throws Exception {
                      return callback.onDownloadStart(type, url);
                    }
                  });
      if (outDir == null) {
        L.d("Cannot find writable location for downloaded file " + url);
        throw new Exception("Cannot save file " + url);
      }
      final File outFile = generateFileName(outDir, fileName, type, isZip);
      if (outFile == null) {
        L.d("Cannot generate file name");
        throw new Exception("Cannot generate file name");
      }
      L.d("Creating file: " + outFile.getAbsolutePath());
      if (outFile.exists() || !outFile.createNewFile()) {
        L.d("Cannot create file " + outFile.getAbsolutePath());
        throw new Exception("Cannot create file");
      }

      L.d("Download started: " + outFile.getAbsolutePath());
      //			long lastTs = System.currentTimeMillis();
      //			int lastPercent = -1;
      FileOutputStream os = null;
      boolean success = false;
      try {
        os = new FileOutputStream(outFile);
        byte[] buf = new byte[16384];
        int totalWritten = 0;
        while (totalWritten < contentLength || contentLength == -1) {
          int bytesRead = is.read(buf);
          if (bytesRead <= 0) break;
          os.write(buf, 0, bytesRead);
          totalWritten += bytesRead;
          //					final int percent = totalWritten * 100 / contentLength;
          //					long ts = System.currentTimeMillis();
          //					if ( percent!=lastPercent && ts - lastTs > 1500 ) {
          //						L.d("Download progress: " + percent + "%");
          //						BackgroundThread.instance().postGUI(new Runnable() {
          //							@Override
          //							public void run() {
          //								callback.onDownloadProgress(type, url, percent);
          //							}
          //						});
          //					}
        }
        success = true;
      } finally {
        if (os != null) os.close();
        if (!success) {
          if (outFile.exists() && outFile.isFile()) {
            L.w("deleting unsuccessully downloaded file " + outFile);
            outFile.delete();
          }
        }
      }
      L.d("Download finished");
      BackgroundThread.instance()
          .executeGUI(
              new Runnable() {
                @Override
                public void run() {
                  callback.onDownloadEnd(type, url, outFile);
                }
              });
    }