Skip to content

Commit

Permalink
重构多部份内容 (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
scx567888 authored Sep 29, 2024
1 parent 2943f05 commit 0637235
Show file tree
Hide file tree
Showing 32 changed files with 581 additions and 259 deletions.
14 changes: 14 additions & 0 deletions scx-common/src/main/java/cool/scx/common/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.file.*;
Expand Down Expand Up @@ -214,4 +215,17 @@ public static void merge(Path source, Path target) throws IOException {
}
}

public static void appendToFile(Path target, InputStream appendContent) throws IOException {
try (var in = appendContent) {
try (var out = Files.newOutputStream(target, APPEND, CREATE, SYNC, WRITE)) {
in.transferTo(out);
} catch (NoSuchFileException e) {
Files.createDirectories(target.getParent());
try (var out = Files.newOutputStream(target, APPEND, CREATE, SYNC, WRITE);) {
in.transferTo(out);
}
}
}
}

}
69 changes: 37 additions & 32 deletions scx-core/src/test/java/cool/scx/core/test/TestModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
import cool.scx.core.test.person.Person;
import cool.scx.core.test.person.PersonService;
import cool.scx.data.query.QueryOption;
import cool.scx.http.routing.handler.StaticHandler;
import cool.scx.jdbc.sql.SQL;
//import org.springframework.scheduling.support.CronTrigger;
import cool.scx.scheduling.ScxScheduling;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

Expand All @@ -32,12 +33,9 @@
import java.util.Arrays;
import java.util.List;

import static cool.scx.core.eventbus.ZeroCopyMessageCodec.ZERO_COPY_CODEC_NAME;
import static cool.scx.core.eventbus.ZeroCopyMessageWrapper.zeroCopyMessage;
import static cool.scx.data.field_filter.FieldFilterBuilder.ofIncluded;
import static cool.scx.data.query.QueryBuilder.*;
import static java.lang.System.Logger.Level.ERROR;
import static org.testng.Assert.assertEquals;

