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; }
public static DocumentFormat byMimeType(String format) { if (format == null) return null; String s = format.toLowerCase(); for (int i = 0; i < DocumentFormat.values().length; i++) if (values()[i].matchMimeType(s)) return values()[i]; return null; }
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; }
private FileInfo scanZip(FileInfo zip) { try { File zf = new File(zip.pathname); long arcsize = zf.length(); // ZipFile file = new ZipFile(zf); ArrayList<ZipEntry> entries = engine.getArchiveItems(zip.pathname); ArrayList<FileInfo> items = new ArrayList<FileInfo>(); // for ( Enumeration<?> e = file.entries(); e.hasMoreElements(); ) { for (ZipEntry entry : entries) { if (entry.isDirectory()) continue; String name = entry.getName(); FileInfo item = new FileInfo(); item.format = DocumentFormat.byExtension(name); if (item.format == null) continue; File f = new File(name); item.filename = f.getName(); item.path = f.getPath(); item.pathname = entry.getName(); item.size = (int) entry.getSize(); // item.createTime = entry.getTime(); item.createTime = zf.lastModified(); item.arcname = zip.pathname; item.arcsize = (int) entry.getCompressedSize(); item.isArchive = true; items.add(item); } if (items.size() == 0) { L.i("Supported files not found in " + zip.pathname); return null; } else if (items.size() == 1) { // single supported file in archive FileInfo item = items.get(0); item.isArchive = true; item.isDirectory = false; return item; } else { zip.isArchive = true; zip.isDirectory = true; zip.isListed = true; for (FileInfo item : items) { item.parent = zip; zip.addFile(item); } return zip; } } catch (Exception e) { L.e("IOException while opening " + zip.pathname + " " + e.getMessage()); } return null; }
public static DocumentFormat byExtension(String filename) { String s = filename.toLowerCase(); for (int i = 0; i < DocumentFormat.values().length; i++) if (values()[i].matchExtension(s)) return values()[i]; return null; }
public static DocumentFormat byId(int i) { if (i >= 0 && i < DocumentFormat.values().length) return values()[i]; return null; }
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); } }); }