Java Code Examples for com.hippo.unifile.UniFile#subFile()

The following examples show how to use com.hippo.unifile.UniFile#subFile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SpiderDen.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
private boolean removeFromDownloadDir(int index) {
    UniFile dir = getDownloadDir();
    if (dir == null) {
        return false;
    }

    boolean result = false;
    for (int i = 0, n = GalleryProvider2.SUPPORT_IMAGE_EXTENSIONS.length; i < n; i++) {
        String filename = generateImageFilename(index, GalleryProvider2.SUPPORT_IMAGE_EXTENSIONS[i]);
        UniFile file = dir.subFile(filename);
        if (file != null) {
            result |= file.delete();
        }
    }
    return result;
}
 
Example 2
Source File: SpiderDen.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
private boolean removeFromDownloadDir(int index) {
    UniFile dir = getDownloadDir();
    if (dir == null) {
        return false;
    }

    boolean result = false;
    for (int i = 0, n = GalleryProvider2.SUPPORT_IMAGE_EXTENSIONS.length; i < n; i++) {
        String filename = generateImageFilename(index, GalleryProvider2.SUPPORT_IMAGE_EXTENSIONS[i]);
        UniFile file = dir.subFile(filename);
        if (file != null) {
            result |= file.delete();
        }
    }
    return result;
}
 
Example 3
Source File: SpiderQueen.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
@Nullable
public UniFile save(int index, @NonNull UniFile dir, @NonNull String filename) {
    int state = getPageState(index);
    if (STATE_FINISHED != state) {
        return null;
    }

    InputStreamPipe pipe = mSpiderDen.openInputStreamPipe(index);
    if (null == pipe) {
        return null;
    }

    OutputStream os = null;
    try {
        pipe.obtain();

        // Get dst file
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(pipe.open(), null, options);
        pipe.close();
        String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(options.outMimeType);
        UniFile dst = dir.subFile(null != extension ? filename + "." + extension : filename);
        if (null == dst) {
            return null;
        }

        // Copy
        os = dst.openOutputStream();
        IOUtils.copy(pipe.open(), os);
        return dst;
    } catch (IOException e) {
        return null;
    } finally {
        pipe.close();
        pipe.release();
        IOUtils.closeQuietly(os);
    }
}
 
Example 4
Source File: DirGalleryProvider.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public UniFile save(int index, @NonNull UniFile dir, @NonNull String filename) {
    UniFile[] fileList = mFileList.get();
    if (null == fileList || index < 0 || index >= fileList.length) {
        return null;
    }

    UniFile src = fileList[index];
    String extension = FileUtils.getExtensionFromFilename(src.getName());
    UniFile dst = dir.subFile(null != extension ? filename + "." + extension : filename);
    if (null == dst) {
        return null;
    }

    InputStream is = null;
    OutputStream os = null;
    try {
        is = src.openInputStream();
        os = dst.openOutputStream();
        IOUtils.copy(is, os);
        return dst;
    } catch (IOException e) {
        return null;
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }
}
 
Example 5
Source File: CommonOperations.java    From MHViewer with Apache License 2.0 5 votes vote down vote up
public static void removeNoMediaFile(UniFile file) {
    if (null == file) {
        return;
    }

    UniFile noMedia = file.subFile(".nomedia");
    if (null != noMedia && noMedia.isFile()) {
        noMedia.delete();
    }
}
 
Example 6
Source File: SpiderQueen.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@Nullable
public UniFile save(int index, @NonNull UniFile dir, @NonNull String filename) {
    int state = getPageState(index);
    if (STATE_FINISHED != state) {
        return null;
    }

    InputStreamPipe pipe = mSpiderDen.openInputStreamPipe(index);
    if (null == pipe) {
        return null;
    }

    OutputStream os = null;
    try {
        pipe.obtain();

        // Get dst file
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(pipe.open(), null, options);
        pipe.close();
        String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(options.outMimeType);
        UniFile dst = dir.subFile(null != extension ? filename + "." + extension : filename);
        if (null == dst) {
            return null;
        }

        // Copy
        os = dst.openOutputStream();
        IOUtils.copy(pipe.open(), os);
        return dst;
    } catch (IOException e) {
        return null;
    } finally {
        pipe.close();
        pipe.release();
        IOUtils.closeQuietly(os);
    }
}
 
