Exemplo n.º 1
0
  @RequestMapping(value = "api/{idString}/exprs/my", method = RequestMethod.GET)
  public @ResponseBody List<Expr> getMyExprsBycollection(
      @PathVariable("idString") String idString,
      @RequestParam(value = "tag") String content,
      HttpServletRequest request) {
    Visit visit = new Visit();
    visit.setCreated(new Date());
    visit.setUserId(idString);
    visit.setVisitUrl(request.getRequestURL().toString());
    visit.setClientIp(request.getRemoteAddr());
    visitRepository.save(visit);

    List<Collection> collections = collectionRepository.findAllByOwnerAndContent(idString, content);
    List<Long> exprIds = new ArrayList<Long>();
    for (Collection collection : collections) {
      exprIds.add(collection.getExprId());
    }
    List<Expr> exprs = exprRepository.findAllByIdIn(exprIds);
    for (Expr expr : exprs) {
      expr.eraseDangerousInfo();
    }
    return exprs;
  }
Exemplo n.º 2
0
  @RequestMapping(value = "api/{idString}/expr/new", method = RequestMethod.POST)
  public @ResponseBody Expr createExpr(
      @PathVariable("idString") String idString,
      @RequestParam("expr") MultipartFile exprFile,
      @RequestParam("tag") String content,
      HttpServletRequest request)
      throws IOException {
    Visit visit = new Visit();
    visit.setCreated(new Date());
    visit.setUserId(idString);
    visit.setVisitUrl(request.getRequestURL().toString());
    visit.setClientIp(request.getRemoteAddr());
    visitRepository.save(visit);

    String md5 = StringUtil.MD5(exprFile.getBytes());
    List<Expr> exprs = exprRepository.findAllByMd5(md5);
    if (exprs.isEmpty()) {
      String extension = FileUtil.getExtention(exprFile);
      if (FileUtil.saveExpr(md5 + extension, exprFile.getBytes())) {
        Expr expr = new Expr();
        expr.setMd5(md5);
        expr.setCreator(idString);
        expr.setExtension(extension);
        exprRepository.save(expr);
        Collection collection = new Collection();
        collection.setExprId(expr.getId());
        collection.setOwner(idString);
        collection.setContent("");
        collectionRepository.save(collection);
        if (content.length() > 0) {
          collection = new Collection();
          collection.setExprId(expr.getId());
          collection.setOwner(idString);
          collection.setContent(content);
          collectionRepository.save(collection);
        }
        return expr.eraseDangerousInfo();
      } else {
        return null;
      }
    } else {
      List<Collection> collections =
          collectionRepository.findAllByOwnerAndContentAndExprId(
              idString, "", exprs.get(0).getId());
      if (collections.isEmpty()) {
        Collection collection = new Collection();
        collection.setExprId(exprs.get(0).getId());
        collection.setOwner(idString);
        collection.setContent("");
        collectionRepository.save(collection);
      }
      collections =
          collectionRepository.findAllByOwnerAndContentAndExprId(
              idString, content, exprs.get(0).getId());
      if (collections.isEmpty()) {
        Collection collection = new Collection();
        collection.setExprId(exprs.get(0).getId());
        collection.setOwner(idString);
        collection.setContent(content);
        collectionRepository.save(collection);
      }
      return exprs.get(0).eraseDangerousInfo();
    }
  }