157 lines
4.1 KiB
Lua
157 lines
4.1 KiB
Lua
DBTextures = {}
|
|
local db = {}
|
|
|
|
local dbUploadStatus = {}
|
|
local isUploading = {}
|
|
local isDownoading = {}
|
|
local downloadCallback = {}
|
|
local downloadDir = Utl.chgToSDCard(Path.Combine(Application.persistentDataPath, CLPathCfg.self.basePath, "download"))
|
|
|
|
function DBTextures.init()
|
|
InvokeEx.cancelInvoke(DBTextures.releaseTimeout)
|
|
InvokeEx.invoke(DBTextures.releaseTimeout, 60)
|
|
end
|
|
|
|
function DBTextures.clean()
|
|
InvokeEx.cancelInvoke(DBTextures.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 DBTextures.releaseTimeout()
|
|
for k, v in ipairs(db) do
|
|
if DateEx.nowMS - v.lastUseTime > 300000 then
|
|
GameObject.DestroyImmediate(v.texture)
|
|
db[k] = nil
|
|
end
|
|
end
|
|
end
|
|
|
|
---@return UnityEngine.UnityWebRequest
|
|
function DBTextures.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 DBTextures.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 DBTextures.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 DBTextures.hadDownloaded(name)
|
|
local localPath = Path.Combine(downloadDir, name)
|
|
if File.Exists(localPath) then
|
|
return true, localPath
|
|
end
|
|
return false, localPath
|
|
end
|
|
|
|
function DBTextures.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 DBTextures
|