123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- import { Injectable } from '@angular/core';
- import ObsClient from "esdk-obs-browserjs"
- import Parse from "parse";
- export interface HwobsDir{
- Prefix:string
- }
- export interface HwobsFile{
- ETag: "\"f0ec968fe51ab48348307e06476122eb\""
- Key:string
- LastModified:string
- Owner:object
- Size:string
- StorageClass:string
- }
- export class HwobsProvider {
- obsClient:ObsClient
- bucketName:string
- host:string
- globalPrefix:string = ""
- constructor(options:{
- host:string
- bucketName:string
- access_key_id:string
- secret_access_key:string
- prefix?:string
- server?:string
- }) {
- this.globalPrefix = options.prefix || ""
- this.host = options?.host
- this.bucketName = options?.bucketName
- this.obsClient = new ObsClient({
- access_key_id: options.access_key_id,
- secret_access_key: options.secret_access_key,
-
- server: options?.server||'https://obs.cn-south-1.myhuaweicloud.com'
- });
- }
-
- listDir(prefix:any):Promise<{
- dirs:Array<HwobsDir>,
- files:Array<HwobsFile>
- }>{
- return new Promise((resolve,reject)=>{
- this.obsClient.listObjects({
- Bucket : this.bucketName,
- Prefix : prefix,
- Delimiter: '/'
- }, (err:any, result:any) => {
- if(err){
- console.error('Error-->' + err);
- reject(err)
- }else{
- console.log('Status-->' + result.CommonMsg.Status);
- console.log(result)
- if(result.CommonMsg.Status < 300 && result.InterfaceResult){
- for(var j in result.InterfaceResult.Contents){
- console.log('Contents[' + j + ']:');
- console.log('Key-->' + result.InterfaceResult.Contents[j]['Key']);
- console.log('Owner[ID]-->' + result.InterfaceResult.Contents[j]['Owner']['ID']);
- }
- }
- let dirs:HwobsDir[] = result.InterfaceResult.CommonPrefixes
- let files:HwobsFile[] = result.InterfaceResult.Contents
- resolve({dirs:dirs,files:files})
- }
- });
- })
- }
-
- async uploadFile(file:File,key:string):Promise<Parse.Object>{
- console.log(this.globalPrefix,key)
-
-
-
- let attach = await this.checkFileExists(file);
- if(attach?.id) return attach
- return new Promise((resolve,reject)=>{
- this.obsClient.putObject({
- Bucket : this.bucketName,
- Key : this.globalPrefix+key,
- SourceFile : file
- }, async (err:any, result:any) => {
- if(err){
- console.error('Error-->' + err);
- reject(err)
- }else{
- console.log('Status-->' + result.CommonMsg.Status);
- let attach = await this.saveAttachment(file,this.globalPrefix+key)
- resolve(attach)
- }
- });
- })
- }
- Attachment = Parse.Object.extend("Attachment")
- async checkFileExists(file:any):Promise<Parse.Object>{
- let hash = await this.getFileHash(file)
-
- let attach:Parse.Object
- let query = new Parse.Query("Attachment")
- query.equalTo("hash",hash);
- query.equalTo("size",file.size);
- let exists:any = await query.first();
- if(!exists?.id) exists = new this.Attachment()
- attach = exists
- return attach
- }
- async saveAttachment(file:File,key:string){
- console.log("saveAttachment",key)
- let hash = await this.getFileHash(file)
- let attach = await this.checkFileExists(file)
- attach.set("name",file.name)
- attach.set("size",file.size)
- attach.set("mime",file.type)
- attach.set("url",this.host + key)
- attach.set("hash",hash)
- attach = await attach.save()
- return attach
- }
- async getFileHash(file:File) {
- return new Promise((resolve, reject) => {
- const reader = new FileReader();
- reader.onload = async (event:any) => {
- const buffer = event.target.result;
- const hashBuffer = await crypto.subtle.digest('SHA-256', buffer);
- const hashArray = Array.from(new Uint8Array(hashBuffer));
- const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, '0')).join('');
- resolve(hashHex);
- };
- reader.onerror = (event:any) => {
- reject(event.target.error);
- };
- reader.readAsArrayBuffer(file);
- });
- }
- }
|