Skip to content

Commit

Permalink
🦄 fix otale#69
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Mar 1, 2017
1 parent f528807 commit ac7c462
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
22 changes: 5 additions & 17 deletions src/main/java/com/tale/controller/admin/AttachController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import com.blade.ioc.annotation.Inject;
import com.blade.jdbc.core.Take;
import com.blade.jdbc.model.Paginator;
import com.blade.kit.DateKit;
import com.blade.kit.FileKit;
import com.blade.kit.Tools;
import com.blade.kit.UUID;
import com.blade.mvc.annotation.Controller;
import com.blade.mvc.annotation.JSON;
import com.blade.mvc.annotation.QueryParam;
Expand All @@ -31,7 +29,6 @@
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

/**
Expand Down Expand Up @@ -65,8 +62,7 @@ public String index(Request request, @QueryParam(value = "page", defaultValue =
@JSON
public RestResponse upload(Request request) {

String upDir = CLASSPATH.substring(0, CLASSPATH.length() - 1);
LOGGER.info("UPLOAD DIR = {}", upDir);
LOGGER.info("UPLOAD DIR = {}", TaleUtils.upDir);

Users users = this.user();
Integer uid = users.getUid();
Expand All @@ -75,22 +71,14 @@ public RestResponse upload(Request request) {
try {
fileItems.forEach(f -> {
String fname = f.fileName();

String prefix = "/upload/" + DateKit.dateFormat(new Date(), "yyyy/MM");

String dir = upDir + prefix;
if (!FileKit.exist(dir)) {
new File(dir).mkdirs();
}

String fkey = prefix + "/" + UUID.UU32() + "." + FileKit.getExtension(fname);
String fkey = TaleUtils.getFileKey(fname);
String ftype = TaleUtils.isImage(f.file()) ? Types.IMAGE : Types.FILE;

String filePath = upDir + fkey;
String filePath = TaleUtils.upDir + fkey;

File file = new File(filePath);
try {
Tools.copyFileUsingFileChannels(f.file(), file);
f.file().delete();
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -108,7 +96,7 @@ public RestResponse upload(Request request) {
public RestResponse delete(@QueryParam Integer id, Request request) {
try {
Attach attach = attachService.byId(id);
if(null == attach){
if (null == attach) {
return RestResponse.fail("不存在该附件");
}
attachService.delete(id);
Expand Down
16 changes: 13 additions & 3 deletions src/main/java/com/tale/utils/TaleUtils.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.tale.utils;

import com.blade.context.WebContextHolder;
import com.blade.kit.DateKit;
import com.blade.kit.StringKit;
import com.blade.kit.Tools;
import com.blade.kit.*;
import com.blade.mvc.http.Request;
import com.blade.mvc.http.Response;
import com.blade.mvc.http.wrapper.Session;
Expand All @@ -12,6 +10,7 @@
import com.sun.syndication.feed.rss.Item;
import com.sun.syndication.io.FeedException;
import com.sun.syndication.io.WireFeedOutput;
import com.tale.controller.admin.AttachController;
import com.tale.ext.Commons;
import com.tale.ext.Theme;
import com.tale.init.TaleConst;
Expand Down Expand Up @@ -385,4 +384,15 @@ public static <T> String listToInSql(java.util.List<T> list){
sbuf.append(')');
return '(' + sbuf.substring(1);
}

public static final String upDir = AttachController.CLASSPATH.substring(0, AttachController.CLASSPATH.length() - 1);

public static String getFileKey(String name){
String prefix = "/upload/" + DateKit.dateFormat(new Date(), "yyyy/MM");
String dir = upDir + prefix;
if (!FileKit.exist(dir)) {
new File(dir).mkdirs();
}
return prefix + "/" + com.blade.kit.UUID.UU32() + "." + FileKit.getExtension(name);
}
}

0 comments on commit ac7c462

Please sign in to comment.