root 2 gadi atpakaļ
vecāks
revīzija
8d9d29e6a4
2 mainītis faili ar 55 papildinājumiem un 4 dzēšanām
  1. 5 2
      src/components/App.tsx
  2. 50 2
      src/data/blob.ts

+ 5 - 2
src/components/App.tsx

@@ -224,6 +224,7 @@ import {
   isSupportedImageFile,
   resizeImageFile,
   SVGStringToFile,
+  Upload_AWS,
 } from "../data/blob";
 import {
   getInitializedImageElements,
@@ -4483,7 +4484,9 @@ class App extends React.Component<AppProps, AppState> {
       );
       throw new Error(t("errors.imageInsertError"));
     }
-
+    window.console.log(imageFile);
+    window.console.log("bbbbbb");
+    window.console.log(imageFile);
     const existingFileData = this.files[fileId];
     if (!existingFileData?.dataURL) {
       try {
@@ -4526,7 +4529,7 @@ class App extends React.Component<AppProps, AppState> {
     //
     //此处改成亚马逊上传后返回
     const dataURL =
-      this.files[fileId]?.dataURL || (await getDataURL(imageFile));
+      this.files[fileId]?.dataURL || (await Upload_AWS(imageFile));
 
     const imageElement = mutateElement(
       _imageElement,

+ 50 - 2
src/data/blob.ts

@@ -16,6 +16,7 @@ import { FileSystemHandle } from "./filesystem";
 import { isValidExcalidrawData } from "./json";
 import { restore } from "./restore";
 import { ImportedLibraryData } from "./types";
+const AWS: any = require("https://pbl.cocorobo.cn/pbl-student-table/dist/js/aws-sdk-2.235.1.min.js");
 
 const parseFileContents = async (blob: Blob | File) => {
   let contents: string;
@@ -158,7 +159,7 @@ export const loadFromBlob = async (
 
     return result;
   } catch (error: any) {
-    console.error(error.message);
+    window.console.error(error.message);
     throw new Error(t("alerts.couldNotLoadInvalidFile"));
   }
 };
@@ -204,7 +205,7 @@ export const generateIdFromFile = async (file: File): Promise<FileId> => {
     );
     return bytesToHexString(new Uint8Array(hashBuffer)) as FileId;
   } catch (error: any) {
-    console.error(error);
+    window.console.error(error);
     // length 40 to align with the HEX length of SHA-1 (which is 160 bit)
     return nanoid(40) as FileId;
   }
@@ -222,6 +223,53 @@ export const getDataURL = async (file: Blob | File): Promise<DataURL> => {
   });
 };
 
+export const Upload_AWS = async (file: Blob | File): Promise<DataURL> => {
+  return new Promise((resolve, reject) => {
+    const credentials = {
+      accessKeyId: "AKIATLPEDU37QV5CHLMH",
+      secretAccessKey: "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",
+    }; //秘钥形式的登录上传
+    AWS.config.update(credentials);
+    AWS.config.region = "cn-northwest-1"; //设置区域
+
+    const bucket = new AWS.S3({
+      params: {
+        Bucket: "ccrb",
+      },
+    }); //选择桶
+    //file.name = "";
+    if (file) {
+      const _name = file.name || "";
+      const params = {
+        Key: `${_name.split(".")[0] + new Date().getTime()}.` + `png`,
+        ContentType: file.type,
+        Body: file,
+        "Access-Control-Allow-Credentials": "*",
+        ACL: "public-read",
+      }; //key可以设置为桶的相抵路径,Body为文件, ACL最好要设置
+      const options = {
+        partSize: 2048 * 1024 * 1024,
+        queueSize: 2,
+        leavePartsOnError: true,
+      };
+      bucket
+        .upload(params, options)
+        .on("httpUploadProgress", (evt: any) => {
+          //这里可以写进度条
+          // console.log("Uploaded : " + parseInt((evt.loaded * 80) / evt.total) + '%');
+        })
+        .send((err: any, data: any) => {
+          if (data) {
+            window.console.log(data.Location);
+            resolve(data.Location);
+          } else {
+            reject(err);
+          }
+        });
+    }
+  });
+};
+
 export const dataURLToFile = (dataURL: DataURL, filename = "") => {
   const dataIndexStart = dataURL.indexOf(",");
   const byteString = atob(dataURL.slice(dataIndexStart + 1));