Browse Source

feat: test-upload comp

MetaPunkGames 4 months ago
parent
commit
14acb697cd

+ 4 - 0
projects/textbook/src/app/app.routes.ts

@@ -64,4 +64,8 @@ export const routes: Routes = [
     canActivate: mapToCanActivate([AuthGuard]),
     loadChildren: () =>import('../modules/nav-author/modules.routes').then((mod) => mod.NavAuthorRoutingModule),
   },
+  {
+    path: 'test/upload', // 用户登录/注册
+    loadComponent:()=> import('../app/comp-upload/example-upload/example-upload.component').then((mod) => mod.ExampleUploadComponent),
+  }
 ];

+ 3 - 1
projects/textbook/src/app/comp-upload/comp-upload.component.ts

@@ -25,6 +25,8 @@ export class CompUploadComponent implements OnInit {
   @Input('type') type: string = 'file';
   @Input('size') size: number = 2048; //上传文件限制大小单位KB
 
+  @Input('acl') acl: "public-read-write" | "public-read" | "private" | "default" = "default"
+
   get accept() {
     let type;
     switch (this.type) {
@@ -132,7 +134,7 @@ export class CompUploadComponent implements OnInit {
       if (file) {
         let ossFile;
         try {
-          ossFile = await this.ossProvider?.upload(file?.originFileObj);
+          ossFile = await this.ossProvider?.upload(file?.originFileObj,null,{acl:this.acl});
         } catch (err) { }
         if (ossFile) {
           ossFile.locaname = file.name;

+ 21 - 0
projects/textbook/src/app/comp-upload/example-upload/example-upload.component.html

@@ -0,0 +1,21 @@
+<h1>测试上传组件</h1>
+
+<app-comp-upload
+  [type]="'pdf'"
+    [width]="320"
+    (change)="upload($event)"
+    title="上传PDF"
+  ></app-comp-upload>
+
+
+<h1>测试私有上传</h1>
+
+<app-comp-upload
+  [type]="'pdf'"
+  [acl]="'private'"
+    [width]="320"
+    (change)="upload($event)"
+    title="上传PDF"
+  ></app-comp-upload>
+
+  <button (click)="get('http://hep-textbook.oss-cn-beijing.aliyuncs.com/undefined/2024-10-8/1728352740465-test.pdf')"></button>

+ 0 - 0
projects/textbook/src/app/comp-upload/example-upload/example-upload.component.scss


+ 22 - 0
projects/textbook/src/app/comp-upload/example-upload/example-upload.component.spec.ts

@@ -0,0 +1,22 @@
+import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
+
+import { ExampleUploadComponent } from './example-upload.component';
+
+describe('ExampleUploadComponent', () => {
+  let component: ExampleUploadComponent;
+  let fixture: ComponentFixture<ExampleUploadComponent>;
+
+  beforeEach(waitForAsync(() => {
+    TestBed.configureTestingModule({
+      imports: [ExampleUploadComponent],
+    }).compileComponents();
+
+    fixture = TestBed.createComponent(ExampleUploadComponent);
+    component = fixture.componentInstance;
+    fixture.detectChanges();
+  }));
+
+  it('should create', () => {
+    expect(component).toBeTruthy();
+  });
+});

+ 20 - 0
projects/textbook/src/app/comp-upload/example-upload/example-upload.component.ts

@@ -0,0 +1,20 @@
+import { Component, OnInit } from '@angular/core';
+import { CompUploadComponent } from '../comp-upload.component';
+
+@Component({
+  selector: 'app-example-upload',
+  templateUrl: './example-upload.component.html',
+  styleUrls: ['./example-upload.component.scss'],
+  standalone: true,
+  imports:[CompUploadComponent]
+})
+export class ExampleUploadComponent  implements OnInit {
+
+  constructor() { }
+
+  ngOnInit() {}
+
+  upload(ev:any){
+    console.log(ev)
+  }
+}

+ 12 - 3
projects/textbook/src/app/comp-upload/provider-oss-aliyun.ts

@@ -1,4 +1,4 @@
-import OSS from "ali-oss";
+import OSS, { PutObjectOptions } from "ali-oss";
 import Parse from "parse";
 
 
@@ -24,14 +24,23 @@ export class ProvierOssAli{
     constructor(){
     }
 
-    async upload(file:File,onProcess:Function){
+    async upload(file:File,onProcess?:Function,options?:any){
         await this.initClient();
         let now = new Date();
         let fname = `${Parse.User.current()?.id}/${now.getFullYear()}-${now.getMonth()+1}-${now.getDate()}/${now.getTime()}-${file?.name}`;
-        const r1 = await this.client?.put(fname, file);
+        let putOptions:any = {headers:{}}
+        if(options?.acl){
+            if(!putOptions.headers) putOptions.headers = {}
+            putOptions.headers["x-oss-object-acl"] = options?.acl
+        }
+        const r1 = await this.client?.put(fname, file, putOptions);
         console.log(file)
         console.log('put success: %j', r1);
         return r1
+    }
+    async get(url:string){
+        await this.initClient();
+        // const r1 = await this.client?.get(fname, file, putOptions);
 
     }
 

+ 11 - 0
server/cloud/aliyun/func-aliyun-oss.js

@@ -15,9 +15,20 @@ export function defineAliOssSTS(){
         });
 }
 
+// 公开OSS
+// 用户登录名称 mailto:hep-textbook-api@cloudhep.onaliyun.com
+// Bucket: hep-textbook
+// 内网地址:http://oss-cn-beijing.aliyuncs.com/
+// 外网地址:oss-cn-beijing-internal.aliyuncs.com
 const ALI_OSS_BUCKET = process.env.ALI_OSS_BUCKET || "hep-textbook"
 const ALI_OSS_ACCESS_KEY_ID = process.env.ALI_OSS_ACCESS_KEY_ID || "LTAI5t6AbTiAvXmeoVdJZhL3"
 const ALI_OSS_ACCESS_KEY_SECRET = process.env.ALI_OSS_ACCESS_KEY_SECRET || "KLtQRdIW69KLP7jnzHNUf7eKmdptxH"
+// 私有OSS
+// ---oss---
+const ALI_OSS_BUCKET_SECRET = process.env.ALI_OSS_BUCKET || "hep-textbook"
+// AccessKey ID: LTAI5t6AbTiAvXmeoVdJZhL3
+// AccessKey Secret: KLtQRdIW69KLP7jnzHNUf7eKmdptxH
+
 // 通过STS服务生成临时访问凭证。临时访问凭证包括临时访问密钥(AccessKeyId和AccessKeySecret)和安全令牌(SecurityToken)。
 const { STS } = require('ali-oss');
 const sts = new STS({