Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use uploadFile from sdk #118

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 9 additions & 18 deletions src/app/@api/file.api.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from '@env/environment';
import {
FILE_SIZES,
BUILD5_PROD_ADDRESS_API,
BUILD5_TEST_ADDRESS_API,
WEN_FUNC,
} from '@build-5/interfaces';
import { FILE_SIZES } from '@build-5/interfaces';
import { NzUploadXHRArgs } from 'ng-zorro-antd/upload';
import { Observable, map, of } from 'rxjs';
import { Build5, https } from '@build-5/sdk';

const EXTENSION_PAT = /\.[^/.]+$/;
const ORIGIN = environment.production ? Build5.PROD : Build5.TEST;

@Injectable({
providedIn: 'root',
Expand Down Expand Up @@ -42,22 +39,16 @@ export class FileApi {
return of(url.match('.mp4$') ? 'video' : 'image');
};

public upload(memberId: string, item: NzUploadXHRArgs) {
const formData = new FormData();
formData.append('file', <Blob>item.postFile);
formData.append('member', memberId);
formData.append('uid', item.file.uid);
formData.append('projectApiKey', environment.build5Token);
const origin = environment.production ? BUILD5_PROD_ADDRESS_API : BUILD5_TEST_ADDRESS_API;
return this.httpClient
.post(origin + WEN_FUNC.uploadFile, formData)
public upload = (memberId: string, item: NzUploadXHRArgs) =>
https(ORIGIN)
.project(environment.build5Token)
.uploadFile(<Blob>item.postFile, memberId, item.file.uid)
.pipe(
map((b: any) => {
map((r) => {
if (item.onSuccess) {
item.onSuccess(b.data.url, item.file, b.data.url);
item.onSuccess(r.url, item.file, r.url);
}
}),
)
.subscribe();
}
}
4 changes: 2 additions & 2 deletions src/app/pages/space/pages/upsert/upsert.page.html
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ <h1 class="mt-7" *ngIf="editMode" i18n>Edit space</h1>
[nzDirectory]="false"
nzType="drag"
[nzLimit]="1"
[nzCustomRequest]="uploadFile.bind(this, 'space_avatar')"
[nzCustomRequest]="uploadFile.bind(this)"
(nzChange)="uploadChangeAvatar($event)"
nzFileType="image/png,image/jpg,image/jpeg,image/webp"
[nzShowUploadList]="true"
Expand Down Expand Up @@ -210,7 +210,7 @@ <h1 class="mt-7" *ngIf="editMode" i18n>Edit space</h1>
nzListType="picture-card"
[nzDirectory]="false"
[nzLimit]="1"
[nzCustomRequest]="uploadFile.bind(this, 'space_banner')"
[nzCustomRequest]="uploadFile.bind(this)"
(nzChange)="uploadChangeBanner($event)"
nzFileType="image/png,image/jpg,image/jpeg,image/webp"
[nzPreviewFile]="previewFile"
Expand Down
2 changes: 1 addition & 1 deletion src/app/pages/space/pages/upsert/upsert.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class UpsertPage implements OnInit {
}
}

public uploadFile(type: 'space_avatar' | 'space_banner', item: NzUploadXHRArgs): Subscription {
public uploadFile(item: NzUploadXHRArgs): Subscription {
if (!this.auth.member$.value) {
const err = $localize`Member seems to log out during the file upload request.`;
this.nzNotification.error(err, '');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@
[nzDirectory]="false"
nzType="drag"
[nzLimit]="1"
[nzCustomRequest]="newService.uploadFile.bind(newService, 'token_icon')"
[nzCustomRequest]="newService.uploadFile.bind(newService)"
(nzChange)="newService.uploadChangeIcon($event)"
nzFileType="image/png,image/jpg,image/jpeg,image/webp"
[nzShowUploadList]="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@
nzType="drag"
nzListType="picture-card"
[nzLimit]="1"
[nzCustomRequest]="newService.uploadFile.bind(newService, 'token_introductionary')"
[nzCustomRequest]="newService.uploadFile.bind(newService)"
(nzChange)="newService.uploadChangeIntroductionary($event)"
nzFileType="image/png,image/jpg,image/jpeg,image/webp,video/mp4"
[nzShowUploadList]="{
Expand Down
5 changes: 1 addition & 4 deletions src/app/pages/token/services/new.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,7 @@ export class NewService {
}
}

public uploadFile(
type: 'token_icon' | 'token_introductionary',
item: NzUploadXHRArgs,
): Subscription {
public uploadFile(item: NzUploadXHRArgs): Subscription {
if (!this.auth.member$.value) {
const err = $localize`Member seems to log out during the file upload request.`;
this.nzNotification.error(err, '');
Expand Down
Loading