-
Notifications
You must be signed in to change notification settings - Fork 0
/
azurblobstorage.ts
199 lines (163 loc) · 5.17 KB
/
azurblobstorage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import CryptoJS from 'crypto-js'
import { IResolverElement, Resolver } from './resolver'
import Upload, { UploadOptions } from 'react-native-background-upload'
import * as mime from 'react-native-mime-types';
type RequestType =
| 'Upload'
| 'Download'
;
interface FetchParam {
assest: any,
container: string,
type: RequestType,
filenameprefix: string
}
interface AzureCosmosBlobElement extends IResolverElement<RequestType> {
azurBlobFetch(param: FetchParam): Promise<BlobResponse>;
}
class BlobResponse
{
uploadId : string;
filename : string;
constructor(uploadId : string,filename : string)
{
this.uploadId = uploadId;
this.filename = filename;
}
}
interface AzureConfig {
storageKey: string;
account: string;
version: string;
}
class AzureBlobLocator extends Resolver<RequestType, AzureCosmosBlobElement> {
static myInstance: AzureBlobLocator = null;
static config: AzureConfig;
static getInstance() {
if (AzureBlobLocator.myInstance == null) {
AzureBlobLocator.myInstance = new AzureBlobLocator();
}
return this.myInstance;
}
constructor() {
super();
super.Add(new AzureBlolUpload())
}
}
export const initAzureBlob = (config: AzureConfig) => {
AzureBlobLocator.config = config;
}
const UUID = () => {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
const IOSAssest = (assest) => {
if (!assest || !assest.uri) return assest;
let assestUri = assest.uri;
if (assestUri.includes("ph:/")) {
const appleId = assest.uri.substring(5, 41);
const fil = assest.filename.split('.');
const ext = fil[1];
assestUri = `assets-library://asset/asset.${ext}?id=${appleId}&ext=${ext}`;
}
return assestUri;
}
class AzureBlolUpload implements AzureCosmosBlobElement {
Key: RequestType = 'Upload';
constructor() {
}
async azurBlobFetch(param: FetchParam) {
const today = new Date();
const UTCstring = today.toUTCString();
let authparam = new AuthParam();
authparam.request = "PUT";
authparam.version = AzureBlobLocator.config.version;
authparam.storagekey = AzureBlobLocator.config.storageKey;
authparam.account = AzureBlobLocator.config.account;
authparam.container = param.container;
authparam.blobtype = "BlockBlob";
authparam.filename = `${param.filenameprefix}${UUID()}`;
authparam.date = UTCstring;
const metadata = await Upload.getFileInfo(param.assest.uri);
authparam.length = metadata.size || param.assest.fileSize;
authparam.contentType = metadata.mimeType || mime.lookup(param.assest.filename).toString();
const uri = IOSAssest(param.assest);
const auth = authHead(authparam);
const options: UploadOptions = {
url: `https://${authparam.account}.blob.core.windows.net/${authparam.container}/${authparam.filename}`,
path: uri,
method: 'PUT',
type: 'raw',
headers: {
'content-type': `${authparam.contentType}`,
'Content-Length': `${authparam.length}`,
'x-ms-version': `${authparam.version}`,
'x-ms-blob-type': 'BlockBlob',
'Authorization': auth,
'x-ms-date': UTCstring,
},
notification: {
enabled: true
}
}
const uploadId =await Upload.startUpload(options);
return new BlobResponse(uploadId,authparam.filename)
}
}
class AuthParam implements IAuthParam {
request: string;
length: number;
contentType: string;
version: string;
filename: string;
storagekey: string;
account: string;
container: string;
date: string;
blobtype: string;
}
interface IAuthParam {
request: string,
length: number,
contentType: string,
version: string,
filename: string,
storagekey: string,
account: string,
container: string,
date: string,
blobtype: string
}
export const authHead = (param: IAuthParam) => {
var signatureParts = [
"PUT",
"",
"",
param.length,
"",
param.contentType,
"",
"",
"",
"",
"",
"",
`x-ms-blob-type:${param.blobtype}`,
`x-ms-date:${param.date}`,
`x-ms-version:${param.version}`,
`/${param.account}/${param.container}/${param.filename}`
];
const signatureRaw = signatureParts.join("\n");
console.log(signatureRaw)
const storageKey = param.storagekey;
const signatureBytes = CryptoJS.HmacSHA256(signatureRaw, CryptoJS.enc.Base64.parse(storageKey));
const signatureEncoded = signatureBytes.toString(CryptoJS.enc.Base64);
return `SharedKey ${param.account}:${signatureEncoded}`;
}
export const azureblobfetch = async (param: FetchParam) => {
let loctor = AzureBlobLocator.getInstance();
const reducer = loctor.Find(param.type);
return reducer.azurBlobFetch(param)
}