modify
This commit is contained in:
174
Assets/trCRM/upgradeRes4Dev/priority/lua/db/DBAttachment.lua
Normal file
174
Assets/trCRM/upgradeRes4Dev/priority/lua/db/DBAttachment.lua
Normal file
@@ -0,0 +1,174 @@
|
||||
DBAttachment = {} -- 附件相关
|
||||
local db = {}
|
||||
|
||||
local dbUploadStatus = {}
|
||||
local isUploading = {}
|
||||
local isDownoading = {}
|
||||
local downloadCallback = {}
|
||||
local downloadDir = Utl.chgToSDCard(Path.Combine(Application.persistentDataPath, CLPathCfg.self.basePath, "download"))
|
||||
local _TimeOutSec = 6 * 60 * 60 * 1000
|
||||
local invokeSec = 60
|
||||
|
||||
function DBAttachment.init()
|
||||
InvokeEx.cancelInvoke(DBAttachment.releaseTimeout)
|
||||
InvokeEx.invoke(DBAttachment.releaseTimeout, invokeSec)
|
||||
end
|
||||
|
||||
function DBAttachment.clean()
|
||||
InvokeEx.cancelInvoke(DBAttachment.releaseTimeout)
|
||||
for k, v in ipairs(db) do
|
||||
GameObject.DestroyImmediate(v)
|
||||
end
|
||||
db = {}
|
||||
|
||||
---@param v UnityEngine.UnityWebRequest
|
||||
for k, www in pairs(dbUploadStatus) do
|
||||
if www then
|
||||
www.Dispose()
|
||||
www = nil
|
||||
end
|
||||
end
|
||||
dbUploadStatus = {}
|
||||
end
|
||||
|
||||
function DBAttachment.releaseTimeout()
|
||||
for k, v in ipairs(db) do
|
||||
if DateEx.nowMS - v.lastUseTime > 3000000 then
|
||||
GameObject.DestroyImmediate(v.texture)
|
||||
db[k] = nil
|
||||
end
|
||||
end
|
||||
|
||||
-- 删掉超时的文件
|
||||
local files = FileEx.GetFiles(downloadDir)
|
||||
local count = files.Length
|
||||
for i = 0, count - 1 do
|
||||
---@type System.IO.FileInfo
|
||||
local fi = FileInfo(files[i])
|
||||
if fi then
|
||||
local diff = (DateTime.Now:ToFileTime() - fi.LastAccessTime:ToFileTime()) / 10000
|
||||
if diff > _TimeOutSec then
|
||||
FileEx.Delete(files[i])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
InvokeEx.invoke(DBAttachment.releaseTimeout, invokeSec)
|
||||
end
|
||||
|
||||
---@return UnityEngine.UnityWebRequest
|
||||
function DBAttachment.getByUrl(url, callback, orgs)
|
||||
local tt = db[url]
|
||||
if tt then
|
||||
tt.lastUseTime = DateEx.nowMS
|
||||
db[url] = tt
|
||||
Utl.doCallback(callback, tt.texture, orgs)
|
||||
return nil
|
||||
end
|
||||
|
||||
local request =
|
||||
WWWEx.get(
|
||||
url,
|
||||
nil,
|
||||
CLAssetType.texture,
|
||||
function(content)
|
||||
db[url] = {texture = content, lastUseTime = DateEx.nowMS}
|
||||
Utl.doCallback(callback, content, orgs)
|
||||
end,
|
||||
nil,
|
||||
orgs,
|
||||
true,
|
||||
1
|
||||
)
|
||||
return request
|
||||
end
|
||||
|
||||
function DBAttachment.upload(path, uploadPath, finishCallback)
|
||||
local key = joinStr(path, "_", uploadPath)
|
||||
if (not dbUploadStatus[key]) and (not isUploading[key]) then
|
||||
local www =
|
||||
NetProto.uploadFile(
|
||||
path,
|
||||
uploadPath,
|
||||
function(result, orgs)
|
||||
isUploading[key] = nil
|
||||
if result then
|
||||
dbUploadStatus[key] = result
|
||||
end
|
||||
Utl.doCallback(finishCallback, result)
|
||||
end
|
||||
)
|
||||
isUploading[key] = www
|
||||
return www
|
||||
else
|
||||
if dbUploadStatus[key] then
|
||||
Utl.doCallback(finishCallback, dbUploadStatus[key])
|
||||
else
|
||||
printe("进到这里来了,有问题!!!!!!!")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function DBAttachment.cancelUpload(path, uploadPath)
|
||||
local key = joinStr(path, "_", uploadPath)
|
||||
local www = isUploading[key]
|
||||
if www then
|
||||
www.Dispose()
|
||||
www = nil
|
||||
dbUploadStatus[key] = nil
|
||||
isUploading[key] = nil
|
||||
end
|
||||
end
|
||||
|
||||
function DBAttachment.hadDownloaded(name)
|
||||
local localPath = Path.Combine(downloadDir, name)
|
||||
if File.Exists(localPath) then
|
||||
return true, localPath
|
||||
end
|
||||
return false, localPath
|
||||
end
|
||||
|
||||
function DBAttachment.download(name, url, callback)
|
||||
downloadCallback[name] = callback
|
||||
if isDownoading[name] then
|
||||
return isDownoading[name]
|
||||
end
|
||||
local localPath = Path.Combine(downloadDir, name)
|
||||
local _url = url
|
||||
if File.Exists(localPath) then
|
||||
_url = Path.Combine("file://", localPath)
|
||||
end
|
||||
local www =
|
||||
WWWEx.get(
|
||||
_url,
|
||||
nil,
|
||||
CLAssetType.bytes,
|
||||
function(content, orgs)
|
||||
isDownoading[name] = nil
|
||||
if content then
|
||||
local bytes = content
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(localPath))
|
||||
File.WriteAllBytes(localPath, bytes)
|
||||
Utl.doCallback(downloadCallback[name], content, localPath)
|
||||
downloadCallback[name] = nil
|
||||
else
|
||||
MyUtl.toastW("下载附件为空")
|
||||
Utl.doCallback(downloadCallback[name], content, nil)
|
||||
downloadCallback[name] = nil
|
||||
end
|
||||
end,
|
||||
function()
|
||||
isDownoading[name] = nil
|
||||
MyUtl.toastW("下载附件失败")
|
||||
Utl.doCallback(downloadCallback[name], nil, nil)
|
||||
downloadCallback[name] = nil
|
||||
end,
|
||||
nil,
|
||||
true,
|
||||
2
|
||||
)
|
||||
isDownoading[name] = www
|
||||
return www
|
||||
end
|
||||
|
||||
return DBAttachment
|
||||
Reference in New Issue
Block a user