Example 7
Source File: DirGalleryProvider.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public UniFile save(int index, @NonNull UniFile dir, @NonNull String filename) {
    UniFile[] fileList = mFileList.get();
    if (null == fileList || index < 0 || index >= fileList.length) {
        return null;
    }

    UniFile src = fileList[index];
    String extension = FileUtils.getExtensionFromFilename(src.getName());
    UniFile dst = dir.subFile(null != extension ? filename + "." + extension : filename);
    if (null == dst) {
        return null;
    }

    InputStream is = null;
    OutputStream os = null;
    try {
        is = src.openInputStream();
        os = dst.openOutputStream();
        IOUtils.copy(is, os);
        return dst;
    } catch (IOException e) {
        return null;
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }
}
 
Example 8
Source File: CommonOperations.java    From EhViewer with Apache License 2.0 5 votes vote down vote up
public static void removeNoMediaFile(UniFile file) {
    if (null == file) {
        return;
    }

    UniFile noMedia = file.subFile(".nomedia");
    if (null != noMedia && noMedia.isFile()) {
        noMedia.delete();
    }
}
 
Example 9
Source File: SpiderDen.java    From MHViewer with Apache License 2.0 4 votes vote down vote up
public static UniFile getGalleryDownloadDir(GalleryInfo galleryInfo) {
    UniFile dir = Settings.getDownloadLocation();
    if (dir != null) {
        // Read from DB
        String dirname = EhDB.getDownloadDirname(galleryInfo.getCid());
        if (null != dirname) {
            // Some dirname may be invalid in some version
            dirname = FileUtils.sanitizeFilename(dirname);
            EhDB.putDownloadDirname(galleryInfo.getCid(), dirname);
        }

        // Find it
        if (null == dirname) {
            UniFile[] files = dir.listFiles(new StartWithFilenameFilter(galleryInfo.getCid() + "-"));
            if (null != files) {
                // Get max-length-name dir
                int maxLength = -1;
                for (UniFile file : files) {
                    if (file.isDirectory()) {
                        String name = file.getName();
                        int length = name.length();
                        if (length > maxLength) {
                            maxLength = length;
                            dirname = name;
                        }
                    }
                }
                if (null != dirname) {
                    EhDB.putDownloadDirname(galleryInfo.getCid(), dirname);
                }
            }
        }

        // Create it
        if (null == dirname) {
            dirname = FileUtils.sanitizeFilename(galleryInfo.getCid() + "-" + EhUtils.getSuitableTitle(galleryInfo));
            EhDB.putDownloadDirname(galleryInfo.getCid(), dirname);
        }

        return dir.subFile(dirname);
    } else {
        return null;
    }
}
 
Example 10
Source File: SpiderDen.java    From EhViewer with Apache License 2.0 4 votes vote down vote up
public static UniFile getGalleryDownloadDir(GalleryInfo galleryInfo) {
    UniFile dir = Settings.getDownloadLocation();
    if (dir != null) {
        // Read from DB
        String dirname = EhDB.getDownloadDirname(galleryInfo.gid);
        if (null != dirname) {
            // Some dirname may be invalid in some version
            dirname = FileUtils.sanitizeFilename(dirname);
            EhDB.putDownloadDirname(galleryInfo.gid, dirname);
        }

        // Find it
        if (null == dirname) {
            UniFile[] files = dir.listFiles(new StartWithFilenameFilter(galleryInfo.gid + "-"));
            if (null != files) {
                // Get max-length-name dir
                int maxLength = -1;
                for (UniFile file : files) {
                    if (file.isDirectory()) {
                        String name = file.getName();
                        int length = name.length();
                        if (length > maxLength) {
                            maxLength = length;
                            dirname = name;
                        }
                    }
                }
                if (null != dirname) {
                    EhDB.putDownloadDirname(galleryInfo.gid, dirname);
                }
            }
        }

        // Create it
        if (null == dirname) {
            dirname = FileUtils.sanitizeFilename(galleryInfo.gid + "-" + EhUtils.getSuitableTitle(galleryInfo));
            EhDB.putDownloadDirname(galleryInfo.gid, dirname);
        }

        return dir.subFile(dirname);
    } else {
        return null;
    }
}