Skip to content

Commit 235e275

Browse files
authored
Merge pull request #1515 from numbersprotocol/revert-1467-feature-download-asset-using-cdn
Revert "Feature download asset using cdn"
2 parents 41be469 + 030008f commit 235e275

File tree

5 files changed

+24
-34
lines changed

5 files changed

+24
-34
lines changed

src/app/features/home/capture-tab/capture-tab.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export class CaptureTabComponent {
9999

100100
refreshCaptures(event: Event) {
101101
this.diaBackendAssetRefreshingService
102-
.refresh$()
102+
.refresh()
103103
.pipe(finalize(() => (<CustomEvent>event).detail.complete()))
104104
.subscribe();
105105
}

src/app/features/home/details/information/session/information-session.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class DetailedCapture {
5252
const [index, meta] = Object.entries(proof.indexedAssets)[0];
5353
if (!(await this.mediaStore.exists(index)) && proof.diaBackendAssetId) {
5454
const mediaBlob = await this.diaBackendAssetRepository
55-
.downloadAssetFileFromCDN$(proof.diaBackendAssetId)
55+
.downloadFile$({ id: proof.diaBackendAssetId, field: 'asset_file' })
5656
.pipe(
5757
first(),
5858
catchError((err: unknown) => this.errorService.toastError$(err))
@@ -63,6 +63,7 @@ export class DetailedCapture {
6363
return proof.getFirstAssetUrl();
6464
});
6565
}
66+
6667
return this.diaBackendAsset$.pipe(
6768
isNonNullable(),
6869
switchMap(asset =>

src/app/shared/dia-backend/asset/dia-backend-asset-repository.service.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export class DiaBackendAssetRepository {
134134
iif(
135135
() => !!image,
136136
of(image).pipe(isNonNullable()),
137-
this.downloadAssetFileFromCDN$(postCapture.id).pipe(
137+
this.downloadFile$({ id: postCapture.id, field: 'asset_file' }).pipe(
138138
first(),
139139
tap(blob => {
140140
// eslint-disable-next-line rxjs/no-subject-value
@@ -200,18 +200,16 @@ export class DiaBackendAssetRepository {
200200
);
201201
}
202202

203-
downloadAssetFileFromCDN$(cid: string) {
203+
downloadFile$({ id, field }: { id: string; field: AssetDownloadField }) {
204+
const formData = new FormData();
205+
formData.append('field', field);
204206
return defer(() => this.authService.getAuthHeaders()).pipe(
205207
concatMap(headers =>
206-
this.httpClient.get<DiaBackendAsset>(
207-
`${BASE_URL}/api/v3/assets/${cid}`,
208-
{ headers }
208+
this.httpClient.post(
209+
`${BASE_URL}/api/v3/assets/${id}/download/`,
210+
formData,
211+
{ headers, responseType: 'blob' }
209212
)
210-
),
211-
concatMap(diaBackendAsset =>
212-
this.httpClient.get(`${diaBackendAsset.asset_file}`, {
213-
responseType: 'blob',
214-
})
215213
)
216214
);
217215
}

src/app/shared/dia-backend/asset/downloading/dia-backend-downloading.service.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
1+
import { HttpErrorResponse } from '@angular/common/http';
22
import { Injectable } from '@angular/core';
3+
import { first } from 'rxjs/operators';
34
import { blobToBase64 } from '../../../../utils/encoding/encoding';
45
import { OnConflictStrategy } from '../../../database/table/table';
56
import { HttpErrorCode } from '../../../error/error.service';
@@ -22,8 +23,7 @@ export class DiaBackendAssetDownloadingService {
2223
constructor(
2324
private readonly assetRepository: DiaBackendAssetRepository,
2425
private readonly mediaStore: MediaStore,
25-
private readonly proofRepository: ProofRepository,
26-
private readonly httpClient: HttpClient
26+
private readonly proofRepository: ProofRepository
2727
) {}
2828

2929
async storeRemoteCapture(
@@ -48,11 +48,9 @@ export class DiaBackendAssetDownloadingService {
4848
if (!diaBackendAsset.information.proof) {
4949
return;
5050
}
51-
// This function is only called by storeRemoteCapture, which should always
52-
// get diaBackendAsset from backend. That should mean we're always using
53-
// up-to-date asset_file_thumbnail cdn link.
54-
const thumbnailBlob = await this.httpClient
55-
.get(diaBackendAsset.asset_file_thumbnail, { responseType: 'blob' })
51+
const thumbnailBlob = await this.assetRepository
52+
.downloadFile$({ id: diaBackendAsset.id, field: 'asset_file_thumbnail' })
53+
.pipe(first())
5654
.toPromise();
5755
return this.mediaStore.storeThumbnail(
5856
diaBackendAsset.proof_hash,

src/app/shared/dia-backend/asset/refreshing/dia-backend-asset-refreshing.service.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { HttpErrorResponse } from '@angular/common/http';
22
import { Injectable } from '@angular/core';
33
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
4-
import { EMPTY, forkJoin, of } from 'rxjs';
4+
import { EMPTY, forkJoin } from 'rxjs';
55
import { catchError, concatMap, first } from 'rxjs/operators';
66
import { HttpErrorCode } from '../../../error/error.service';
77
import { ProofRepository } from '../../../repositories/proof/proof-repository.service';
@@ -27,18 +27,15 @@ export class DiaBackendAsseRefreshingService {
2727
.subscribe(value => (this.pendingUploadTasks = value));
2828
}
2929

30-
refresh$() {
30+
refresh() {
3131
// Don't refresh if there are still captures being uploaded.
3232
if (this.pendingUploadTasks > 0) {
3333
return EMPTY;
3434
}
35-
3635
return this.proofRepository.all$.pipe(
3736
first(),
38-
// Remove deleted Captures or Captures that no longer belong to the user.
39-
concatMap(proofs => {
40-
if (proofs.length === 0) return of([]);
41-
return forkJoin(
37+
concatMap(proofs =>
38+
forkJoin(
4239
proofs.map(proof =>
4340
this.assetRepository.fetchByProof$(proof).pipe(
4441
catchError((err: unknown) => {
@@ -52,13 +49,9 @@ export class DiaBackendAsseRefreshingService {
5249
})
5350
)
5451
)
55-
);
56-
}),
57-
concatMap(async () => {
58-
// TO DO: pass in diaBackendAssets and skip those when prefetching.
59-
await this.diaBackendAssetPrefetchingService.prefetch();
60-
return EMPTY;
61-
})
52+
)
53+
),
54+
concatMap(() => this.diaBackendAssetPrefetchingService.prefetch())
6255
);
6356
}
6457
}

0 commit comments

Comments
 (0)