Compare commits

...

5 Commits
master ... gxhy

Author SHA1 Message Date
lyf666 c8b94228b2 Merge remote-tracking branch 'remotes/origin/master' into gxhy
# Conflicts:
#	src/main/java/cn/cloudowr/attach/Config.java
2018-03-02 10:48:57 +08:00
lyf666 8f03fe9fb4 . 2017-05-04 23:31:36 +08:00
lyf666 6506e2f25a . 2017-05-04 23:19:38 +08:00
lyf666 4ab67e0413 . 2017-05-04 23:14:55 +08:00
lyf666 0ac09ec7f7 . 2017-05-04 22:40:05 +08:00
3 changed files with 1 additions and 219 deletions

View File

@ -7,7 +7,6 @@
<groupId>cn.cloudowr</groupId>
<artifactId>attach</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<plugins>
<plugin>

View File

@ -65,7 +65,7 @@ public class Config extends JFinalConfig{
public void afterJFinalStart() {
MongoClientURI connectionString = new MongoClientURI("mongodb://root:CoWR1111@dds-wz9ceed1f19591041.mongodb.rds.aliyuncs.com:3717,dds-wz9ceed1f19591042.mongodb.rds.aliyuncs.com:3717/admin?replicaSet=mgset-5320915");
MongoClient mongoClient = new MongoClient(connectionString);
mongoDatabase = mongoClient.getDatabase("dict");
mongoDatabase = mongoClient.getDatabase("gxhy");
String endpoint = "oss-cn-shenzhen.aliyuncs.com";
String accessKeyId = "LTAI7tcTTMuROink";

View File

@ -1,217 +0,0 @@
package cn.cloudowr.attach;
import cn.cloudowr.sdk.FastJSONUtil;
import cn.cloudowr.sdk.IdInterceptor;
import com.aliyun.oss.model.UploadFileRequest;
import com.aliyun.oss.model.UploadFileResult;
import com.jfinal.aop.Before;
import com.jfinal.upload.UploadFile;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.model.Updates;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**
* Created by lyf66 on 2017/2/17.
*/
public class Controller extends com.jfinal.core.Controller {
public void ping() {
renderText("pong");
}
public void upload() throws Throwable {
UploadFile file = getFile();
String json = getPara("json", null);
Document doc = null;
if (json != null) {
try {
doc = Document.parse(json);
} catch (Exception e) {
renderError(400);
return;
}
}
if (doc == null) {
doc = new Document();
}
String key = UUID.randomUUID().toString().replace("-", "");
UploadFileRequest uploadFileRequest = new UploadFileRequest(Config.BukketName, key);
uploadFileRequest.setUploadFile(file.getUploadPath() + File.separator + file.getFileName());
uploadFileRequest.setTaskNum(5);
uploadFileRequest.setPartSize(1 * 1024 * 1024);
uploadFileRequest.setEnableCheckpoint(true);
UploadFileResult uploadFileResult = Config.getOssClient().uploadFile(uploadFileRequest);
uploadFileResult.getMultipartUploadResult();
MongoCollection<Document> collection = Config.getMongoDatabase().getCollection("attach");
doc.put("ossurl", Config.OssUrl + key);
doc.put("filename", file.getFileName());
collection.insertOne(doc);
file.getFile().delete();
// renderJson(doc.toJson());
renderJson(FastJSONUtil.bsonToJSON(doc));
}
public void download() throws Throwable {
String id = getPara("id");
if (id == null || id.isEmpty()) {
renderError(404);
return;
}
MongoCollection<Document> collection = Config.getMongoDatabase().getCollection("attach");
Document doc = collection.find(Filters.eq("_id", new ObjectId(id))).first();
renderJson("url", doc.getString("ossurl"));
}
public void attach() {
Map<String, String[]> paraMap = getParaMap();
List<Bson> ops = new ArrayList<>();
paraMap.forEach((key, arr) -> {
if ("id".equals(key)) {
ops.add(Filters.eq("_id", new ObjectId(arr[0])));
} else {
ops.add(Filters.eq(key, arr[0]));
}
});
MongoCollection<Document> collection = Config.getMongoDatabase().getCollection("attach");
List<Document> documents = new ArrayList<>();
collection.find(
Filters.and(ops)
).into(documents);
// List<String> json = new ArrayList<>();
// documents.forEach(doc -> {
// json.add(doc.toJson());
// });
// renderText(Arrays.toString(json.toArray()), ContentType.JSON);
renderJson(FastJSONUtil.bsonToJSON(documents));
}
public void attachList() {
int pageNum = getParaToInt("pageNum", 0);
int pageSize = getParaToInt("pageSize", 20);
MongoCollection<Document> collection = getCollection();
List<Document> documents = new ArrayList<>();
collection.find().limit(pageSize).skip(pageNum * pageSize).into(documents);
// List<String> json = new ArrayList<>();
// documents.forEach(doc -> {
// json.add(doc.toJson());
// });
// renderText(Arrays.toString(json.toArray()), ContentType.JSON);
renderJson(FastJSONUtil.bsonToJSON(documents));
}
public void addAttach() {
Map<String, String[]> paraMap = getParaMap();
Document attach = new Document();
paraMap.forEach((key, arr) -> {
if (arr.length == 1) {
attach.put(key, arr[0]);
} else if (arr.length > 1) {
List<String> s = new ArrayList<>();
for (String s1 : arr) {
s.add(s1);
}
attach.put(key, s);
}
});
getCollection().insertOne(attach);
// renderJson(attach.toJson());
renderJson(FastJSONUtil.bsonToJSON(attach));
}
@Before(IdInterceptor.class)
public void updateAttach() {
String id = getPara("id");
String action = getPara("action");
String[] keysToUnset = getParaValues("keys");
List<Bson> ops = new ArrayList<>();
if ("unset".equals(action)) {
for (String key : keysToUnset) {
if (key.equals("id")) continue;
ops.add(Updates.unset(key));
}
} else {
Map<String, String[]> paraMap = getParaMap();
paraMap.forEach((key, arr) -> {
if (!"id".equals(key) && !"action".equals(key)) {
if (arr.length == 1) {
ops.add(Updates.set(key, arr[0]));
} else if (arr.length > 1) {
List<String> s = new ArrayList<>();
for (String s1 : arr) {
s.add(s1);
}
ops.add(Updates.set(key, s));
}
}
});
}
UpdateResult result = getCollection().updateOne(
Filters.eq("_id", new ObjectId(id)),
Updates.combine(
ops
),
new UpdateOptions().upsert(true)
);
renderJson("result", result.getMatchedCount() >= 1);
}
@Before(IdInterceptor.class)
public void deleteAttach() {
String id = getPara("id");
DeleteResult result = getCollection().deleteOne(
Filters.eq("_id", new ObjectId(id))
);
renderJson("result", result.getDeletedCount() == 1);
}
private MongoCollection<Document> getCollection() {
MongoDatabase mongoDataBase = Config.getMongoDatabase();
MongoCollection<Document> collection = mongoDataBase.getCollection("attach");
return collection;
}
private static String getPrefix(String filename) {
int lastIndexOf = filename.lastIndexOf(".");
if (lastIndexOf == -1) {
return filename;
} else {
return filename.substring(0, lastIndexOf);
}
}
private static String getSuffix(String filename) {
int lastIndexOf = filename.lastIndexOf(".");
if (lastIndexOf == -1) {
return "";
} else {
return filename.substring(lastIndexOf + 1, filename.length());
}
}
}