Skip to content
Snippets Groups Projects
Commit 4d191ff5 authored by Igor Markin's avatar Igor Markin
Browse files

Get upload time for files from backblaze and provide timestamp for status data

parent 733a4916
No related branches found
No related tags found
1 merge request!74Get upload time for files from backblaze and provide timestamp for status data
Pipeline #42093 passed
......@@ -4,7 +4,10 @@ declare class CloudflareService {
private _defaultBucket;
private _statusesService;
constructor(cdnUrl: string, defaultBucket: string);
fetchFile(fileName: string, bucket?: string): Promise<unknown>;
fetchFile<T>(fileName: string, bucket?: string): Promise<{
data: T;
uploadTime: number;
}>;
fetchStatusData(statusFileName: string): Promise<StatusData>;
readStatuses(statusId: string): Promise<Array<StatusData>>;
getMerkleTree(fileName: string): Promise<Array<string>>;
......
......@@ -28,6 +28,7 @@ export interface StatusData {
statusClassName?: string;
batchId: string;
transactionHash: string;
timestamp: number;
}
export interface BatchVerificationDetails {
statusPosition: number;
......
......@@ -30,23 +30,29 @@ class CloudflareService {
this._statusesService = new StatusesService();
}
async fetchFile(
async fetchFile<T>(
fileName: string,
bucket = this._defaultBucket
): Promise<unknown> {
): Promise<{ data: T; uploadTime: number }> {
// v?= is a dirty fix to prevent caching of the CORS error response in case file is missing.
// Because of browser caching CORS error in case of missing file, it's not possible to retrieve it when it's being uploaded.
// Dig into https://www.backblaze.com/b2/docs/cors_rules.html and fix
const { data } = await axios(
`${this._cdnUrl}/file/${bucket}/${fileName}?v=${Math.random()}`
);
return data;
const response = await axios(`${this._cdnUrl}/file/${bucket}/${fileName}`);
const data = response.data as T;
const uploadTime = response.headers["x-bz-upload-timestamp"]
? parseInt(response.headers["x-bz-upload-timestamp"])
: -1;
return { data, uploadTime };
}
async fetchStatusData(statusFileName: string): Promise<StatusData> {
const rawStatusData = (await this.fetchFile(
statusFileName
)) as RawStatusData;
const {
data: rawStatusData,
uploadTime,
} = await this.fetchFile<RawStatusData>(statusFileName);
let decodedResult;
try {
decodedResult = this._statusesService.decodeStatusObject(
......@@ -62,6 +68,7 @@ class CloudflareService {
transactionHash: rawStatusData.TransactionID,
statusRaw: rawStatusData.Status,
statusClassName: decodedResult.className,
timestamp: uploadTime,
};
}
......@@ -91,9 +98,9 @@ class CloudflareService {
}
async getMerkleTree(fileName: string): Promise<Array<string>> {
const merkleTreeFileContents = (await this.fetchFile(
const { data: merkleTreeFileContents } = await this.fetchFile<string[]>(
fileName
)) as Array<string>;
);
const statusesData: Array<StatusData> = await Promise.all(
merkleTreeFileContents.map((statusFileName) => {
......
......@@ -230,9 +230,11 @@ const withServices = (
const base64SHA256 = await cryptoService.SHA256(base64);
const backblazeDataPart = (await cloudFlareServiceInstance.fetchFile(
const {
data: backblazeDataPart,
} = await cloudFlareServiceInstance.fetchFile<BackblazeDataPartRaw>(
`qrcode-${arrayBufferToHex(base64SHA256)}`
)) as BackblazeDataPartRaw;
);
const assembledData = assembleQrCodeData(
base64ToArrayBuffer(qrCodeDataPart.data),
......
......@@ -196,9 +196,9 @@ class VerificationService extends EventEmitter {
if (verificationMethod === VERIFICATION_METHOD_MERKLE_TREE) {
batchItems = await this._cloudflareService.getMerkleTree(batchFilename);
} else if (verificationMethod === VERIFICATION_METHOD_SHA256_BATCH) {
batchItems = (await this._cloudflareService.fetchFile(
batchFilename
)) as string[];
({ data: batchItems } = await this._cloudflareService.fetchFile<
string[]
>(batchFilename));
}
} catch (e) {
this.catchVerificationError(
......
......@@ -32,6 +32,7 @@ export interface StatusData {
statusClassName?: string;
batchId: string;
transactionHash: string;
timestamp: number;
}
export interface BatchVerificationDetails {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment