fix:ASW-99 调整 xapk 校验
This commit is contained in:
@@ -25,15 +25,10 @@ import org.springframework.core.io.Resource;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.zip.ZipEntry;
|
|
||||||
import java.util.zip.ZipFile;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class PackageServiceImpl extends ServiceImpl<PackageDao, PackageEntity> implements IPackageService {
|
public class PackageServiceImpl extends ServiceImpl<PackageDao, PackageEntity> implements IPackageService {
|
||||||
@@ -98,27 +93,12 @@ public class PackageServiceImpl extends ServiceImpl<PackageDao, PackageEntity> i
|
|||||||
entity.setVersion(apkInfo.getVersionName());
|
entity.setVersion(apkInfo.getVersionName());
|
||||||
entity.setIdentifier(apkInfo.getPackageName());
|
entity.setIdentifier(apkInfo.getPackageName());
|
||||||
} else {
|
} else {
|
||||||
ZipFile zipFile = new ZipFile(destination);
|
ApkInfo apkInfo = apkUtil.parseXapk(destination.getPath());
|
||||||
ZipEntry entry = zipFile.getEntry("manifest.json");
|
if (T.ObjectUtil.isNull(apkInfo)) {
|
||||||
InputStream inputStream = zipFile.getInputStream(entry);
|
|
||||||
StringBuilder manifestJson = new StringBuilder();
|
|
||||||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
|
|
||||||
String line;
|
|
||||||
while ((line = reader.readLine()) != null) {
|
|
||||||
manifestJson.append(line).append("\n");
|
|
||||||
}
|
|
||||||
Map manifest = T.JSONUtil.toBean(manifestJson.toString(), Map.class);
|
|
||||||
ZipEntry packageFile = zipFile.getEntry(T.StrUtil.concat(true,T.MapUtil.getStr(manifest, "package_name"),".apk"));
|
|
||||||
File temp = T.FileUtil.file(Constants.TEMP_PATH, packageFile.getName());
|
|
||||||
FileUtil.writeBytes(zipFile.getInputStream(packageFile).readAllBytes(), temp);
|
|
||||||
ApkInfo apkInfo = apkUtil.parseApk(temp.getPath());
|
|
||||||
String versionName = apkInfo.getVersionName();
|
|
||||||
String packageName = apkInfo.getPackageName();
|
|
||||||
if (!T.BooleanUtil.and(versionName.equals(T.MapUtil.getStr(manifest, "version_name")), packageName.equals(T.MapUtil.getStr(manifest, "package_name")))){
|
|
||||||
throw new ASWException(RCode.PACKAGE_FILE_TYPE_ERROR);
|
throw new ASWException(RCode.PACKAGE_FILE_TYPE_ERROR);
|
||||||
}
|
}
|
||||||
entity.setVersion(versionName);
|
entity.setVersion(apkInfo.getSdkVersion());
|
||||||
entity.setIdentifier(packageName);
|
entity.setIdentifier(apkInfo.getPackageName());
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error(e, "[savePackage] [save package error] [file: {}]", fileResource.getFilename());
|
log.error(e, "[savePackage] [save package error] [file: {}]", fileResource.getFilename());
|
||||||
|
|||||||
@@ -1,9 +1,14 @@
|
|||||||
package net.geedge.asw.module.app.util;
|
package net.geedge.asw.module.app.util;
|
||||||
|
|
||||||
|
import cn.hutool.core.io.FileUtil;
|
||||||
import cn.hutool.log.Log;
|
import cn.hutool.log.Log;
|
||||||
|
import net.geedge.asw.common.util.Constants;
|
||||||
import net.geedge.asw.common.util.T;
|
import net.geedge.asw.common.util.T;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipFile;
|
||||||
|
|
||||||
public class ApkUtil {
|
public class ApkUtil {
|
||||||
|
|
||||||
@@ -60,6 +65,9 @@ public class ApkUtil {
|
|||||||
while ((temp = bufferedReader.readLine()) != null) {
|
while ((temp = bufferedReader.readLine()) != null) {
|
||||||
setApkInfoProperty(apkInfo, temp);
|
setApkInfoProperty(apkInfo, temp);
|
||||||
}
|
}
|
||||||
|
if (T.StrUtil.isBlank(apkInfo.getPackageName()) || T.StrUtil.isBlank(apkInfo.getVersionName())) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
return apkInfo;
|
return apkInfo;
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error(e, "[parseApk] [error] [path: {}]", apkPath);
|
log.error(e, "[parseApk] [error] [path: {}]", apkPath);
|
||||||
@@ -73,6 +81,43 @@ public class ApkUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ApkInfo parseXapk(String xapkPath) {
|
||||||
|
InputStream inputStream = null;
|
||||||
|
BufferedReader reader = null;
|
||||||
|
File tempFile = null;
|
||||||
|
try {
|
||||||
|
ZipFile zipFile = new ZipFile(T.FileUtil.file(xapkPath));
|
||||||
|
ZipEntry entry = zipFile.getEntry("manifest.json");
|
||||||
|
inputStream = zipFile.getInputStream(entry);
|
||||||
|
StringBuilder manifestJson = new StringBuilder();
|
||||||
|
reader = new BufferedReader(new InputStreamReader(inputStream));
|
||||||
|
String line;
|
||||||
|
while ((line = reader.readLine()) != null) {
|
||||||
|
manifestJson.append(line).append("\n");
|
||||||
|
}
|
||||||
|
Map manifest = T.JSONUtil.toBean(manifestJson.toString(), Map.class);
|
||||||
|
ZipEntry packageFile = zipFile.getEntry(T.StrUtil.concat(true, T.MapUtil.getStr(manifest, "package_name"), ".apk"));
|
||||||
|
tempFile = T.FileUtil.file(Constants.TEMP_PATH, packageFile.getName());
|
||||||
|
FileUtil.writeBytes(zipFile.getInputStream(packageFile).readAllBytes(), tempFile);
|
||||||
|
ApkInfo apkInfo = this.parseApk(tempFile.getPath());
|
||||||
|
if (apkInfo == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (!T.BooleanUtil.and(apkInfo.getVersionName().equals(T.MapUtil.getStr(manifest, "version_name")),
|
||||||
|
apkInfo.getPackageName().equals(T.MapUtil.getStr(manifest, "package_name")))) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return apkInfo;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error(e, "[parseXapk] [error] [path: {}]", xapkPath);
|
||||||
|
return null;
|
||||||
|
} finally {
|
||||||
|
T.FileUtil.del(tempFile);
|
||||||
|
T.IoUtil.close(inputStream);
|
||||||
|
T.IoUtil.close(reader);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void setApkInfoProperty(ApkInfo apkInfo, String source) {
|
private void setApkInfoProperty(ApkInfo apkInfo, String source) {
|
||||||
if (source.startsWith(APPLICATION)) {
|
if (source.startsWith(APPLICATION)) {
|
||||||
String[] rs = source.split("( icon=')|'");
|
String[] rs = source.split("( icon=')|'");
|
||||||
|
|||||||
Reference in New Issue
Block a user