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
+1
View File
@@ -97,3 +97,4 @@ src/app/pipes/process.service.ts
src/app/domain
_src/
-src
plugin copy
+47467 -1642
View File
File diff suppressed because it is too large Load Diff
+1
View File
@@ -175,6 +175,7 @@
"rocket.chat.realtime.api.rxjs": "^2.1.1",
"rxjs": "~6.6.3",
"rxjs-compat": "^6.6.7",
"sanitize-filename-ts": "^1.0.2",
"sharp": "^0.30.7",
"socket.io-client": "^2.3.0",
"tinymce": "^6.6.0",
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { MiddlewareRemoteDatasourcecService } from './middleware-remote-datasourcec.service';
describe('MiddlewareRemoteDatasourcecService', () => {
let service: MiddlewareRemoteDatasourcecService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(MiddlewareRemoteDatasourcecService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
@@ -0,0 +1,17 @@
import { Injectable } from '@angular/core';
import { SessionStore } from "src/app/store/session.service"
import { MiddlewareServiceService } from "src/app/shared/API/middleware/middleware-service.service"
@Injectable({
providedIn: 'root'
})
export class MiddlewareRemoteDatasourcecService {
constructor(
private MiddlewareServiceService: MiddlewareServiceService,
) {}
refreshToken() {
const refreshToken = SessionStore.user.Authorization
return this.MiddlewareServiceService.refreshToken(refreshToken)
}
}
+2
View File
@@ -413,6 +413,8 @@ const routes: Routes = [
canActivate: [InactivityGuard]
},
{ path: '**', redirectTo: '/', pathMatch: 'full' },
];
@NgModule({
+5 -2
View File
@@ -20,7 +20,8 @@ import { RouteService } from 'src/app/services/route.service';
import { NetworkServiceService, ConnectionStatus } from 'src/app/services/network-service.service';
import { UserSession } from '../models/user.model';
import { PermissionList } from '../models/permission/permissionList';
// import { ChunkService } from "src/app/services/stream/chunk.service"
// import { StreamService } from "src/app/services/stream/stream.service"
import { Plugins } from '@capacitor/core';
const { App } = Plugins;
@@ -92,7 +93,9 @@ export class HomePage implements OnInit {
private NetworkServiceService: NetworkServiceService,
public eventService: EventsService,
public ActiveTabService: ActiveTabService,
private RoleIdService: RoleIdService
private RoleIdService: RoleIdService,
// private ChunkService: ChunkService,
// private StreamService: StreamService
) {
if (SessionStore.exist) {
this.user = SessionStore.user;
-1
View File
@@ -228,7 +228,6 @@
<!-- {{year.yearInfo.yearName}} -->
<div *ngFor="let month of year.months " class="header-day" >
<!-- {{ month.monthInfo.monthName | json }} -->
<!-- <hr> -->
<div *ngFor="let day of month.days " class="EventListBox-container" >
@@ -32,6 +32,10 @@ import { SessionStore } from 'src/app/store/session.service';
import { ViewMediaPage } from 'src/app/modals/view-media/view-media.page';
import { Camera, CameraResultType, CameraSource } from '@capacitor/camera';
import { RouteService } from 'src/app/services/route.service';
import { FileValidatorService } from "src/app/services/file/file-validator.service"
import { sanitize } from "sanitize-filename-ts";
@Component({
selector: 'app-group-messages',
templateUrl: './group-messages.page.html',
@@ -104,6 +108,7 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy {
private file: File,
private fileOpener: FileOpener,
public RouteService: RouteService,
private FileValidatorService: FileValidatorService
) {
this.ChatSystemService.getUser()
@@ -507,7 +512,7 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy {
"mimeType": audioFile.value.mimeType,
},
attachments: [{
"title": fileName,
"title": sanitize(fileName),
"title_link_download": true,
"type": "audio"
}],
@@ -828,6 +833,11 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy {
const file: any = await this.fileService.getFileFromDevice(types);
const fileName = file.name
const validation = this.FileValidatorService.fileNameValidation(fileName)
if(validation.isOk) {
if (file.type != "application/img" && file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/gif") {
@@ -846,8 +856,8 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy {
"guid": '',
},
attachments: [{
"title": file.name,
"name": file.name,
"title": sanitize(fileName),
"name": sanitize(fileName),
// "text": "description",
"title_link_download": false,
}],
@@ -860,6 +870,9 @@ export class GroupMessagesPage implements OnInit, AfterViewInit, OnDestroy {
} else {
}
} else {
this.toastService._badRequest("ficheiro invalido")
}
}
+15 -12
View File
@@ -33,6 +33,10 @@ import { Filesystem, Directory } from '@capacitor/filesystem';
import { NewEventPage } from '../../agenda/new-event/new-event.page';
import { NotificationsService } from 'src/app/services/notifications.service';
import { RochetChatConnectorService } from 'src/app/services/chat/rochet-chat-connector.service'
import { FileValidatorService } from "src/app/services/file/file-validator.service"
import { sanitize } from "sanitize-filename-ts";
const IMAGE_DIR = 'stored-images';
@@ -117,6 +121,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
private fileOpener: FileOpener,
private router: Router,
public RochetChatConnectorService: RochetChatConnectorService,
private FileValidatorService: FileValidatorService
) {
try {
@@ -413,12 +418,13 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
this.audioRecorded = `data:${recordData.value.mimeType};base64,${recordData?.value?.recordDataBase64}`;
}
//Converting base64 to blob
const encodedData = btoa(this.audioRecorded);
const blob = this.fileService.base64toBlob(encodedData, recordData.value.mimeType)
const validation = await this.FileValidatorService.validateAudioFromBlob(blob)
const formData = new FormData();
formData.append("blobFile", blob);
@@ -429,7 +435,7 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
"mimeType": audioFile.value.mimeType,
},
attachments: [{
"title": fileName,
"title": sanitize(fileName),
"title_link_download": true,
"type": "audio"
}],
@@ -759,7 +765,6 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
}
async addFileToChatMobile(types: typeof FileType[]) {
console.log('add image from gallery')
const roomId = this.roomId
const file = await Camera.getPhoto({
@@ -852,17 +857,17 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
formData.append('blobFile', blob);
console.log('add file', fileBase64) */
const fileName = file.name
const validation = this.FileValidatorService.fileNameValidation(fileName)
if (file.type != "application/img" && file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/gif") {
console.log('TYPE', file.type)
if(validation.isOk) {
const encodedData = btoa(JSON.stringify(await this.getBase64(file).catch((error) => {
console.error(error);
})));
console.log(encodedData)
const blob = this.fileService.base64toBlob(encodedData, file.type)
console.log('BLOB BLOB', blob)
const formData = new FormData();
formData.append('blobFile', blob);
@@ -874,8 +879,8 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
"guid": '',
},
attachments: [{
"title": file.name,
"name": file.name,
"title": sanitize(fileName),
"name": sanitize(fileName),
// "text": "description",
"title_link_download": false,
}],
@@ -885,11 +890,9 @@ export class MessagesPage implements OnInit, AfterViewInit, OnDestroy {
}
});
} else {
console.log('file not supported')
this.toastService._badRequest("ficheiro invalido")
}
}
getBase64(file) {
@@ -153,16 +153,16 @@ export class AttendeesPageModal implements OnInit {
this.showLoader = false;
} else {
this.contacts = result;
console.log('Attendes Email',this.loggeduser.Email)
// console.log('Attendes Email',this.loggeduser.Email)
let filterLoggedUserEmail = this.contacts.filter(item => item.EmailAddress.toLocaleLowerCase() != this.loggeduser.Email.toLocaleLowerCase())
if(this.taskType == 0 || this.taskType == 1){
filterLoggedUserEmail = this.contacts.filter(item => item.IsPR == false)
}
console.log('Attendes Email', filterLoggedUserEmail)
// console.log('Attendes Email', filterLoggedUserEmail)
let filterEmptyEmail = filterLoggedUserEmail.filter(item => item.EmailAddress.toLocaleLowerCase() != "")
this.contacts = filterEmptyEmail;
console.log('Attendes Email', this.contacts)
//console.log('Attendes Email', this.contacts)
this.showLoader = false;
}
@@ -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');
@@ -135,15 +136,26 @@ export class NewPublicationPage implements OnInit {
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) => {
console.log('taked: ', picture)
this.capturedImage = picture
});
} else if(validation.isError) {
validation.error
this.toastService._badRequest("imagem invalida")
}
}
async laodPicture() {
@@ -156,6 +168,10 @@ export class NewPublicationPage implements OnInit {
this.capturedImage = 'data:image/jpeg;base64,' +capturedImage.base64String;
this.capturedImageTitle = 'foto';
const validation = await this.FileValidatorService.ValidateImage(this.capturedImage)
if(validation.isOk) {
const compressedImage = await this.compressImageBase64(
this.capturedImage,
800, // maxWidth
@@ -165,6 +181,10 @@ export class NewPublicationPage implements OnInit {
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
}
@@ -436,3 +432,4 @@ export class NewPublicationPage implements OnInit {
}
}
@@ -19,6 +19,9 @@
<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">
@@ -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"];
@@ -218,6 +225,50 @@ export class PublicationsPage implements OnInit {
}
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,
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { MiddlewareRepositoryService } from './middleware-repository.service';
describe('MiddlewareRepositoryService', () => {
let service: MiddlewareRepositoryService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(MiddlewareRepositoryService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
@@ -0,0 +1,18 @@
import { Injectable } from '@angular/core';
import { MiddlewareRemoteDatasourcecService } from "src/app/datasource/middleware/remote/middleware-remote-datasourcec.service"
@Injectable({
providedIn: 'root'
})
export class MiddlewareRepositoryService {
constructor(
private MiddlewareRemoteDatasourcecService: MiddlewareRemoteDatasourcecService,
) {}
refreshToken() {
return this.MiddlewareRemoteDatasourcecService.refreshToken()
}
}
+10
View File
@@ -0,0 +1,10 @@
export type Either<T, E> = { isOk: true; isError: false, value: T } | { isOk: false; isError: true, error: E };
export function ok<T, E>(value: T): Either<T, E> {
return { isOk: true, isError: false, value };
}
export function error<T, E>(error: E): Either<T, E> {
return { isOk: false, isError: true, error};
}
+1 -1
View File
@@ -64,7 +64,7 @@ export class AuthService {
const data = SessionStore.getDataFromLocalStorage();
if (!data?.user?.Authorization && SessionStore?.user?.Authorization) {
window.location.reload();
// window.location.reload();
}
if (window['all-process-gabinete']) {
@@ -151,12 +151,12 @@ export class AttendeesPageModal implements OnInit {
this.showLoader = false;
} else {
this.contacts = result;
console.log('Attendes Email',this.loggeduser.Email)
// console.log('Attendes Email',this.loggeduser.Email)
let filterLoggedUserEmail = this.contacts.filter(item => item.EmailAddress.toLocaleLowerCase() != this.loggeduser.Email.toLocaleLowerCase())
console.log('Attendes Email', filterLoggedUserEmail)
// console.log('Attendes Email', filterLoggedUserEmail)
let filterEmptyEmail = filterLoggedUserEmail.filter(item => item.EmailAddress.toLocaleLowerCase() != "")
this.contacts = filterEmptyEmail;
console.log('Attendes Email', this.contacts)
// console.log('Attendes Email', this.contacts)
this.showLoader = false;
}
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { FileValidatorService } from './file-validator.service';
describe('FileValidatorService', () => {
let service: FileValidatorService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(FileValidatorService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
@@ -0,0 +1,173 @@
import { Injectable } from '@angular/core';
import { Either, error, ok} from "src/app/services/Either"
import * as Sentry from '@sentry/capacitor';
@Injectable({
providedIn: 'root'
})
export class FileValidatorService {
forbiddenExtensions = [
".exe",
".bat",
".sh",
".jar",
".js",
".vbs",
".ps1",
".ini",
".config",
".zip",
".rar",
".tar.gz",
".7z",
".cab",
".sql",
".bak",
".htaccess",
".htpasswd",
".scr",
".pif",
".com",
".msi",
".dll",
".sys",
".ini",
".docm",
".xlsm",
".pptm",
".rtf",
".so",
".dylib",
".dat",
".log",
".conf",
".php",
".py",
".rb",
];
constructor() { }
private base64ToBlob(base64) {
const binaryData = atob(base64);
const arrayBuffer = new ArrayBuffer(binaryData.length);
const view = new Uint8Array(arrayBuffer);
for (let i = 0; i < binaryData.length; i++) {
view[i] = binaryData.charCodeAt(i);
}
return new Blob([arrayBuffer], { type: 'video/mp4' }); // Adjust the type as per your video format
}
ValidateImage(base64Image: string): Promise<Either<true, Event>> {
const imageDiv: HTMLImageElement = document.createElement("img")
return new Promise((resolve, reject) => {
imageDiv.onload = () => {
resolve(ok(true))
}
imageDiv.onerror = (e: Event) => {
resolve(error(e))
Sentry.captureMessage('FileValidatorService invalid image content');
}
imageDiv.src = base64Image
})
}
ValidateVideoFromBase64Data(base64Data: string) : Promise<Either<true, false>> {
const blob = this.base64ToBlob(base64Data);
return new Promise((resolve, reject) => {
const videoElement = document.createElement('video');
videoElement.src = URL.createObjectURL(blob);
// Add event listeners to handle video load and error
videoElement.addEventListener('loadeddata', () => {
resolve(ok(true))
// You can also perform additional validation checks here if needed.
});
videoElement.addEventListener('error', () => {
resolve(ok(true))
Sentry.captureMessage('FileValidatorService invalid video content');
});
})
}
ValidateVideoFromBlob(blob: Blob) : Promise<Either<true, false>> {
return new Promise((resolve, reject) => {
const videoElement = document.createElement('video');
videoElement.src = URL.createObjectURL(blob);
// Add event listeners to handle video load and error
videoElement.addEventListener('loadeddata', () => {
resolve(ok(true))
// You can also perform additional validation checks here if needed.
});
videoElement.addEventListener('error', () => {
resolve(ok(true))
Sentry.captureMessage('FileValidatorService invalid video content');
});
})
}
validateAudioFromBlob(blob: Blob): Promise<Either<true, false>> {
return new Promise((resolve, reject) => {
const audioElement = new Audio();
// Add event listeners to handle audio load and error
audioElement.addEventListener('canplaythrough', () => {
console.log('Valid audio');
resolve(ok(true))
// You can also perform additional validation checks here if needed.
});
audioElement.addEventListener('error', () => {
console.log('Invalid audio');
resolve(error(false))
});
audioElement.src = URL.createObjectURL(blob);
})
}
hasDuplicateExtension(filename): Either<true, false> {
// Use a regular expression to match multiple consecutive dots
const duplicateExtensionRegex = /\.\.+/;
const a = duplicateExtensionRegex.test(filename);
if(a) {
return ok(true)
} else {
Sentry.captureMessage('FileValidatorService invalid filename '+ filename);
return error(false)
}
}
fileNameValidation(fileName): Either<true, false> {
const fileExtension = fileName.slice(((fileName.lastIndexOf(".") - 1) >>> 0) + 2);
const found = this.forbiddenExtensions.includes(`.${fileExtension.toLowerCase()}`);
if(found) {
return error(false)
} else {
return ok(true)
}
}
}
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { HttpServiceService } from './http-service.service';
describe('HttpServiceService', () => {
let service: HttpServiceService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(HttpServiceService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
@@ -0,0 +1,43 @@
import { HttpClient, HttpContext, HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
// import { Result, err, ok } from 'neverthrow'
import { tap, shareReplay, catchError } from "rxjs/operators";
import { Observable, of } from "rxjs";
@Injectable({
providedIn: 'root'
})
export class HttpServiceService {
constructor(private http: HttpClient) {}
put(url: string, body: any | null, options: Options): Observable<any> {
return this.http.put(url, body, options as any).pipe(
tap((response) => {
// Handle success response if needed
}),
catchError((error) => {
// Handle error response if needed
return of(error);
})
);
}
}
interface Options {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
context?: HttpContext;
observe?: 'body';
params?: HttpParams | {
[param: string]: string | number | boolean | ReadonlyArray<string | number | boolean>;
};
reportProgress?: boolean;
responseType?: 'arraybuffer';
withCredentials?: boolean;
}
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { TokenInterceptorService } from './token-interceptor.service';
describe('TokenInterceptorService', () => {
let service: TokenInterceptorService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(TokenInterceptorService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
@@ -0,0 +1,81 @@
import { Injectable } from "@angular/core";
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor,
HttpErrorResponse,
HTTP_INTERCEPTORS,
} from "@angular/common/http";
import { Observable, throwError, BehaviorSubject } from "rxjs";
import { catchError, filter, take, switchMap } from "rxjs/operators";
import { SessionStore } from "src/app/store/session.service";
import { MiddlewareRepositoryService } from "src/app/repository/middleWare/middleware-repository.service"
@Injectable({
providedIn: 'root'
})
export class TokenInterceptorService {
private isRefreshing = false;
private refreshTokenSubject: BehaviorSubject<any> = new BehaviorSubject<any>(
null
);
constructor(private middlewareRepositoryService: MiddlewareRepositoryService) {
this.middlewareRepositoryService = middlewareRepositoryService
}
intercept(
request: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
if (SessionStore.user.Authorization) {
request = this.addToken(request, SessionStore.user.Authorization);
}
return next.handle(request).pipe(
catchError((error) => {
if (error instanceof HttpErrorResponse && error.status === 401) {
return this.handle401Error(request, next);
} else {
return throwError(error);
}
})
) as any
}
private addToken(request: HttpRequest<any>, token: string) {
return request.clone({
setHeaders: {
Authorization: `Bearer ${token}`,
},
});
}
private handle401Error(request: HttpRequest<any>, next: HttpHandler) {
if (!this.isRefreshing) {
this.isRefreshing = true;
this.refreshTokenSubject.next(null);
return this.middlewareRepositoryService.refreshToken().pipe(
switchMap((token: any) => {
this.isRefreshing = false;
this.refreshTokenSubject.next(token['result'].accessToken);
return next.handle(this.addToken(request, token['result'].accessToken));
})
);
} else {
return this.refreshTokenSubject.pipe(
filter((token) => token != null),
take(1),
switchMap((jwt) => {
return next.handle(this.addToken(request, jwt));
})
);
}
}
}
@@ -239,6 +239,8 @@ export class NotificationsService {
}
);
} else {
try {
navigator.serviceWorker.onmessage = (event) => {
console.log('Mensagem recebida do Service Worker:', event.data.data);
let object = {
@@ -250,6 +252,9 @@ export class NotificationsService {
this.notificatinsRoutes(object)
}
};
} catch(e) {
console.log(e)
}
}
}
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { ChunkService } from './chunk.service';
describe('ChunkService', () => {
let service: ChunkService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(ChunkService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
+140
View File
@@ -0,0 +1,140 @@
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class ChunkService {
constructor() {
window["ChunkService"] = this
// const a = this.generateChunkFile({} as any, {} as any)
}
generateChunkFile(base64: string, chunkSizeInBytes: number): File[] {
const byteCharacters = atob(base64); // Decode base64 data to binary
const chunks: File[] = [];
let start = 0;
function createChunk(): File {
const chunkSize = Math.min(chunkSizeInBytes, byteCharacters.length - start);
const chunkData = byteCharacters.slice(start, start + chunkSize);
start += chunkSize;
// console.log({chunkData})
const chunkArray = new Uint8Array(chunkData.length);
for (let i = 0; i < chunkData.length; i++) {
chunkArray[i] = chunkData.charCodeAt(i);
// console.log('chunkData.charCodeAt', chunkData.charCodeAt(i))
}
// console.log({Uint8Array:"Uint8Array", chunkArray})
const blob = new Blob([chunkArray]);
// console.log('blob')
// console.log('File')
return new File([blob], `chunk_${chunks.length + 1}`);
}
while (start < byteCharacters.length) {
chunks.push(createChunk());
}
return chunks;
}
generateChunkOfUint8Array(base64: string, chunkSizeInBytes: number): Uint8Array[] {
const byteCharacters = atob(base64); // Decode base64 data to binary
const chunks: Uint8Array[] = [];
let start = 0;
function createChunk(): Uint8Array {
const chunkSize = Math.min(chunkSizeInBytes, byteCharacters.length - start);
const chunkData = byteCharacters.slice(start, start + chunkSize);
start += chunkSize;
const chunkArray = new Uint8Array(chunkData.length);
for (let i = 0; i < chunkData.length; i++) {
chunkArray[i] = chunkData.charCodeAt(i);
}
return chunkArray;
}
while (start < byteCharacters.length) {
chunks.push(createChunk());
}
return chunks;
}
async uploadChunk(file: File): Promise<any> {
// Read and upload the file in chunks (as you've previously implemented)
const chunkSize = 1024 * 500; // Adjust the chunk size as needed
const chunks = [];
let offset = 0;
let i = 0
while (offset < file.size) {
console.log(offset)
const chunk = file.slice(offset, offset + chunkSize);
const reader = new FileReader();
reader.onload = () => {
chunks[i] = new Uint8Array(reader.result as ArrayBuffer)
};
reader.readAsArrayBuffer(chunk);
offset += chunkSize;
}
return chunks
}
onFileSelect(event: any, chunkSizeInBytes):Promise<Blob[]> {
const file:File = event.target.files[0];
const filename = file.name;
//const chunkSize = 1024 * 1024; // 1 MB chunks (adjust as needed)
const chunkSize = chunkSizeInBytes
return new Promise((resolve, reject) => {
// Read and upload chunks
const fileReader = new FileReader();
fileReader.onload = (e) => {
const arrayBuffer = e.target.result as ArrayBuffer;
const blob = new Blob([new Uint8Array(arrayBuffer)]);
const totalChunks = Math.ceil(file.size / chunkSize);
const chunks: Blob[] = []
for (let i = 1; i <= totalChunks; i++) {
const start = (i - 1) * chunkSize;
const end = i * chunkSize;
const chunk = blob.slice(start, end);
chunks.push(chunk)
}
resolve(chunks)
};
fileReader.readAsArrayBuffer(file);
})
}
generateChunkFromBase64(base64: string, chunkSizeInBytes: number) {
const byteCharacters = atob(base64); // Decode base64 data to binary
const chunkArray = [];
for (let offset = 0; offset < byteCharacters.length; offset += chunkSizeInBytes) {
const chunkData = byteCharacters.slice(offset, offset + chunkSizeInBytes);
chunkArray.push(chunkData);
}
return chunkArray;
}
}
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { StreamService } from './stream.service';
describe('StreamService', () => {
let service: StreamService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(StreamService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
+112
View File
@@ -0,0 +1,112 @@
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpEventType } from '@angular/common/http';
@Injectable({
providedIn: 'root'
})
export class StreamService {
constructor(
private http: HttpClient,
) {
window["StreamService"] = this
}
async uploadFile() {
const API_URL = 'http://localhost:3000/upload'; // Replace with your server URL
const filePath = 'path/to/large-file.zip'; // Replace with the path to your file
const fileName = 'my-file'; // Specify your desired filename
const fileExtension = 'zip'; // Specify the file extension
const headers = new HttpHeaders()
.append('X-File-Name', fileName)
.append('X-File-Extension', fileExtension);
const file = await this.readFileInChunks(filePath);
const chunkSize = 1024 * 1024; // 1 MB chunk size (adjust as needed)
for (let offset = 0; offset < file.length; offset += chunkSize) {
const chunk = file.slice(offset, offset + chunkSize);
// await this.uploadChunk(API_URL, chunk, headers);
}
console.log('Upload completed.');
}
async readFileInChunks(filePath: string): Promise<Uint8Array> {
const response = await fetch(filePath);
const reader = response.body.getReader();
const chunks: Uint8Array[] = [];
let done = false;
while (!done) {
const { value, done: isDone } = await reader.read();
if (!isDone) {
chunks.push(value);
}
done = isDone;
}
return new Uint8Array([].concat(...chunks.map((chunk) => Array.from(chunk))));
}
async uploadChunk(url: string, chunks: Uint8Array[], fileName, fileExtension): Promise<void> {
let i = 1
console.log('123', chunks.length)
for(const chunk of chunks) {
try {
console.log("iterate")
const headers = new HttpHeaders()
.append('X-File-Name', fileName)
.append('X-File-Extension', fileExtension)
.append('X-File-Content-Length', chunks.length.toString())
.append('X-File-Index', i.toString())
await this.http.post('http://localhost:3001/upload', chunk.buffer, { headers, responseType: 'blob' }).toPromise();
i++
} catch (error) {
console.error('Upload error:', error);
}
}
}
async uploadChunkNoLoop(url: string, chunk: Uint8Array, fileName, fileExtension, i, length): Promise<void> {
console.log("iterate")
const headers = new HttpHeaders()
.append('X-File-Name', fileName)
.append('X-File-Extension', fileExtension)
.append('X-File-Content-Length', length)
.append('X-File-Index', i.toString())
await this.http.post('http://localhost:3001/upload', chunk.buffer, { headers, responseType: 'blob' }).toPromise();
}
uploadChunk1(chunk: Blob, chunkNumber: number, totalChunks: number, filename: string) {
console.log(chunk)
const headers = new HttpHeaders()
.append('X-File-Name', filename)
.append('X-File-Content-Length', totalChunks.toString())
.append('X-File-Index', chunkNumber.toString())
return this.http.post('http://localhost:3001/upload-chunk', Blob, { headers, responseType: 'blob' });
}
}
// const text = 'Hello, World00120301010asdf1002sdf 0fsdfasf0001230 12300!\n';
// const base64 = btoa(text);
// let chunks = window["ChunkService"].generateChunkOfUint8Array(base64, 8)
// window.StreamService.uploadChunk("", chunks, "peter12", "txt")
+5
View File
@@ -0,0 +1,5 @@
export interface refreshToken {
Authorization: string,
refreshToken: null
}
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { MiddlewareServiceService } from './middleware-service.service';
describe('MiddlewareServiceService', () => {
let service: MiddlewareServiceService;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(MiddlewareServiceService);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});
@@ -0,0 +1,23 @@
import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
import { HttpServiceService } from 'src/app/services/http/http-service.service';
import { Observable } from "rxjs";
import { refreshToken } from "./interface"
@Injectable({
providedIn: 'root'
})
export class MiddlewareServiceService {
constructor(
private HttpServiceService: HttpServiceService,
) {}
refreshToken(refreshToken: string): Observable<refreshToken> {
const data = {
refreshToken: refreshToken
}
return this.HttpServiceService.put(environment.apiURL + "UserAuthentication/RefreshToken", data, {})
}
}
@@ -29,6 +29,7 @@ import { FileOpener } from '@awesome-cordova-plugins/file-opener/ngx';
import { SessionStore } from 'src/app/store/session.service';
import { ViewMediaPage } from 'src/app/modals/view-media/view-media.page';
import { PermissionService } from 'src/app/services/permission.service';
import { FileValidatorService } from "src/app/services/file/file-validator.service"
@Component({
selector: 'app-group-messages',
@@ -105,6 +106,7 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe
private platform: Platform,
private fileOpener: FileOpener,
public p: PermissionService,
private FileValidatorService: FileValidatorService
) {
this.ChatSystemService.getUser()
@@ -901,9 +903,11 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe
const file: any = await this.fileService.getFileFromDevice(types);
const fileName = file.name
const validation = this.FileValidatorService.fileNameValidation(fileName)
if (file.type != "application/img" && file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/gif") {
if(validation.isOk) {
const encodedData = btoa(JSON.stringify(await this.getBase64(file).catch ((error) => {
console.error(error);
@@ -946,9 +950,8 @@ export class GroupMessagesPage implements OnInit, OnChanges, AfterViewInit, OnDe
fileBase64: fileBase64,
}
})
} else {
this.toastService._badRequest("ficheiro invalido")
}
@@ -33,6 +33,7 @@ import { Howl } from 'howler';
import { ViewMediaPage } from 'src/app/modals/view-media/view-media.page';
import { ChatMessageDebuggingPage } from 'src/app/shared/popover/chat-message-debugging/chat-message-debugging.page';
import { PermissionService } from 'src/app/services/permission.service';
import { FileValidatorService } from "src/app/services/file/file-validator.service"
const IMAGE_DIR = 'stored-images';
@@ -122,6 +123,7 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
private platform: Platform,
private fileOpener: FileOpener,
public p: PermissionService,
private FileValidatorService: FileValidatorService
) {
// update
this.checkAudioPermission()
@@ -835,11 +837,12 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
const roomId = this.roomId
const file: any = await this.fileService.getFileFromDevice(types);
console.log(file.type)
const fileName = file.name
const validation = this.FileValidatorService.fileNameValidation(fileName)
if (file.type != "application/img" && file.type != "image/png" && file.type != "image/jpeg" && file.type != "image/gif") {
if(validation.isOk) {
const encodedData = btoa(JSON.stringify(await this.getBase64(file).catch((error) => {
console.error(error);
@@ -886,12 +889,12 @@ export class MessagesPage implements OnInit, OnChanges, AfterViewInit, OnDestroy
fileBase64: fileBase64,
}
})
} else {
this.toastService._badRequest("ficheiro invalido")
}
}
_getBase64(file) {
@@ -247,15 +247,15 @@ export class AttendeePage implements OnInit {
this.showLoader = false;
} else {
this.contacts = this.sort(result as any);
console.log('Attendes Email', this.loggeduser.Email)
// console.log('Attendes Email', this.loggeduser.Email)
let filterLoggedUserEmail = this.contacts.filter(item => item.EmailAddress.toLocaleLowerCase() != this.loggeduser.Email.toLocaleLowerCase())
if(this.taskType == 0 || this.taskType == 1){
filterLoggedUserEmail = this.contacts.filter(item => item.IsPR == false)
}
console.log('Attendes Email', filterLoggedUserEmail)
// console.log('Attendes Email', filterLoggedUserEmail)
let filterEmptyEmail = filterLoggedUserEmail.filter(item => item.EmailAddress.toLocaleLowerCase() != "")
this.contacts = filterEmptyEmail;
console.log('Attendes Email', this.contacts)
// console.log('Attendes Email', this.contacts)
this.showLoader = false;
}
@@ -10,6 +10,7 @@ import { ThemeService } from 'src/app/services/theme.service';
import { Camera, CameraResultType, CameraSource} from '@capacitor/camera';
import { HttpErrorHandle } from 'src/app/services/http-error-handle.service';
import { PublicationFolderService } from 'src/app/store/publication-folder.service';
import { FileValidatorService } from "src/app/services/file/file-validator.service"
@Component({
selector: 'app-new-publication',
@@ -50,7 +51,8 @@ export class NewPublicationPage implements OnInit {
private toastService: ToastService,
public ThemeService: ThemeService,
private httpErroHandle: HttpErrorHandle,
public PublicationFolderService: PublicationFolderService
public PublicationFolderService: PublicationFolderService,
private FileValidatorService: FileValidatorService
) {
this.publicationTitle = 'Nova Publicação';
@@ -115,6 +117,10 @@ export class NewPublicationPage implements OnInit {
this.capturedImage = 'data:image/jpeg;base64,' +capturedImage.base64String;
this.capturedImageTitle = 'foto';
const validation = await this.FileValidatorService.ValidateImage(this.capturedImage)
if(validation.isOk) {
const compressedImage = await this.compressImageBase64(
this.capturedImage,
800, // maxWidth
@@ -124,6 +130,10 @@ export class NewPublicationPage implements OnInit {
console.log('taked: ', picture)
this.capturedImage = picture
});
} else {
this.toastService._badRequest("imagem invalida")
}
}
async laodPicture() {
@@ -136,6 +146,11 @@ export class NewPublicationPage implements OnInit {
this.capturedImage = 'data:image/jpeg;base64,' +capturedImage.base64String;
this.capturedImageTitle = 'foto';
const validation = await this.FileValidatorService.ValidateImage(this.capturedImage)
if(validation.isOk) {
const compressedImage = await this.compressImageBase64(
this.capturedImage,
800, // maxWidth
@@ -145,6 +160,10 @@ export class NewPublicationPage implements OnInit {
console.log('Selected: ', picture)
this.capturedImage = picture
});
} else {
this.toastService._badRequest("imagem invalida")
}
}
+13 -7
View File
@@ -1,19 +1,20 @@
// inspire by https://github.com/hc-oss/use-indexeddb
export class IndexedDBConnector {
static connect(config) {
return new Promise((resolve, reject) => {
return new Promise(async (resolve, reject) => {
const idbInstance = indexedDB || self.indexedDB || self.mozIndexedDB || self.webkitIndexedDB || self.msIndexedDB;
if (idbInstance) {
const request = idbInstance.open(config.databaseName, config.version);
request.onsuccess = () => {
resolve(request.result);
return resolve(request.result);
};
request.onerror = (e) => {
reject(e.target.error.name);
};
request.onupgradeneeded = async (e) => {
await this.migrate(config);
return await this.connect(config);
const db = e.target.result;
await this.runMigrations(db, config);
resolve(await this.connect(config))
};
// request.onblocked = async (e: any) => {
// reject(e.target.error.name);
@@ -25,18 +26,23 @@ export class IndexedDBConnector {
});
}
static migrate(config) {
return new Promise((resolve, reject) => {
return new Promise(async (resolve, reject) => {
const idbInstance = indexedDB || self.indexedDB || self.mozIndexedDB || self.webkitIndexedDB || self.msIndexedDB;
if (idbInstance) {
const request = idbInstance.open(config.databaseName, config.version);
request.onsuccess = () => {
// request.result.close()
request.onsuccess = async () => {
resolve(false);
};
request.onerror = (e) => {
//console.log("error migrate " + config.databaseName)
//console.log("error migrate"+ e.target.error)
reject(e.target.error.name);
};
request.onupgradeneeded = async (e) => {
// console.log("migrate create db")
const db = e.target.result;
await this.runMigrations(db, config);
db.close();
+18
View File
@@ -13,5 +13,23 @@
</rule>
</rules>
</rewrite>
<httpProtocol>
<customHeaders>
<!-- Iframe on the same host only -->
<add name="X-Frame-Options" value="SAMEORIGIN" />
<!-- HTTPS on -->
<add name="Strict-Transport-Security" value="max-age=31536000" />
<add name="Referrer-Policy" value="no-referrer" />
<add name="X-Content-Type-Options" value="nosniff" />
<!-- <add e="Expect-CT" value="enforce, max-age=86400" /> -->
<!-- Permision -->
<add name="Permissions-Policy" value="camera=(), microphone=()" />
<!-- same origin only -->
<add name="Cross-Origin-Embedder-Policy" value="require-corp" />
<!-- same origin only -->
<add name="Cross-Origin-Opener-Policy" value="same-origin" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>