Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fileserver #26

Merged
merged 9 commits into from
Oct 22, 2024
1 change: 1 addition & 0 deletions muyun-boot/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies {
implementation(project(":muyun-platform"))
implementation(project(":muyun-proxy"))
implementation(project(":muyun-log"))
implementation(project(":muyun-fileserver"))

testImplementation("io.quarkus:quarkus-junit5")
testImplementation("io.rest-assured:rest-assured")
Expand Down
3 changes: 3 additions & 0 deletions muyun-boot/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
muyun:
debug: true
super-user-id: 1
fileserver:
Ning19230223 marked this conversation as resolved.
Show resolved Hide resolved
upload-path: D:\\file-uploads\\
Ning19230223 marked this conversation as resolved.
Show resolved Hide resolved
page-path: fileServer
web:
redirects:
- from: /
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package net.ximatai.muyun.test.fileserver;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.Response;
import io.vertx.core.json.JsonObject;
import net.ximatai.muyun.test.testcontainers.PostgresTestResource;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;

@QuarkusTest
@QuarkusTestResource(value = PostgresTestResource.class, restrictToAnnotatedClass = true)
public class TestFileServer {

// 文件名
String fileName;
String uid;
// 文件内容
String fileContent = "";
// 临时文件
File tempFile;

@BeforeEach
public void setup() throws IOException {
int fileNameInt = getRandomInt();
fileName = String.valueOf(fileNameInt);
tempFile = File.createTempFile(fileName, ".txt");
FileOutputStream fos = new FileOutputStream(tempFile);
int ctx1 = getRandomInt();
fileContent += String.valueOf(ctx1 + "\n");
int ctx2 = getRandomInt();
fileContent += String.valueOf(ctx2);
fos.write(fileContent.getBytes());
fos.close();
}

@Test
void testFileProcess() {
// 文件上传
Response response = given()
.multiPart("file", tempFile)
.when()
.post("/fileServer/form")
.then()
.log().all()
.statusCode(200)
.extract()
.response();

uid = response.getBody().asString();

// 下载文件
Response response2 = given()
.when()
.get("/fileServer/download/" + uid)
.then()
.log().all()
.statusCode(200)
.extract()
.response();

String downloadContent = response2.getBody().asString();
// 验证文件内容是否相同
assertEquals(fileContent, downloadContent);

// 读取文件info
Response response3 = given()
.when()
.get("/fileServer/info/" + uid)
.then()
.log().all()
.statusCode(200)
.extract()
.response();
String jsonResponse = response3.getBody().asString();
JsonObject jsonObject = new JsonObject(jsonResponse);
String fileName = jsonObject.getString("name");
assertEquals(tempFile.getName(), fileName);
}

@AfterEach
public void tearDown() {
String deleteUrl = "/fileServer/delete/" + uid;
given()
.when()
.get(deleteUrl)
.then()
.statusCode(200);
tempFile.delete();
}

/**
* @return 返回一个随机的正数
*/
int getRandomInt() {
Random rand = new Random();
int num = rand.nextInt();
while (num < 1000000000) {
num = rand.nextInt();
}
return num;
}
}
12 changes: 12 additions & 0 deletions muyun-fileserver/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
plugins {
java
`java-library`
}

dependencies {
api(enforcedPlatform(libs.quarkus.platform.bom))
api("io.quarkus:quarkus-rest")
api("io.quarkus:quarkus-arc")
api("io.quarkus:quarkus-vertx")
api("io.quarkus:quarkus-reactive-routes")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package net.ximatai.muyun.fileserver;

public class FileInfoEntity {
String name;
long size;
String suffix;
String uid;
String time;

public FileInfoEntity() {
}

public FileInfoEntity(String name, long size, String suffix, String uid, String time) {
this.name = name;
this.size = size;
this.suffix = suffix;
this.uid = uid;
this.time = time;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public long getSize() {
return size;
}

public void setSize(long size) {
this.size = size;
}

public String getSuffix() {
return suffix;
}

public void setSuffix(String suffix) {
this.suffix = suffix;
}

public String getUid() {
return uid;
}

public void setUid(String uid) {
this.uid = uid;
}

public String getTime() {
return time;
}

public void setTime(String time) {
this.time = time;
}
}
Loading
Loading