add file chuck upload, file validation, redirect to home page incase route doesnt exist and refresh token interceptor

This commit is contained in:
Peter Maquiran
2023-11-09 11:45:04 +01:00
parent a05f85a37f
commit a16e97a59a
41 changed files with 48604 additions and 1902 deletions
@@ -23,7 +23,7 @@ import { PublicationFolderService } from 'src/app/store/publication-folder.servi
import { RouteService } from 'src/app/services/route.service';
import { FileService } from 'src/app/services/functions/file.service';
import { readAndCompressImage } from 'browser-image-resizer';
import { FileValidatorService } from "src/app/services/file/file-validator.service"
const config = {
quality: 0.5,
maxWidth: 800,
@@ -100,7 +100,8 @@ export class NewPublicationPage implements OnInit {
private httpErrorHandle: HttpErrorHandle,
public PublicationFolderService: PublicationFolderService,
private RouteService: RouteService,
public FileService: FileService
public FileService: FileService,
private FileValidatorService: FileValidatorService
) {
this.publicationType = this.navParams.get('publicationType');
@@ -132,18 +133,29 @@ export class NewPublicationPage implements OnInit {
source: CameraSource.Camera
});
this.capturedImage = 'data:image/jpeg;base64,' +capturedImage.base64String;
this.capturedImageTitle = 'foto';
const compressedImage = await this.compressImageBase64(
this.capturedImage,
800, // maxWidth
800, // maxHeight
0.9 // quality
).then((picture) => {
console.log('taked: ', picture)
this.capturedImage = picture
});
this.capturedImage = 'data:image/jpeg;base64,' +capturedImage.base64String;
this.capturedImageTitle = 'foto';
const validation = await this.FileValidatorService.ValidateImage(this.capturedImage)
if(validation.isOk) {
validation.value
const compressedImage = await this.compressImageBase64(
this.capturedImage,
800, // maxWidth
800, // maxHeight
0.9 // quality
).then((picture) => {
this.capturedImage = picture
});
} else if(validation.isError) {
validation.error
this.toastService._badRequest("imagem invalida")
}
}
async laodPicture() {
@@ -156,15 +168,23 @@ export class NewPublicationPage implements OnInit {
this.capturedImage = 'data:image/jpeg;base64,' +capturedImage.base64String;
this.capturedImageTitle = 'foto';
const compressedImage = await this.compressImageBase64(
this.capturedImage,
800, // maxWidth
800, // maxHeight
0.9 // quality
).then((picture) => {
console.log('Selected: ', picture)
this.capturedImage = picture
});
const validation = await this.FileValidatorService.ValidateImage(this.capturedImage)
if(validation.isOk) {
const compressedImage = await this.compressImageBase64(
this.capturedImage,
800, // maxWidth
800, // maxHeight
0.9 // quality
).then((picture) => {
console.log('Selected: ', picture)
this.capturedImage = picture
});
} else {
this.toastService._badRequest("imagem invalida")
}
}
@@ -189,30 +209,6 @@ export class NewPublicationPage implements OnInit {
});
/* // in use
async laodPicture() {
const capturedImage = await Camera.getPhoto({
quality: 90,
// allowEditing: true,
resultType: CameraResultType.Uri,
source: CameraSource.Photos
});
const response = await fetch(capturedImage.webPath!);
const blob = await response.blob();
this.convertBlobToBase64Worker.postMessage(blob);
this.convertBlobToBase64Worker.onmessage = async (oEvent)=> {
this.capturedImage = oEvent.data
this.capturedImageTitle = 'foto'
}
} */
runValidation() {
this.validateFrom = true
}
@@ -316,7 +312,7 @@ export class NewPublicationPage implements OnInit {
else {
const date = formatDate(new Date(), 'yyyy-MM-dd HH:mm:ss')
this.publication = {
DateIndex: date,
DocumentId: null,
@@ -332,7 +328,7 @@ export class NewPublicationPage implements OnInit {
const loader = this.toastService.loading()
try {
await this.publications.CreatePublication(this.folderId, this.publication).toPromise();
this.close();
this.httpErrorHandle.httpsSucessMessagge('Criar publicação')
@@ -373,7 +369,7 @@ export class NewPublicationPage implements OnInit {
this.publicationTitle = 'Editar Publicação';
this.pub = this.navParams.get('publication');
}
}
@@ -383,11 +379,11 @@ export class NewPublicationPage implements OnInit {
//this.imgResultBeforeCompress = image;s
this.imageCompress.getOrientation(this.capturedImage).then((orientation) => {
this.imageCompress.compressFile(this.capturedImage, orientation, 90, 90).then(
result => {
this.capturedImage = result;
}
);
@@ -403,36 +399,37 @@ export class NewPublicationPage implements OnInit {
return new Promise((resolve, reject) => {
const image = new (window as any).Image();
image.src = base64String;
image.onload = async () => {
const canvas = document.createElement('canvas');
let newWidth = image.width;
let newHeight = image.height;
if (newWidth > maxWidth) {
newHeight *= maxWidth / newWidth;
newWidth = maxWidth;
}
if (newHeight > maxHeight) {
newWidth *= maxHeight / newHeight;
newHeight = maxHeight;
}
canvas.width = newWidth;
canvas.height = newHeight;
const context = canvas.getContext('2d');
context?.drawImage(image, 0, 0, newWidth, newHeight);
const compressedBase64 = canvas.toDataURL('image/jpeg', quality);
resolve(compressedBase64);
};
image.onerror = (error) => {
reject(error);
};
});
}
}
@@ -19,8 +19,11 @@
<div class="title-content width-100 d-flex justify-space-between">
<div class="div-title flex-grow-1">
<ion-label class="title font-25-em">Acções</ion-label>
<!-- <div>
<input type="file" (change)="onFileSelect($event)" />
</div> -->
</div>
<div class="div-icon">
<button *ngIf="p.userPermission([p.permissionList.Actions.create])" title="Adicionar nova ação presidencial" class="btn-no-color" (click)="AddPublicationFolder()">
<ion-icon *ngIf="ThemeService.currentTheme == 'default' " slot="icon-only" src='assets/images/icons-add.svg'></ion-icon>
+63 -12
View File
@@ -14,6 +14,10 @@ import { ToastService } from 'src/app/services/toast.service';
import { ThemeService } from 'src/app/services/theme.service'
import { PermissionService } from 'src/app/services/permission.service';
import { Storage } from '@ionic/storage';
import { ChunkService } from 'src/app/services/stream/chunk.service'
import { StreamService } from 'src/app/services/stream/stream.service'
import { HttpClient, HttpHeaders, HttpEventType } from '@angular/common/http';
// import { ActionModel } from 'src/app/models/beast-orm';
@@ -68,6 +72,9 @@ export class PublicationsPage implements OnInit {
public ThemeService: ThemeService,
public p: PermissionService,
private storage: Storage,
private ChunkService: ChunkService,
private StreamService:StreamService,
private http: HttpClient,
) {
this.months = ["Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho", "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"];
@@ -143,11 +150,11 @@ export class PublicationsPage implements OnInit {
this.publications.GetPublicationFolderList().subscribe(async res => {
this.showLoader = false;
const folders: PublicationFolder[] = this.getPublicationFolderMap(res)
this.publicationsEventFolderList = folders.filter((e)=>e.ActionType == 'Evento')
await this.storage.set('actionsEvents', this.publicationsEventFolderList);
this.showLoader = false;
// (async ()=> {
@@ -189,35 +196,79 @@ export class PublicationsPage implements OnInit {
}
async getFromDB() {
//const folders: PublicationFolder[] = await ActionModel.all()
//this.showLoader = false;
// this.publicationsEventFolderList = folders
this.storage.get('actionsEvents').then((events = []) => {
if(Array.isArray(events)) {
const folders: PublicationFolder[] = this.getPublicationFolderMap(events)
this.showLoader = false;
this.publicationsEventFolderList = folders
}
}
});
this.storage.get('actionsViagens').then((viagens = []) => {
if(Array.isArray(viagens)) {
const folders: PublicationFolder[] = this.getPublicationFolderMap(viagens)
this.publicationsTravelFolderList = folders
this.showLoader = false;
}
});
}
async onFileSelect(event: any) {
const file:File = event.target.files[0];
const chunkSize = 1024 * 1024; // Adjust the chunk size as needed
const chunks = [];
let offset = 0;
let i = 0;
let j = 0;
function count () {
j++
return j
}
while (offset < file.size) {
const chunk = file.slice(offset, offset + chunkSize);
const reader = new FileReader();
reader.onload = async () => {
const headers = new HttpHeaders()
.append('X-File-Name', "fileName")
.append('X-File-Extension', "mp4")
.append('X-File-Content-Length', i.toString())
.append('X-File-Index', count().toString());
const a = new Uint8Array(reader.result as ArrayBuffer)
await this.http.post('http://localhost:3001/upload', a.buffer, { headers, responseType: 'blob' }).toPromise();
};
reader.readAsArrayBuffer(chunk);
offset += chunkSize;
i++;
}
}
async editAction(folderId?: string) {
const modal = await this.modalController.create({
component: EditActionPage,
@@ -244,7 +295,7 @@ export class PublicationsPage implements OnInit {
if(error.status == 0) {
this.toastService._badRequest('Sem acesso à internet. Por favor verifique sua conexão')
} else {
this.toastService._badRequest()
}
}
@@ -252,7 +303,7 @@ export class PublicationsPage implements OnInit {
loader.remove()
this.refreshing()
}
}
async AddPublicationFolder(item?: any) {
@@ -270,7 +321,7 @@ export class PublicationsPage implements OnInit {
cssClass: 'new-action modal modal-desktop',
backdropDismiss: false
});
modal.onDidDismiss().then(() => {
this.getActions();
});
@@ -336,7 +387,7 @@ export class PublicationsPage implements OnInit {
cssClass: 'new-action modal modal-desktop',
backdropDismiss: false
});
modal.onDidDismiss();
await modal.present();
@@ -470,7 +521,7 @@ export class PublicationsPage implements OnInit {
},
//translucent: true
});
modal.onDidDismiss().then(res => {
if (res['data'] == 'edit') {
this.closeDesktopComponent();