This commit is contained in:
shimu 2024-07-03 14:36:33 +08:00
parent ab566b1692
commit 31e88cf709
13 changed files with 2 additions and 371 deletions

0
iot/app.log Normal file
View File

View File

@ -20,26 +20,6 @@
<artifactId>core</artifactId>
<version>${com.zeto}</version>
</dependency>
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
<version>5.6.211</version>
</dependency>
<dependency>
<groupId>me.zhyd.oauth</groupId>
<artifactId>JustAuth</artifactId>
<version>1.16.5</version>
</dependency>
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos-sts_api</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<version>3.1.640</version>
</dependency>
</dependencies>
<build>
<plugins>
@ -97,7 +77,7 @@
</filters>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.zeto.${project.name}.Application</mainClass>
</transformer>
</transformers>

View File

@ -4,10 +4,10 @@ import com.zeto.ZenData;
import com.zeto.ZenEngine;
import com.zeto.ZenResult;
import com.zeto.annotation.AccessRole;
import com.zeto.annotation.Inject;
import com.zeto.domain.ZenRole;
import com.zeto.kit.DateKit;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.annotation.Inject;
@Slf4j

View File

@ -1,5 +0,0 @@
package com.zeto.iot.helper.login;
// 钉钉登录
public class DingLogin {
}

View File

@ -1,5 +0,0 @@
package com.zeto.iot.helper.login;
// 企业微信登录
public class WeCom {
}

View File

@ -1,15 +0,0 @@
package com.zeto.iot.helper.upload;
import java.util.Map;
public interface IUploader {
void put(String path, String content, String contentType);
void remove(String path);
String get(String path);
void refresh(String path);
Map<String, Object> token(String fileName, String target);
}

View File

@ -1,62 +0,0 @@
package com.zeto.iot.helper.upload;
import com.zeto.ZenEnvironment;
import com.zeto.iot.helper.upload.cos.CosUploader;
import java.util.HashMap;
import java.util.Map;
public class Uploader {
private static IUploader uploader = null;
private static final Map<String, String> contentTypeMap = new HashMap<String, String>() {{
put("js", "application/javascript;charset=utf-8");
put("css", "text/css;charset=utf-8");
put("json", "application/json;charset=utf-8");
put("gif", "image/gif");
put("png", "image/png");
put("jpg", "image/jpeg");
put("woff", "application/font-woff");
put("woff2", "application/font-woff2");
put("otf", "application/x-font-opentype");
put("ttf", "application/x-font-ttf");
put("svg", "image/svg+xml");
put("eot", "application/vnd.ms-fontobject");
}};
private static IUploader i() {
if (Uploader.uploader == null) {
String type = ZenEnvironment.get("cloudType");
if (type.equals("qcloud")) Uploader.uploader = new CosUploader();
}
return Uploader.uploader;
}
public static void put(String path, String content) {
int extIndex = path.lastIndexOf(".");
if (extIndex < 0) return;
String ext = path.substring(extIndex + 1);
if (!Uploader.contentTypeMap.containsKey(ext)) return;
Uploader.i().put(path, content, Uploader.contentTypeMap.get(ext));
Uploader.refresh(path);
}
public static String get(String path) {
return Uploader.i().get(path);
}
public static void remove(String path) {
Uploader.i().remove(path);
Uploader.refresh(path);
}
public static void refresh(String path) {
// 非线上环境无需刷CDN
if (!ZenEnvironment.isOnline()) return;
String webpath = ZenEnvironment.get("cdnHost") + path;
Uploader.i().refresh(webpath);
}
public static Map<String, Object> token(String fileName, String target) {
return Uploader.i().token(fileName, target);
}
}

View File