public class TestModule extends ScxModule {

Expand Down Expand Up @@ -151,7 +149,6 @@ public static void test1() throws SocketException {
//测试 URIBuilder
for (int i = 0; i < 1000; i = i + 1) {
var s = "http://" + ip.getHostAddress() + ":8888/test0";
// todo
// try {
// var stringHttpResponse = ScxHttpClientHelper.post(
// URIBuilder.of(s)
Expand Down Expand Up @@ -179,11 +176,11 @@ public static void test2() {
// ScxContext.eventBus().request("test-event-bus", car, new DeliveryOptions().setCodecName(ZERO_COPY_CODEC_NAME), c -> {
// assertEquals(c.result().body(), car);
// });
// 通过指定 ZERO_COPY_CODEC_NAME 实现 0 拷贝
// //通过指定 ZERO_COPY_CODEC_NAME 实现 0 拷贝
// ScxContext.eventBus().send("test-event-bus", car, new DeliveryOptions().setCodecName(ZERO_COPY_CODEC_NAME));
// 通过 @ZeroCopyMessage 注解实现 零拷贝
// //通过 @ZeroCopyMessage 注解实现 零拷贝
// ScxContext.eventBus().publish("test-event-bus", car);
// 通过 zeroCopyMessage() 包装器实现 零拷贝 (会自动脱壳)
// //通过 zeroCopyMessage() 包装器实现 零拷贝 (会自动脱壳)
// ScxContext.eventBus().send("test-event-bus", zeroCopyMessage(car));
}

Expand Down Expand Up @@ -244,30 +241,38 @@ public static void test4() {
*/
@Override
public void start(Scx scx) {
//todo
// scx.scxHttpRouter().route("/static/*")
// .handler(StaticHandler.create(FileSystemAccess.ROOT, scx.scxEnvironment().getPathByAppRoot("AppRoot:c\\static").toString())
// .setFilesReadOnly(false));
// var logger = System.getLogger(TestModule.class.getName());
// //测试定时任务
// scx.scxScheduler().scheduleAtFixedRate((a) -> {
// //测试
// logger.log(ERROR, "这是通过 ScxContext.scheduleAtFixedRate() 打印的 : 一共 10 次 , 这时第 " + a.runCount() + " 次执行 !!!");
// }, Instant.now().plusSeconds(3), Duration.of(1, ChronoUnit.SECONDS), 10);
//
// scx.scxScheduler().schedule((a) -> {
// //测试
// logger.log(ERROR, "这是通过 ScxContext.scheduler() 使用 Cron 表达式 打印的 : 这时第 " + a.runCount() + " 次执行 !!!");
// }, new CronTrigger("*/1 * * * * ?"));
//
// scx.scxScheduler().scheduleAtFixedRate((a) -> {
// logger.log(ERROR, "这是通过 ScxContext.scheduleAtFixedRate() 打印的 : 不限次数 不过到 第 10 次手动取消 , 这是第 " + a.runCount() + " 次执行 !!!");
// if (a.runCount() >= 10) {
// a.scheduledFuture().cancel(false);
// }
// }, Instant.now().plusSeconds(3), Duration.of(1, ChronoUnit.SECONDS));
//
// System.out.println("CarModule-Start");
scx.scxHttpRouter().route()
.path("/static/*")
.handler(new StaticHandler(scx.scxEnvironment().getPathByAppRoot("AppRoot:c\\static")));
var logger = System.getLogger(TestModule.class.getName());
//测试定时任务
ScxScheduling.fixedRate()
.startTime(Instant.now().plusSeconds(3))
.delay(Duration.of(1, ChronoUnit.SECONDS))
.maxRunCount(10)
.start((a) -> {
//测试
logger.log(ERROR, "这是通过 ScxContext.scheduleAtFixedRate() 打印的 : 一共 10 次 , 这时第 " + a.runCount() + " 次执行 !!!");
});

ScxScheduling.cron()
.expression("*/1 * * * * ?")
.start((a) -> {
//测试
logger.log(ERROR, "这是通过 ScxContext.scheduler() 使用 Cron 表达式 打印的 : 这时第 " + a.runCount() + " 次执行 !!!");
});

ScxScheduling.fixedRate()
.startTime(Instant.now().plusSeconds(3))
.delay(Duration.of(1, ChronoUnit.SECONDS))
.start((a) -> {
logger.log(ERROR, "这是通过 ScxContext.scheduleAtFixedRate() 打印的 : 不限次数 不过到 第 10 次手动取消 , 这是第 " + a.runCount() + " 次执行 !!!");
if (a.runCount() >= 10) {
a.cancel();
}
});

System.out.println("CarModule-Start");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public static Object test0(@FromQuery String name,
return Map.of("now", yyyy_MM_dd_HH_mm_ss.format(LocalDateTime.now()),
"name", name,
"age", age,
"content", new String(content.content(), StandardCharsets.UTF_8),
"content1", new String(content1.content(), StandardCharsets.UTF_8));
"content", content.asString(),
"content1", content.asString());
}

@ScxRoute(methods = HttpMethod.GET)
Expand Down
8 changes: 4 additions & 4 deletions scx-ext/src/main/java/cool/scx/ext/fss/FSSController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import cool.scx.http.HttpMethod;
import cool.scx.http.exception.InternalServerErrorException;
import cool.scx.http.exception.NotFoundException;
import cool.scx.http.media.multi_part.CachedMultiPartPart;
import cool.scx.http.media.multi_part.MultiPartPart;
import cool.scx.web.annotation.*;
import cool.scx.web.vo.*;

Expand Down Expand Up @@ -77,15 +77,15 @@ public BaseVo upload(
@FromBody String fileHash,// 文件 Hash
@FromBody Integer chunkLength,//分片总长度
@FromBody Integer nowChunkIndex,//当前分片
@FromUpload CachedMultiPartPart fileData//文件内容
@FromUpload MultiPartPart fileData//文件内容
) throws Exception {
var uploadTempFile = getUploadTempPath(fileHash).resolve("scx_fss.temp");
var uploadConfigFile = uploadTempFile.resolveSibling("scx_fss.upload_state");

//判断是否上传的是最后一个分块 (因为 索引是以 0 开头的所以这里 -1)
if (nowChunkIndex == chunkLength - 1) {
//先将数据写入临时文件中
FileUtils.merge(uploadTempFile, fileData.contentPath());
FileUtils.appendToFile(uploadTempFile, fileData.inputStream());
//获取文件描述信息创建 fssObject 对象
var newFSSObject = createFSSObjectByFileInfo(fileName, fileSize, fileHash);
//获取文件真实的存储路径
Expand Down Expand Up @@ -113,7 +113,7 @@ public BaseVo upload(
var needUploadChunkIndex = lastUploadChunk + 1;
//当前的区块索引和需要的区块索引相同 就保存文件内容
if (nowChunkIndex.equals(needUploadChunkIndex)) {
FileUtils.merge(uploadTempFile, fileData.contentPath());
FileUtils.appendToFile(uploadTempFile, fileData.inputStream());
//将当前上传成功的区块索引和总区块长度保存到配置文件中
updateLastUploadChunk(uploadConfigFile, nowChunkIndex, chunkLength);
//像前台返回我们需要的下一个区块索引
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import cool.scx.http.HttpMethod;
import cool.scx.http.ScxHttpClientRequestBase;
import cool.scx.http.ScxHttpClientResponse;
import cool.scx.http.ScxHttpHeaders;
import cool.scx.http.media.MediaWriter;
import io.helidon.http.HeaderNames;
import io.helidon.http.Method;
import io.helidon.webclient.api.WebClient;
Expand All @@ -20,25 +21,15 @@ public HelidonHttpClientRequest(WebClient webClient) {
}

@Override
public HelidonHttpClientResponse send() {
public HelidonHttpClientResponse send(MediaWriter writer) {
var r = webClient.method(Method.create(method.value()));
r.uri(uri.toString());
writer.beforeWrite(headers, ScxHttpHeaders.of());
for (var h : headers) {
r.header(HeaderNames.create(h.getKey().value()), h.getValue());
}
var res = r.request();
return new HelidonHttpClientResponse(res);
}

@Override
public ScxHttpClientResponse send(Object body) {
var r = webClient.method(Method.create(method.value()));
r.uri(uri.toString());
for (var h : headers) {
r.header(HeaderNames.create(h.getKey().value()), h.getValue());
}
var res = r.submit(body);
return new HelidonHttpClientResponse(res);
var httpClientResponse = r.outputStream(writer::write);
return new HelidonHttpClientResponse(httpClientResponse);
}

}
4 changes: 2 additions & 2 deletions scx-http/src/main/java/cool/scx/http/HttpHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ public static String getDownloadContentDisposition(String downloadName) {
return "attachment; filename*=utf-8''" + encode(downloadName, UTF_8).replace("+", "%20");
}

public static MediaType getMediaTypeFromExtension(String ext) {
public static MediaType getMediaTypeByExtension(String ext) {
var fileFormat = FileFormat.ofExtension(ext);
return fileFormat != null ? fileFormat.mediaType() : null;
}

public static MediaType getMediaTypeFromFileName(String filename) {
public static MediaType getMediaTypeByFileName(String filename) {
var fileFormat = FileFormat.ofFileName(filename);
return fileFormat != null ? fileFormat.mediaType() : null;
}
Expand Down
15 changes: 7 additions & 8 deletions scx-http/src/main/java/cool/scx/http/ScxHttpBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import cool.scx.http.media.MediaReader;
import cool.scx.http.media.form_params.FormParams;
import cool.scx.http.media.multi_part.CachedMultiPart;
import cool.scx.http.media.multi_part.CachedMultiPartReader;
import cool.scx.http.media.multi_part.MultiPart;
import cool.scx.http.media.multi_part.MultiPartStreamCachedReader;
import cool.scx.http.media.path.PathReader;
import cool.scx.http.media.string.StringReader;

Expand All @@ -15,8 +14,8 @@

import static cool.scx.http.media.byte_array.ByteArrayReader.BYTE_ARRAY_READER;
import static cool.scx.http.media.form_params.FormParamsReader.FORM_PARAMS_READER;
import static cool.scx.http.media.multi_part.CachedMultiPartReader.CACHED_MULTI_PART_READER;
import static cool.scx.http.media.multi_part.MultiPartReader.MULTI_PART_READER;
import static cool.scx.http.media.multi_part.MultiPartStreamCachedReader.MULTI_PART_READER_CACHED;
import static cool.scx.http.media.multi_part.MultiPartStreamReader.MULTI_PART_READER;
import static cool.scx.http.media.string.StringReader.STRING_READER;

/**
Expand Down Expand Up @@ -48,12 +47,12 @@ default MultiPart asMultiPart() {
return as(MULTI_PART_READER);
}

default CachedMultiPart asCachedMultiPart() {
return as(CACHED_MULTI_PART_READER);
default MultiPart asMultiPartCached() {
return as(MULTI_PART_READER_CACHED);
}

default CachedMultiPart asCachedMultiPart(Path cachePath) {
return as(new CachedMultiPartReader(cachePath));
default MultiPart asMultiPartCached(Path cachePath) {
return as(new MultiPartStreamCachedReader(cachePath));
}

default Path asPath(Path path, OpenOption... options) {
Expand Down
45 changes: 43 additions & 2 deletions scx-http/src/main/java/cool/scx/http/ScxHttpClientRequest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
package cool.scx.http;

import cool.scx.http.media.MediaWriter;
import cool.scx.http.media.byte_array.ByteArrayWriter;
import cool.scx.http.media.input_stream.InputStreamWriter;
import cool.scx.http.media.multi_part.MultiPart;
import cool.scx.http.media.multi_part.MultiPartWriter;
import cool.scx.http.media.path.PathWriter;
import cool.scx.http.media.string.StringWriter;
import cool.scx.http.uri.ScxURI;
import cool.scx.http.uri.ScxURIWritable;

import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Path;

/**
* ScxHttpClientRequest
*/
Expand All @@ -20,9 +31,39 @@ public interface ScxHttpClientRequest {

ScxHttpClientRequest headers(ScxHttpHeadersWritable headers);

ScxHttpClientResponse send();
ScxHttpClientResponse send(MediaWriter writer);

default ScxHttpClientResponse send() {
return send(new byte[]{});
}

default ScxHttpClientResponse send(byte[] bytes) {
return send(new ByteArrayWriter(bytes));
}

default ScxHttpClientResponse send(String str) {
return send(new StringWriter(str));
}

default ScxHttpClientResponse send(String str, Charset charset) {
return send(new StringWriter(str, charset));
}

default ScxHttpClientResponse send(Path path) {
return send(new PathWriter(path));
}

default ScxHttpClientResponse send(Path path, long offset, long length) {
return send(new PathWriter(path, offset, length));
}

default ScxHttpClientResponse send(InputStream inputStream) {
return send(new InputStreamWriter(inputStream));
}

ScxHttpClientResponse send(Object body);
default ScxHttpClientResponse send(MultiPart multiPart) {
return send(new MultiPartWriter(multiPart));
}

default ScxHttpClientRequest uri(String uri) {
return uri(ScxURI.of(uri));
Expand Down
4 changes: 4 additions & 0 deletions scx-http/src/main/java/cool/scx/http/ScxHttpHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,8 @@ default Cookie getSetCookie(String name) {
return setCookies().get(name);
}

default String encode() {
return ScxHttpHeadersHelper.encodeHeaders(this);
}

}
12 changes: 12 additions & 0 deletions scx-http/src/main/java/cool/scx/http/ScxHttpHeadersHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,16 @@ public static ScxHttpHeadersWritable parseHeaders(String headersStr) {
return headers;
}

public static String encodeHeaders(ScxHttpHeaders headers) {
var sb = new StringBuilder();
for (var header : headers) {
var key = header.getKey();
var values = header.getValue();
for (var value : values) {
sb.append(key.value()).append(": ").append(value).append("\r\n");
}
}
return sb.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ default String readDate() {
return params().get("read-date");
}

default String size() {
return params().get("size");
default Long size() {
var size = params().get("size");
return size != null ? Long.valueOf(size) : null;
}

String encode();
Expand Down
Loading

0 comments on commit 0637235

Please sign in to comment.