Search in sources :

Example 1 with Attach

use of com.tale.model.Attach in project tale by otale.

the class AttachServiceImpl method save.

@Override
public Attach save(String fname, String fkey, String ftype, Integer author) {
    Attach attach = new Attach();
    attach.setFname(fname);
    attach.setAuthor_id(author);
    attach.setFkey(fkey);
    attach.setFtype(ftype);
    attach.setCreated(DateKit.getCurrentUnixTime());
    activeRecord.insert(attach);
    return attach;
}
Also used : Attach(com.tale.model.Attach)

Example 2 with Attach

use of com.tale.model.Attach in project tale by otale.

the class AttachController method delete.

@Route(value = "delete")
@JSON
public RestResponse delete(@QueryParam Integer id, Request request) {
    try {
        Attach attach = attachService.byId(id);
        if (null == attach)
            return RestResponse.fail("不存在该附件");
        attachService.delete(id);
        siteService.cleanCache(Types.C_STATISTICS);
        String upDir = CLASSPATH.substring(0, CLASSPATH.length() - 1);
        FileKit.delete(upDir + attach.getFkey());
        logService.save(LogActions.DEL_ATTACH, attach.getFkey(), request.address(), this.getUid());
    } catch (Exception e) {
        String msg = "附件删除失败";
        if (e instanceof TipException)
            msg = e.getMessage();
        else
            LOGGER.error(msg, e);
        return RestResponse.fail(msg);
    }
    return RestResponse.ok();
}
Also used : Attach(com.tale.model.Attach) TipException(com.tale.exception.TipException) IOException(java.io.IOException) TipException(com.tale.exception.TipException) JSON(com.blade.mvc.annotation.JSON) Route(com.blade.mvc.annotation.Route)

Example 3 with Attach

use of com.tale.model.Attach in project tale by otale.

the class AttachController method upload.

/**
     * 上传文件接口
     *
     *  返回格式
     * @param request
     * @return
     */
@Route(value = "upload", method = HttpMethod.POST)
@JSON
public RestResponse upload(Request request) {
    LOGGER.info("UPLOAD DIR = {}", TaleUtils.upDir);
    Users users = this.user();
    Integer uid = users.getUid();
    Map<String, FileItem> fileItemMap = request.fileItems();
    Collection<FileItem> fileItems = fileItemMap.values();
    List<Attach> errorFiles = new ArrayList<>();
    List<Attach> urls = new ArrayList<>();
    try {
        fileItems.forEach((FileItem f) -> {
            String fname = f.fileName();
            if (f.file().length() / 1024 <= TaleConst.MAX_FILE_SIZE) {
                String fkey = TaleUtils.getFileKey(fname);
                String ftype = TaleUtils.isImage(f.file()) ? Types.IMAGE : Types.FILE;
                String filePath = TaleUtils.upDir + fkey;
                File file = new File(filePath);
                try {
                    Tools.copyFileUsingFileChannels(f.file(), file);
                    f.file().delete();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Attach attach = attachService.save(fname, fkey, ftype, uid);
                urls.add(attach);
                siteService.cleanCache(Types.C_STATISTICS);
            } else {
                errorFiles.add(new Attach(fname));
            }
        });
        if (errorFiles.size() > 0) {
            RestResponse restResponse = new RestResponse();
            restResponse.setSuccess(false);
            restResponse.setPayload(errorFiles);
            return restResponse;
        }
        return RestResponse.ok(urls);
    } catch (Exception e) {
        String msg = "文件上传失败";
        if (e instanceof TipException) {
            msg = e.getMessage();
        } else {
            LOGGER.error(msg, e);
        }
        return RestResponse.fail(msg);
    }
}
Also used : Attach(com.tale.model.Attach) RestResponse(com.blade.mvc.view.RestResponse) ArrayList(java.util.ArrayList) Users(com.tale.model.Users) IOException(java.io.IOException) TipException(com.tale.exception.TipException) IOException(java.io.IOException) TipException(com.tale.exception.TipException) FileItem(com.blade.mvc.multipart.FileItem) File(java.io.File) JSON(com.blade.mvc.annotation.JSON) Route(com.blade.mvc.annotation.Route)

Aggregations

Attach (com.tale.model.Attach)3 JSON (com.blade.mvc.annotation.JSON)2 Route (com.blade.mvc.annotation.Route)2 TipException (com.tale.exception.TipException)2 IOException (java.io.IOException)2 FileItem (com.blade.mvc.multipart.FileItem)1 RestResponse (com.blade.mvc.view.RestResponse)1 Users (com.tale.model.Users)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1