|
| 1 | +/* |
| 2 | + * Created by LuaView. |
| 3 | + * Copyright (c) 2017, Alibaba Group. All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT. |
| 6 | + * For the full copyright and license information,please view the LICENSE file in the root |
| 7 | + * directory of this source tree. |
| 8 | + */ |
| 9 | + |
| 10 | +package com.taobao.luaview.userdata.kit; |
| 11 | + |
| 12 | +import android.text.TextUtils; |
| 13 | + |
| 14 | +import com.taobao.luaview.fun.mapper.LuaViewApi; |
| 15 | +import com.taobao.luaview.fun.mapper.LuaViewLib; |
| 16 | +import com.taobao.luaview.global.LuaResourceFinder; |
| 17 | +import com.taobao.luaview.global.VmVersion; |
| 18 | +import com.taobao.luaview.scriptbundle.asynctask.SimpleTask1; |
| 19 | +import com.taobao.luaview.userdata.base.BaseLuaTable; |
| 20 | +import com.taobao.luaview.util.FileUtil; |
| 21 | +import com.taobao.luaview.util.LuaUtil; |
| 22 | + |
| 23 | +import org.luaj.vm2.Globals; |
| 24 | +import org.luaj.vm2.LuaString; |
| 25 | +import org.luaj.vm2.LuaValue; |
| 26 | +import org.luaj.vm2.Varargs; |
| 27 | +import org.luaj.vm2.lib.VarArgFunction; |
| 28 | + |
| 29 | +import java.io.File; |
| 30 | + |
| 31 | +import cn.com.venvy.App; |
| 32 | +import cn.com.venvy.common.utils.VenvyFileUtil; |
| 33 | +import cn.com.venvy.common.utils.VenvyIOUtils; |
| 34 | + |
| 35 | +/** |
| 36 | + * File 用户数据封装 |
| 37 | + * |
| 38 | + * @author song |
| 39 | + * @date 15/9/6 |
| 40 | + */ |
| 41 | +@LuaViewLib(revisions = {"20170306已对标"}) |
| 42 | +@LuaViewApi(since = VmVersion.V_550) |
| 43 | +public class UDFile extends BaseLuaTable { |
| 44 | + |
| 45 | + public UDFile(Globals globals, LuaValue metatable) { |
| 46 | + super(globals, metatable); |
| 47 | + init(); |
| 48 | + } |
| 49 | + |
| 50 | + private void init() { |
| 51 | + set("save", new save()); |
| 52 | + set("read", new read()); |
| 53 | + set("exists", new exists()); |
| 54 | + set("path", new path()); |
| 55 | + } |
| 56 | + |
| 57 | + private int fixIndex(Varargs args) { |
| 58 | + return args != null && args.arg1() instanceof UDFile ? 1 : 0; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * 保存文件 |
| 63 | + */ |
| 64 | + class save extends VarArgFunction { |
| 65 | + @Override |
| 66 | + public Varargs invoke(Varargs args) { |
| 67 | + final int fixIndex = fixIndex(args); |
| 68 | + if (args.narg() > fixIndex) { |
| 69 | + final LuaResourceFinder finder = getLuaResourceFinder(); |
| 70 | + if (finder != null) { |
| 71 | + final LuaValue param1 = args.arg(fixIndex + 1); |
| 72 | + final LuaValue param2 = args.arg(fixIndex + 2); |
| 73 | + |
| 74 | + String name = null; |
| 75 | + byte[] data = null; |
| 76 | + if (LuaUtil.isString(param1)) { |
| 77 | + name = param1.optjstring(null); |
| 78 | + |
| 79 | + if (param2 instanceof UDData) { |
| 80 | + data = ((UDData) param2).bytes(); |
| 81 | + } else if (param2 instanceof LuaString) { |
| 82 | + data = ((LuaString) param2).m_bytes; |
| 83 | + } else if (param2 instanceof CharSequence) { |
| 84 | + data = param2.toString().getBytes(); |
| 85 | + } |
| 86 | + |
| 87 | + } else if (param1 instanceof UDData) { |
| 88 | + data = ((UDData) param1).bytes(); |
| 89 | + name = param2.optjstring(null); |
| 90 | + } |
| 91 | + |
| 92 | + if (data != null && data.length > 0 && !TextUtils.isEmpty(name)) { |
| 93 | + if (args.isfunction(fixIndex + 3)) { |
| 94 | + final LuaValue callback = LuaUtil.getFunction(args, fixIndex + 3); |
| 95 | + new SimpleTask1<Boolean>() { |
| 96 | + @Override |
| 97 | + protected Boolean doInBackground(Object... params) { |
| 98 | + final String path = |
| 99 | + finder.buildSecurePathInSdcard((String) params[0]); |
| 100 | + return FileUtil.save(path, (byte[]) params[1]); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + protected void onPostExecute(Boolean o) { |
| 105 | + LuaUtil.callFunction(callback, o); |
| 106 | + } |
| 107 | + }.executeInPool(name, data); |
| 108 | + } else { |
| 109 | + final String path = finder.buildSecurePathInSdcard(name); |
| 110 | + return valueOf(FileUtil.save(path, data)); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + return FALSE; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + /** |
| 121 | + * 读取文件 |
| 122 | + */ |
| 123 | + class read extends VarArgFunction { |
| 124 | + @Override |
| 125 | + public Varargs invoke(Varargs args) { |
| 126 | + final int fixIndex = fixIndex(args); |
| 127 | + if (args.narg() > fixIndex) { |
| 128 | + final LuaResourceFinder finder = getLuaResourceFinder(); |
| 129 | + if (finder != null) { |
| 130 | + final String name = LuaUtil.getString(args, fixIndex + 1); |
| 131 | + final String path = finder.buildSecurePathInSdcard(name); |
| 132 | + |
| 133 | + if (args.isfunction(fixIndex + 2)) { |
| 134 | + final LuaValue callback = LuaUtil.getFunction(args, fixIndex + 2); |
| 135 | + if (path != null) { |
| 136 | + new SimpleTask1<UDData>() { |
| 137 | + @Override |
| 138 | + protected UDData doInBackground(Object... params) { |
| 139 | + byte[] data = VenvyFileUtil.readBytes(new File(path)); |
| 140 | + if (data != null) { |
| 141 | + return new UDData(getGlobals(), getmetatable(), null).append(data); |
| 142 | + } else { // 外存储卡读取不到的情况下,尝试在assets资源包下读取 |
| 143 | + data = VenvyIOUtils.toBytes(VenvyIOUtils.open(App.getContext(), |
| 144 | + name)); |
| 145 | + return new UDData(getGlobals(), getmetatable(), null).append(data); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + protected void onPostExecute(UDData udData) { |
| 151 | + LuaUtil.callFunction(callback, udData); |
| 152 | + } |
| 153 | + }.executeInPool(); |
| 154 | + } else { |
| 155 | + LuaUtil.callFunction(callback, NIL); |
| 156 | + } |
| 157 | + } else { |
| 158 | + if (path != null) { |
| 159 | + byte[] data = VenvyFileUtil.readBytes(new File(path)); |
| 160 | + if (data != null) { |
| 161 | + return new UDData(getGlobals(), getmetatable(), null).append(data); |
| 162 | + } else { // 外存储卡读取不到的情况下,尝试在assets资源包下读取 |
| 163 | + data = VenvyIOUtils.toBytes(VenvyIOUtils.open(App.getContext(), |
| 164 | + name)); |
| 165 | + if (data != null) { |
| 166 | + return new UDData(getGlobals(), getmetatable(), null).append(data); |
| 167 | + } else { |
| 168 | + return NIL; |
| 169 | + } |
| 170 | + } |
| 171 | + } else { |
| 172 | + return NIL; |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + return NIL; |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * 文件是否存在 |
| 183 | + */ |
| 184 | + class exists extends VarArgFunction { |
| 185 | + @Override |
| 186 | + public Varargs invoke(Varargs args) { |
| 187 | + final int fixIndex = fixIndex(args); |
| 188 | + if (args.narg() > fixIndex) { |
| 189 | + final String fileName = LuaUtil.getString(args, fixIndex + 1); |
| 190 | + return valueOf(VenvyFileUtil.isExistFile(fileName)); |
| 191 | + } |
| 192 | + return valueOf(false); |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * 获取文件绝对路径 |
| 198 | + */ |
| 199 | + class path extends VarArgFunction { |
| 200 | + @Override |
| 201 | + public Varargs invoke(Varargs args) { |
| 202 | + final int fixIndex = fixIndex(args); |
| 203 | + if (args.narg() > fixIndex) { |
| 204 | + final LuaResourceFinder finder = getLuaResourceFinder(); |
| 205 | + if (finder != null) { |
| 206 | + final String fileName = LuaUtil.getString(args, fixIndex + 1); |
| 207 | + final String path = finder.buildSecurePathInSdcard(fileName); |
| 208 | + return path != null ? valueOf(path) : NIL; |
| 209 | + } |
| 210 | + } |
| 211 | + return NIL; |
| 212 | + } |
| 213 | + } |
| 214 | +} |
0 commit comments