From 42ba6d2c3f3f13fc474e0f8a1c24ecd1240426ff Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 8 May 2022 23:39:57 +0800 Subject: compress stream file --- xmake/modules/private/service/stream.lua | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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 -- cgit v1.3.1