|
@@ -0,0 +1,274 @@
|
|
|
+package extract
|
|
|
+
|
|
|
+import (
|
|
|
+ "bytes"
|
|
|
+ "encoding/json"
|
|
|
+ "errors"
|
|
|
+ "io"
|
|
|
+ "log"
|
|
|
+ "mime/multipart"
|
|
|
+ "net/http"
|
|
|
+ "net/rpc"
|
|
|
+ "strconv"
|
|
|
+)
|
|
|
+
|
|
|
+/*
|
|
|
+ http请求地址:172.17.162.27:18011
|
|
|
+ rpc请求地址:172.17.162.27:18012
|
|
|
+*/
|
|
|
+
|
|
|
+var Oss_Address = "http://172.17.162.27:18011"
|
|
|
+var Detail_BucketId = "detail"
|
|
|
+var Html_BucketId = "contenthtml"
|
|
|
+
|
|
|
+// RPC相关结构体
|
|
|
+type UploadArgs struct {
|
|
|
+ Stream []byte // 客户端将文件数据传递过来
|
|
|
+ Gzip bool //是否压缩
|
|
|
+ BucketID string //桶id
|
|
|
+ ObjectName string //对象名称
|
|
|
+}
|
|
|
+
|
|
|
+type Args struct {
|
|
|
+ BucketID string //桶id
|
|
|
+ ObjectName string //对象名称
|
|
|
+}
|
|
|
+
|
|
|
+// 接口统一返回值
|
|
|
+type Result struct {
|
|
|
+ Error_code int `json:"error_code"`
|
|
|
+ Error_msg string `json:"error_msg"`
|
|
|
+ Data interface{} `json:"data"`
|
|
|
+}
|
|
|
+
|
|
|
+const (
|
|
|
+ UploadUrl = "/ossservice/upload"
|
|
|
+ DownloadUrl = "/ossservice/download"
|
|
|
+ DeleteUrl = "/ossservice/delete"
|
|
|
+ GetBidDetailUrl = "/ossservice/biddetail"
|
|
|
+ UploadSuccess = "上传成功"
|
|
|
+ DownloadSuccess = "下载成功"
|
|
|
+ DeleteSuccess = "删除成功"
|
|
|
+ GetBidDetailSuccess = "获取正文成功"
|
|
|
+ UploadFail = "上传失败:%v"
|
|
|
+ DownloadFail = "下载失败:%v"
|
|
|
+ DeleteFail = "删除失败:%v"
|
|
|
+ BidDetailFail = "获取正文失败:%v"
|
|
|
+)
|
|
|
+
|
|
|
+/* restful方式上传
|
|
|
+ * @param domain 域名,例如:https://ossservice.jianyu360.cn
|
|
|
+ * @param bucketId 桶id
|
|
|
+ * @param objectName 对象名称
|
|
|
+ * @param stream 文件流
|
|
|
+ * @param gzip 是否压缩
|
|
|
+ * @return {"error_code":0,"error_msg":"上传成功"}
|
|
|
+ */
|
|
|
+func UpLoadByRestful(domain, bucketId, objectName string, stream []byte, gzip bool) (reply *Result) {
|
|
|
+ reply = &Result{Error_code: -1}
|
|
|
+
|
|
|
+ // 创建一个缓冲区来存储表单数据
|
|
|
+ body := &bytes.Buffer{}
|
|
|
+ writer := multipart.NewWriter(body)
|
|
|
+ writer.WriteField("bucket_id", bucketId)
|
|
|
+ writer.WriteField("object_name", objectName)
|
|
|
+ writer.WriteField("gzip", strconv.FormatBool(gzip))
|
|
|
+
|
|
|
+ // 创建表单字段
|
|
|
+ part, err := writer.CreateFormFile("file", objectName)
|
|
|
+ if err != nil {
|
|
|
+ reply.Error_msg = err.Error()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 模拟文件流
|
|
|
+ fileStream := bytes.NewReader(stream)
|
|
|
+
|
|
|
+ // 将文件流复制到表单字段
|
|
|
+ _, err = io.Copy(part, fileStream)
|
|
|
+ if err != nil {
|
|
|
+ reply.Error_msg = err.Error()
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建 HTTP 请求
|
|
|
+ if respBody, err := post(domain+UploadUrl, writer, body); err != nil {
|
|
|
+ reply.Error_msg = err.Error()
|
|
|
+ } else {
|
|
|
+ json.Unmarshal(respBody, &reply)
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+/* restful方式下载
|
|
|
+ * @param domain 域名,例如:https://ossservice.jianyu360.cn
|
|
|
+ * @param bucketId 桶id
|
|
|
+ * @param objectName 对象名称
|
|
|
+ * @return {"error_code":0,"error_msg":"下载成功"}
|
|
|
+ */
|
|
|
+func DownloadByRestful(domain, bucketId, objectName string) (reply *Result) {
|
|
|
+ reply = &Result{}
|
|
|
+ // 创建一个缓冲区来存储表单数据
|
|
|
+ body := &bytes.Buffer{}
|
|
|
+ writer := multipart.NewWriter(body)
|
|
|
+ writer.WriteField("bucket_id", bucketId)
|
|
|
+ writer.WriteField("object_name", objectName)
|
|
|
+ if respBody, err := post(domain+DownloadUrl, writer, body); err != nil {
|
|
|
+ reply.Error_msg = err.Error()
|
|
|
+ } else {
|
|
|
+ reply.Error_msg = DownloadSuccess
|
|
|
+ reply.Data = respBody
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+/* restful方式删除
|
|
|
+ * @param domain 域名,例如:https://ossservice.jianyu360.cn
|
|
|
+ * @param bucketId 桶id
|
|
|
+ * @param objectName 对象名称
|
|
|
+ * @return {"error_code":0,"error_msg":"上传成功"}
|
|
|
+ */
|
|
|
+func DeleteByRestful(domain, bucketId, objectName string) (reply *Result) {
|
|
|
+ reply = &Result{}
|
|
|
+ // 创建一个缓冲区来存储表单数据
|
|
|
+ body := &bytes.Buffer{}
|
|
|
+ writer := multipart.NewWriter(body)
|
|
|
+ writer.WriteField("bucket_id", bucketId)
|
|
|
+ writer.WriteField("object_name", objectName)
|
|
|
+ if respBody, err := post(domain+DeleteUrl, writer, body); err != nil {
|
|
|
+ reply.Error_msg = err.Error()
|
|
|
+ } else {
|
|
|
+ json.Unmarshal(respBody, &reply)
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+/* restful方式获取标讯正文
|
|
|
+ * @param domain 域名,例如:https://ossservice.jianyu360.cn
|
|
|
+ * @param bucketId 桶id
|
|
|
+ * @param objectName 对象名称
|
|
|
+ * @return {"error_code":0,"error_msg":"获取正文成功","data":"正文内容"}
|
|
|
+ */
|
|
|
+func GetBidDetailByRestful(domain, bucketId, objectName string) (reply *Result) {
|
|
|
+ reply = &Result{}
|
|
|
+ // 创建一个缓冲区来存储表单数据
|
|
|
+ body := &bytes.Buffer{}
|
|
|
+ writer := multipart.NewWriter(body)
|
|
|
+ writer.WriteField("bucket_id", bucketId)
|
|
|
+ writer.WriteField("object_name", objectName)
|
|
|
+ if respBody, err := post(domain+GetBidDetailUrl, writer, body); err != nil {
|
|
|
+ reply.Error_msg = err.Error()
|
|
|
+ } else {
|
|
|
+ reply.Error_msg = GetBidDetailSuccess
|
|
|
+ reply.Data = string(respBody)
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func post(url string, writer *multipart.Writer, body *bytes.Buffer) ([]byte, error) {
|
|
|
+ // 关闭表单写入器
|
|
|
+ if err := writer.Close(); err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ // 创建 HTTP 请求
|
|
|
+ req, err := http.NewRequest("POST", url, body)
|
|
|
+ if err != nil {
|
|
|
+ log.Println("Error creating request:", err)
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ // 设置请求头
|
|
|
+ req.Header.Set("Content-Type", writer.FormDataContentType())
|
|
|
+
|
|
|
+ // 发送请求
|
|
|
+ client := &http.Client{}
|
|
|
+ resp, err := client.Do(req)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ defer resp.Body.Close()
|
|
|
+
|
|
|
+ // 读取响应
|
|
|
+ respBody, err := io.ReadAll(resp.Body)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ if resp.StatusCode != http.StatusOK {
|
|
|
+ return nil, errors.New(string(respBody))
|
|
|
+ }
|
|
|
+ return respBody, nil
|
|
|
+}
|
|
|
+
|
|
|
+/* rpc方式上传
|
|
|
+ * @param address 域名,例如:192.168.3.206:8110
|
|
|
+ * @param args 参数
|
|
|
+ * @param args.BucketID 文件名
|
|
|
+ * @param args.objectName 对象名称
|
|
|
+ * @param args.Stream 文件流
|
|
|
+ * @param args.Gzip 是否压缩
|
|
|
+ * @return {"error_code":0,"error_msg":"上传成功"}
|
|
|
+ * @return error 错误信息
|
|
|
+ */
|
|
|
+func UpLoadByRpc(address string, args *UploadArgs) (Result, error) {
|
|
|
+ var reply Result
|
|
|
+ err := rpcCall(address, "OSSService.Upload", args, &reply)
|
|
|
+ return reply, err
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+ *rpc方式下载
|
|
|
+ * @param address 域名,例如:192.168.3.206:8110
|
|
|
+ * @param args 参数
|
|
|
+ * @param args.BucketID 文件名
|
|
|
+ * @param args.objectName 对象名称
|
|
|
+ * @return {"error_code":0,"error_msg":"下载成功","data":"文件流"}
|
|
|
+ * @return error 错误信息
|
|
|
+ */
|
|
|
+func DownloadByRpc(address string, args *Args) (Result, error) {
|
|
|
+ var reply Result
|
|
|
+ err := rpcCall(address, "OSSService.Download", args, &reply)
|
|
|
+ return reply, err
|
|
|
+}
|
|
|
+
|
|
|
+/* rpc方式删除
|
|
|
+ * @param address 域名,例如:192.168.3.206:8110
|
|
|
+ * @param args 参数
|
|
|
+ * @param args.BucketID 文件名
|
|
|
+ * @param args.objectName 对象名称
|
|
|
+ * @return {"error_code":0,"error_msg":"删除成功"}
|
|
|
+ * @return error 错误信息
|
|
|
+ */
|
|
|
+func DeleteByRpc(address string, args *Args) (Result, error) {
|
|
|
+ var reply Result
|
|
|
+ err := rpcCall(address, "OSSService.Delete", args, &reply)
|
|
|
+ return reply, err
|
|
|
+}
|
|
|
+
|
|
|
+/*
|
|
|
+ *rpc方式获取标讯正文
|
|
|
+ * @param address 域名,例如:192.168.3.206:8110
|
|
|
+ * @param args 参数
|
|
|
+ * @param args.BucketID 文件名
|
|
|
+ * @param args.objectName 对象名称
|
|
|
+ * @return {"error_code":0,"error_msg":"下载成功","data":"正文内容"}
|
|
|
+ * @return error 错误信息
|
|
|
+ */
|
|
|
+func GetBidDetailByRpc(address string, args *Args) (Result, error) {
|
|
|
+ var reply Result
|
|
|
+ err := rpcCall(address, "OSSService.GetBidDetail", args, &reply)
|
|
|
+ return reply, err
|
|
|
+}
|
|
|
+func rpcCall(address, serviceMethod string, args any, reply any) error {
|
|
|
+ client, err := rpc.DialHTTP("tcp", address)
|
|
|
+ if err != nil {
|
|
|
+ log.Println(err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ defer client.Close()
|
|
|
+ err = client.Call(serviceMethod, args, reply)
|
|
|
+ if err != nil {
|
|
|
+ log.Println(err)
|
|
|
+ return err
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|