summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-05-08 23:39:57 +0800
committerruki <[email protected]>2022-05-08 23:39:57 +0800
commit42ba6d2c3f3f13fc474e0f8a1c24ecd1240426ff (patch)
tree947b4cd9373df2198a7c105edfe7967fc4bd8fb5
parentc33af98d91f597ce330bc2aa5fe009bcd39425a9 (diff)
compress stream file
-rw-r--r--xmake/modules/private/service/stream.lua18
1 files changed, 18 insertions, 0 deletions
diff --git a/xmake/modules/private/service/stream.lua b/xmake/modules/private/service/stream.lua
index e6f7a5e99..221577ad6 100644
--- a/xmake/modules/private/service/stream.lua
+++ b/xmake/modules/private/service/stream.lua
@@ -166,9 +166,15 @@ function stream:send_file(filepath, opt)
-- send header
opt = opt or {}
local flags = 0
+ local tmpfile
if opt.compress then
flags = bit.bor(flags, HEADER_FLAG_COMPRESS_LZ4)
+ tmpfile = os.tmpfile()
+ lz4.compress_file(filepath, tmpfile)
+ filepath = tmpfile
end
+
+ -- send header
local size = os.filesize(filepath)
if not self:send_header(size, flags) then
return
@@ -190,6 +196,9 @@ function stream:send_file(filepath, opt)
end
file:close()
end
+ if tmpfile then
+ os.tryrm(tmpfile)
+ end
return ok
end
@@ -319,6 +328,11 @@ end
function stream:recv_file(filepath)
local size, flags = self:recv_header()
if size then
+ local dstfile
+ if bit.band(flags, HEADER_FLAG_COMPRESS_LZ4) == HEADER_FLAG_COMPRESS_LZ4 then
+ dstfile = filepath
+ filepath = os.tmpfile()
+ end
local buff = self._BUFF
local recv = 0
local file = io.open(filepath, "wb")
@@ -331,6 +345,10 @@ function stream:recv_file(filepath)
end
file:close()
if recv == size then
+ if dstfile then
+ lz4.decompress_file(filepath, dstfile)
+ os.tryrm(filepath)
+ end
return true
end
end