@ -1,155 +0,0 @@
package com.zeto.iot.helper.upload.cos;
import com.google.common.base.Strings;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.model.COSObject;
import com.qcloud.cos.model.COSObjectInputStream;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.region.Region;
import com.qcloud.cos.utils.IOUtils;
import com.qcloud.cos.utils.StringUtils;
import com.tencent.cloud.CosStsClient;
import com.tencent.cloud.Response;
import com.zeto.ZenEnvironment;
import com.zeto.iot.helper.upload.IUploader;
import com.zeto.kit.StringKit;
import lombok.extern.slf4j.Slf4j;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
@Slf4j
public class CosUploader implements IUploader {
private final String publicBucket;
private final COSClient cosClient;
private final String defaultType = "public";
public CosUploader() {
String secretId = ZenEnvironment.get("bucketAK");
String secretKey = ZenEnvironment.get("bucketSK");
String region = ZenEnvironment.get("bucketRegion");
publicBucket = ZenEnvironment.get("bucketPublic");
// 初始化用户身份信息(secretId, secretKey)
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
// 设置bucket的区域, COS地域的简称请参照 https://www.qcloud.com/document/product/436/6224
ClientConfig clientConfig = new ClientConfig(new Region(region));
// 生成cos客户端
this.cosClient = new COSClient(cred, clientConfig);
}
@Override
public void remove(String path) {
}
@Override
public void put(String path, String content, String contentType) {
if (Strings.isNullOrEmpty(content) || Strings.isNullOrEmpty(path)) return;
byte[] contentByteArray = content.getBytes(StringUtils.UTF8);
InputStream contentInput = new ByteArrayInputStream(contentByteArray);
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentType(contentType);
metadata.setContentLength(contentByteArray.length);
PutObjectRequest putObjectRequest = new PutObjectRequest(publicBucket, path, contentInput, metadata);
cosClient.putObject(putObjectRequest);
}
@Override
public String get(String path) {
COSObjectInputStream cosObjectInput = null;
try {
COSObject cosObject = cosClient.getObject(publicBucket, path);
cosObjectInput = cosObject.getObjectContent();
} catch (CosClientException e) {
CosUploader.log.error("uploader", e);
}
// 这里是直接读取按实际情况来处理
byte[] bytes = null;
try {
bytes = IOUtils.toByteArray(cosObjectInput);
} catch (IOException e) {
CosUploader.log.error("uploader", e);
} finally {
// 用完流之后一定要调用 close()
try {
cosObjectInput.close();
} catch (IOException e) {
CosUploader.log.error("uploader", e);
}
}
return new String(bytes);
}
// 刷新CDN
@Override
public void refresh(String path) {
}
@Override
public Map<String, Object> token(String fileName, String target) {
String secretId = ZenEnvironment.get("bucketAK");
String secretKey = ZenEnvironment.get("bucketSK");
String region = ZenEnvironment.get("bucketRegion");
String domain = ZenEnvironment.get("bucketDomain");
String filepath = this.getPath(fileName);
TreeMap<String, Object> config = new TreeMap<>();
// 替换为您的云 api 密钥 SecretId
config.put("secretId", secretId);
// 替换为您的云 api 密钥 SecretKey
config.put("secretKey", secretKey);
String bucket = defaultType.equals(target) ? publicBucket : ZenEnvironment.get("bucketPrivate");
// 临时密钥有效时长单位是秒默认 1800 目前主账号最长 2 小时 7200 子账号最长 36 小时 129600
int timeout = 1800;
config.put("durationSeconds", timeout);
config.put("bucket", bucket);
// 换成 publicBucket 所在地区
config.put("region", region);
String allowPrefix = filepath.substring(0, 18) + "*";
config.put("allowPrefixes", new String[]{allowPrefix});
String[] allowActions = new String[]{
"name/cos:PutObject",
"name/cos:PostObject"
};
config.put("allowActions", allowActions);
Map<String, Object> result = new HashMap<>();
try {
Response response = CosStsClient.getCredential(config);
// 腾讯云
result.put("type", "cos");
result.put("startTime", response.startTime);
result.put("expiredTime", response.expiredTime);
// 地域地址https://cloud.tencent.com/document/product/436/6224
result.put("pathname", filepath);
result.put("bucket", bucket);
result.put("region", region);
result.put("url", domain + filepath);
result.put("secretId", response.credentials.tmpSecretId);
result.put("secretKey", response.credentials.tmpSecretKey);
result.put("sessionToken", response.credentials.sessionToken);
} catch (IOException e) {
throw new RuntimeException(e);
}
return result;
}
private String getPath(String fileName) {
String ext = StringKit.fileExt(fileName);
String fileId = StringKit.objectId();
return "f01/" + ext + "/" + fileId.substring(0, 2) + "/" + fileId.substring(2) + "." + ext;
}
}

