Demo entry 6350225

java

   

Submitted by anonymous on Mar 08, 2017 at 09:30
Language: Java. Code size: 1.5 kB.

@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<String> errorFiles = 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();
                    }
                    attachService.save(fname, fkey, ftype, uid);
                    siteService.cleanCache(Types.C_STATISTICS);
                } else {
                    errorFiles.add(fname);
                }
            });
        } catch (Exception e) {
            return RestResponse.fail();
        }
        return RestResponse.ok(errorFiles);
    }

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).