Search in sources :

Example 1 with InternalIOException

use of com.duan.blogos.exception.internal.InternalIOException in project BlogSystem by DuanJiaNing.

the class BloggerPictureServiceImpl method removeDefaultPictureIfNecessary.

/*
     * 腾地方
     * 移到默认图片到私有图片文件夹,同时修改数据库记录
     * @param bloggerId 博主id
     */
private void removeDefaultPictureIfNecessary(int bloggerId, BloggerPictureCategoryEnum defaultCate) {
    BloggerPicture unique = pictureDao.getBloggerUniquePicture(bloggerId, defaultCate.getCode());
    if (unique != null) {
        try {
            // 移动默认图片到私有类别图片所在文件夹
            String newPath = imageManager.moveImage(unique, bloggerId, BloggerPictureCategoryEnum.PRIVATE);
            // 更新数据库记录
            unique.setCategory(BloggerPictureCategoryEnum.PRIVATE.getCode());
            unique.setPath(newPath);
            pictureDao.update(unique);
        } catch (IOException e) {
            e.printStackTrace();
            // MAYBUG 回滚数据库操作,但磁盘操作无法预料,也无法处理
            throw new InternalIOException(e);
        }
    }
}
Also used : BloggerPicture(com.duan.blogos.entity.blogger.BloggerPicture) InternalIOException(com.duan.blogos.exception.internal.InternalIOException) InternalIOException(com.duan.blogos.exception.internal.InternalIOException) IOException(java.io.IOException)

Example 2 with InternalIOException

use of com.duan.blogos.exception.internal.InternalIOException in project BlogSystem by DuanJiaNing.

the class BloggerPictureServiceImpl method updatePicture.

@Override
public boolean updatePicture(int pictureId, BloggerPictureCategoryEnum category, String bewrite, String title) {
    BloggerPicture oldPicture = pictureDao.getPictureById(pictureId);
    // 修改设备上图片路径,如果需要的话
    String newPath = null;
    if (category != null && category.getCode() != oldPicture.getCategory()) {
        // 修改了类别
        int bloggerId = oldPicture.getBloggerId();
        try {
            int pictureManagerId = bloggerProperties.getPictureManagerBloggerId();
            // 如果为图片管理员在操作
            if (pictureManagerId == bloggerId) {
                // 以下两种情况将更新失败,对于默认图片,且图片管理员在操作的情况下,要修改类别或删除图片,只能
                // 以 普通 -> 默认 的修改方向替换图片,因为这些图片必须时刻存在
                // 1 目标类别是默认类别,原先类别也为默认类别      默认 -> 默认
                // 2 目标类别为普通类别,原先类别为默认类别        默认 -> 普通
                int oldCategory = oldPicture.getCategory();
                if ((BloggerPictureCategoryEnum.isDefaultPictureCategory(oldCategory) && BloggerPictureCategoryEnum.isDefaultPictureCategory(category.getCode())) || (BloggerPictureCategoryEnum.isDefaultPictureCategory(oldCategory) && !BloggerPictureCategoryEnum.isDefaultPictureCategory(category.getCode()))) {
                    return false;
                } else {
                    // 腾位置,如果需要的话
                    removeDefaultPictureIfNecessary(bloggerId, category);
                    // 移动到目标目录
                    newPath = imageManager.moveImage(oldPicture, bloggerId, category);
                }
            } else {
                // 不是图片管理员则只需移动文件即可
                newPath = imageManager.moveImage(oldPicture, bloggerId, category);
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new InternalIOException(e);
        }
    }
    BloggerPicture newPicture = new BloggerPicture();
    newPicture.setBewrite(bewrite);
    newPicture.setBloggerId(oldPicture.getBloggerId());
    newPicture.setCategory(category == null ? oldPicture.getCategory() : category.getCode());
    newPicture.setId(oldPicture.getId());
    newPicture.setTitle(title);
    newPicture.setPath(newPath == null ? oldPicture.getPath() : newPath);
    int effect = pictureDao.update(newPicture);
    if (effect <= 0)
        return false;
    return true;
}
Also used : BloggerPicture(com.duan.blogos.entity.blogger.BloggerPicture) InternalIOException(com.duan.blogos.exception.internal.InternalIOException) InternalIOException(com.duan.blogos.exception.internal.InternalIOException) IOException(java.io.IOException)

Example 3 with InternalIOException

use of com.duan.blogos.exception.internal.InternalIOException in project BlogSystem by DuanJiaNing.

the class BloggerPictureServiceImpl method deletePicture.

@Override
public boolean deletePicture(int bloggerId, int pictureId, boolean deleteOnDisk) {
    BloggerPicture picture = getPicture(pictureId);
    // 对默认图片,图片管理员只能以更新(上传)的方式删除图片,因为这些图片必须时刻存在
    int pictureManagerId = bloggerProperties.getPictureManagerBloggerId();
    int cate = picture.getCategory();
    if (bloggerId == pictureManagerId && BloggerPictureCategoryEnum.isDefaultPictureCategory(cate))
        return false;
    // 删除数据库记录
    String path = picture.getPath();
    int effect = pictureDao.delete(pictureId);
    if (effect <= 0)
        return false;
    if (deleteOnDisk) {
        // 删除磁盘文件
        boolean succ = imageManager.deleteImageFromDisk(path);
        // 删除失败时抛出异常,使数据库事务回滚
        if (!succ)
            throw new InternalIOException();
    }
    return true;
}
Also used : BloggerPicture(com.duan.blogos.entity.blogger.BloggerPicture) InternalIOException(com.duan.blogos.exception.internal.InternalIOException)

Aggregations

BloggerPicture (com.duan.blogos.entity.blogger.BloggerPicture)3 InternalIOException (com.duan.blogos.exception.internal.InternalIOException)3 IOException (java.io.IOException)2