View File

@ -1,4 +0,0 @@
package com.zeto.iot.helper.upload.oss;
public class OssUploader {
}

View File

@ -1,57 +0,0 @@
package com.zeto.iot.hooks;
import com.zeto.IZenHook;
import com.zeto.ZenData;
import com.zeto.ZenEngine;
import com.zeto.ZenResult;
import com.zeto.annotation.ZenHook;
import com.zeto.driver.JsonKit;
import com.zeto.iot.helper.upload.Uploader;
import com.zeto.iot.hooks.domain.PageComponentDO;
import com.zeto.iot.hooks.domain.PageDO;
import com.zeto.kit.StringKit;
import org.noear.solon.annotation.Inject;
@ZenHook("patch/activityStatus")
public class ActivityStatusHook implements IZenHook {
private final static String online = "2";
private final static String offline = "3";
@Inject
private ZenEngine zenEngine;
@Override
public ZenResult before(ZenData context) {
String status = context.get("status");
ZenResult result = zenEngine.execute("get/activity", context);
String pathStr = StringKit.objectId();
String path = "/" + pathStr.substring(0, 2) + "/" + pathStr.substring(2);
// 页面上线
if (ActivityStatusHook.online.equals(status)) {
PageDO pageDO = result.get("content", PageDO.class);
Object pageData = result.getObject("data");
pageDO.setData(pageData);
StringBuilder jsResult = new StringBuilder();
for (PageComponentDO componentDO : pageDO.getComponents()) {
String jsSource = "/" + componentDO.getLib() + "/" + componentDO.getName() + ".js";
jsResult.append(Uploader.get(jsSource)).append(";");
}
String pageDataStr = JsonKit.stringify(pageDO);
// 推送数据与js资源
Uploader.put("/jsz" + path + ".json", pageDataStr);
Uploader.put("/jsz" + path + ".js", jsResult.toString());
context.put("output", path);
}
return ZenResult.success();
}
@Override
public void after(ZenData context, ZenResult result) {
String id = context.getId();
String status = context.get("status");
// 页面上线
if (ActivityStatusHook.offline.equals(status)) {
//todo: 完成cdn下线
}
}
}

View File

@ -1,21 +0,0 @@
package com.zeto.iot.hooks;
import com.zeto.IZenHook;
import com.zeto.ZenData;
import com.zeto.ZenResult;
import com.zeto.annotation.ZenHook;
import com.zeto.iot.helper.upload.Uploader;
import java.util.Map;
@ZenHook("zetoUploadHook")
public class ZetoUploadHook implements IZenHook {
@Override
public ZenResult before(ZenData context) {
IZenHook.super.before(context);
String fileName = context.get("filename");
String target = context.get("target");
Map<String, Object> data = Uploader.token(fileName, target);
return ZenResult.success().setData(data);
}
}

View File

@ -1,13 +0,0 @@
package com.zeto.iot.hooks.domain;
import lombok.Data;
import java.util.Map;
@Data
public class PageComponentDO {
private String name;
private String id;
private String lib;
private Map<String, String> meta;
}

View File

@ -1,12 +0,0 @@
package com.zeto.iot.hooks.domain;
import lombok.Data;
import java.util.List;
@Data
public class PageDO {
private List<PageComponentDO> components;
private int counter;
private Object data;
}