implement image post, put, delete;

This commit is contained in:
Marco Zeisler 2021-01-11 21:45:14 +01:00
parent 54083540d1
commit 63f7d733dc
11 changed files with 154 additions and 66 deletions

View File

@ -3334,6 +3334,21 @@
"dev": true, "dev": true,
"optional": true "optional": true
}, },
"angular-file-uploader": {
"version": "7.0.3",
"resolved": "https://registry.npmjs.org/angular-file-uploader/-/angular-file-uploader-7.0.3.tgz",
"integrity": "sha512-0wkr/3Vn3toG/2sTSiD5vJ5EK3HKYXwzyFd3M3hRegcHhQnagN34licn4r+CBWJQQED5aRWUVB9OWOCtuNwimw==",
"requires": {
"tslib": "^2.0.0"
},
"dependencies": {
"tslib": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.1.0.tgz",
"integrity": "sha512-hcVC3wYEziELGGmEEXue7D75zbwIIVUMWAVbHItGPx0ziyXxrOMQx4rQEVEV45Ut/1IotuEvwqPopzIOkDMf0A=="
}
}
},
"ansi-colors": { "ansi-colors": {
"version": "3.2.4", "version": "3.2.4",
"resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-3.2.4.tgz", "resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-3.2.4.tgz",

View File

@ -26,6 +26,7 @@
"@angular/platform-browser-dynamic": "~9.1.9", "@angular/platform-browser-dynamic": "~9.1.9",
"@angular/router": "~9.1.9", "@angular/router": "~9.1.9",
"@types/uuid": "8.3.0", "@types/uuid": "8.3.0",
"angular-file-uploader": "^7.0.3",
"bootstrap": "~4.3.1", "bootstrap": "~4.3.1",
"jquery": "3.5.1", "jquery": "3.5.1",
"ngx-logger": "4.1.9", "ngx-logger": "4.1.9",

View File

@ -18,9 +18,10 @@ import {MatCardModule} from '@angular/material/card';
import {MatPaginatorModule} from '@angular/material/paginator'; import {MatPaginatorModule} from '@angular/material/paginator';
import {MatTableModule} from '@angular/material/table'; import {MatTableModule} from '@angular/material/table';
import {MatTabsModule} from '@angular/material/tabs'; import {MatTabsModule} from '@angular/material/tabs';
import { ImagesComponent } from './component/images/images.component'; import {ImagesComponent} from './component/images/images.component';
import { MapComponent } from './component/map/map.component'; import {MapComponent} from './component/map/map.component';
import {AgmCoreModule} from '@agm/core'; import {AgmCoreModule} from '@agm/core';
import {AngularFileUploaderModule} from 'angular-file-uploader';
@NgModule({ @NgModule({
declarations: [LandingComponent, ImagesComponent, MapComponent], declarations: [LandingComponent, ImagesComponent, MapComponent],
@ -48,8 +49,8 @@ import {AgmCoreModule} from '@agm/core';
premium customer, in which case you can premium customer, in which case you can
use clientId use clientId
*/ */
}) }),
AngularFileUploaderModule,
], ],
// enables injecting // enables injecting
providers: [ providers: [

View File

@ -1,18 +1,30 @@
<h2>Available Images</h2> <h2>Available Images</h2>
<div> <div style="font-weight: bolder; font-size: 20px">
<span style="width: 9.5em; display: inline-block; margin-left: 1.5em">Name</span> <span style="width: 7.6em; display: inline-block; margin-left: 1em">Name</span>
<span style="width: 15em; display: inline-block">Date</span> <span style="width: 15em; display: inline-block">Date</span>
</div> </div>
<mat-expansion-panel *ngFor="let image of images | slice:paginationGetStart():paginationGetEnd()"> <mat-expansion-panel *ngFor="let image of images | slice:paginationGetStart():paginationGetEnd(); let i = index">
<mat-expansion-panel-header (click)="$event.stopPropagation()"> <mat-expansion-panel-header (click)="$event.stopPropagation()">
<span style="width: 10em" <span style="width: 10em"
>{{image.name}}</span> >{{image.name}}</span>
<span style="width: 15em" <span style="width: 15em"
>{{image.datetime}}</span> >{{image.datetime}}</span>
</mat-expansion-panel-header> </mat-expansion-panel-header>
TEST
<div style="width: 20em; height: 20em; border: solid; margin-bottom: 1em">
IMAGE
</div>
<angular-file-uploader style="margin-left: 0 !important;"
[config]="getImageUpdateConfig(image.filename)"
>
</angular-file-uploader>
<span style="float: right">
<button mat-raised-button color="warn" (click)="deleteImage(image)">Delete</button>
</span>
</mat-expansion-panel> </mat-expansion-panel>
<mat-paginator [length]="images.length" <mat-paginator [length]="images.length"
@ -21,3 +33,8 @@
(page)="lastPageEvent = $event" (page)="lastPageEvent = $event"
showFirstLastButtons showFirstLastButtons
></mat-paginator> ></mat-paginator>
<h3>Upload New Image</h3>
<angular-file-uploader style="margin-left: 0 !important;"
[config]="imageUploadConfig" id="generalUpload">
</angular-file-uploader>

View File

@ -1,13 +1,16 @@
import {Component, Input, OnInit} from '@angular/core'; import {AfterViewInit, Component, Input, OnInit} from '@angular/core';
import {PageEvent} from '@angular/material/paginator'; import {PageEvent} from '@angular/material/paginator';
import {ImageMetadata} from '../../interfaces/interface'; import {ImageMetadata} from '../../interfaces/interface';
import {RestService} from '../../services/rest.service';
import {NGXLogger} from 'ngx-logger';
import {AngularFileUploaderConfig} from 'angular-file-uploader';
@Component({ @Component({
selector: 'app-images', selector: 'app-images',
templateUrl: './images.component.html', templateUrl: './images.component.html',
styleUrls: ['./images.component.css'] styleUrls: ['./images.component.css']
}) })
export class ImagesComponent implements OnInit { export class ImagesComponent implements OnInit, AfterViewInit {
@Input() @Input()
images: ImageMetadata[]; images: ImageMetadata[];
@ -15,12 +18,26 @@ export class ImagesComponent implements OnInit {
pageSizeOptions: number[] = [5, 10, 25, 100]; pageSizeOptions: number[] = [5, 10, 25, 100];
lastPageEvent: PageEvent; lastPageEvent: PageEvent;
constructor() { } imageUploadConfig: AngularFileUploaderConfig = {
uploadAPI: {
url: this.restService.getPostImageUrl()
}
};
constructor(
public restService: RestService,
private logger: NGXLogger
) {
}
ngOnInit(): void { ngOnInit(): void {
this.lastPageEvent = {pageIndex: 0, pageSize: this.pageSizeOptions[0], length: this.images.length}; this.lastPageEvent = {pageIndex: 0, pageSize: this.pageSizeOptions[0], length: this.images.length};
} }
ngAfterViewInit(): void {
}
paginationGetStart(): number { paginationGetStart(): number {
return this.lastPageEvent.pageIndex * this.lastPageEvent.pageSize; return this.lastPageEvent.pageIndex * this.lastPageEvent.pageSize;
} }
@ -29,4 +46,18 @@ export class ImagesComponent implements OnInit {
return this.paginationGetStart() + this.lastPageEvent.pageSize; return this.paginationGetStart() + this.lastPageEvent.pageSize;
} }
getImageUpdateConfig(filename: string): AngularFileUploaderConfig {
return {
uploadAPI: {
url: this.restService.getUpdateImageUrl(filename),
method: 'PUT'
}
};
}
deleteImage(image: ImageMetadata) {
this.restService.deleteImage(image.filename).toPromise().then(() => {
this.images = this.images.filter(im => im !== image);
}).catch(err => this.logger.error(err));
}
} }

View File

@ -3,6 +3,7 @@ export interface StorageHealth {
} }
export interface ImageMetadata { export interface ImageMetadata {
id: string;
datetime: string; datetime: string;
device_id: string; device_id: string;
filename: string; filename: string;

View File

@ -8,34 +8,42 @@ import {StorageHealth} from '../interfaces/interface';
@Injectable() @Injectable()
export class RestService { export class RestService {
private currentLocation = 'http://' + environment.location + ':' + environment.port + '/';
private imageUrl = this.currentLocation + 'image/';
private getImageUrl = this.imageUrl + 'get/';
private deleteImageUrl = this.imageUrl + 'delete/';
private postImageUrl = this.imageUrl + 'post$/';
private updateImageUrl = this.imageUrl + 'update/';
private healthCheckUrl = this.currentLocation + 'check';
constructor( constructor(
private logger: NGXLogger, private logger: NGXLogger,
private http: HttpClient private http: HttpClient
) { ) {
} }
private currentLocation = 'http://' + environment.location + ':' + environment.port;
private imageUrl = this.currentLocation + '/image';
private getImageUrl = this.imageUrl + '/get';
private deleteImageUrl = this.imageUrl + '/delete';
private postImageUrl = this.imageUrl + '/post';
private updateImageUrl = this.imageUrl + '/update';
private healthCheckUrl = this.currentLocation + '/check';
private static stripUploadFileName(name: string) {
// const generalUploadFileName: string =
// (document.getElementById('generalUpload').firstChild as HTMLElement).children[5].children[0].textContent;
// this.logger.debug('Get filename', generalUploadFileName);
const temp: string[] = name.split('.');
return temp.slice(0, temp.length - 1).join('.');
}
public getAllImages(): Observable<any> { public getAllImages(): Observable<any> {
return this.http.get<any>(this.getImageUrl + 'all').pipe( return this.http.get<any>(this.getImageUrl + '/all').pipe(
tap(next => this.logger.debug('getAllImages', next)) tap(next => this.logger.debug('getAllImages', next))
); );
} }
public getImage(id: string): Observable<any> { public getImage(id: string): Observable<any> {
return this.http.get<any>(this.getImageUrl + id).pipe( return this.http.get<any>(this.getImageUrl + id).pipe(
tap(next => this.logger.debug('getImage', id, next)) tap(next => this.logger.debug('/getImage', id, next))
); );
} }
public getImageVersion(id: string, version: string): Observable<any> { public getImageVersion(id: string, version: string): Observable<any> {
return this.http.get<any>(this.getImageUrl + id + '/version/' + version).pipe( return this.http.get<any>(this.getImageUrl + '/' + id + '/version/' + version).pipe(
tap(next => this.logger.debug('getImageVersion', id, version, next)) tap(next => this.logger.debug('getImageVersion', id, version, next))
); );
} }
@ -46,22 +54,31 @@ export class RestService {
); );
} }
public deleteImage(id: string): Observable<any> { public deleteImage(filenameWithExtension: string): Observable<any> {
return this.http.delete<any>(this.deleteImageUrl + id).pipe( const stripped = RestService.stripUploadFileName(filenameWithExtension);
tap(next => this.logger.debug('deleteImage', id, next)) return this.http.delete<any>(this.deleteImageUrl + '/' + stripped).pipe(
tap(next => this.logger.debug('deleteImage', stripped, next))
); );
} }
public postImage(id: string, image: any): Observable<any> { // public postImage(id: string, image: any): Observable<any> {
return this.http.post<any>(this.postImageUrl + id, image).pipe( // return this.http.post<any>(this.postImageUrl + '/' + id, image).pipe(
tap(next => this.logger.debug('postImage', id, image, next)) // tap(next => this.logger.debug('postImage', id, image, next))
); // );
// }
public getPostImageUrl(): string {
return this.postImageUrl;
} }
public updateImage(id: string, image: any): Observable<any> { // public updateImage(id: string, image: any): Observable<any> {
return this.http.put<any>(this.updateImageUrl + id, image).pipe( // return this.http.put<any>(this.updateImageUrl + '/' + id, image).pipe(
tap(next => this.logger.debug('updateImage', id, image, next)) // tap(next => this.logger.debug('updateImage', id, image, next))
); // );
// }
public getUpdateImageUrl(filenameWithExtension: string): string {
return this.updateImageUrl + '/' + RestService.stripUploadFileName(filenameWithExtension);
} }
public healthCheck(): Observable<StorageHealth> { public healthCheck(): Observable<StorageHealth> {

View File

@ -12,6 +12,4 @@
</body> </body>
</html> </html>
<!--<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>--> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!--<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>-->
<!--<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>-->

View File

@ -5,7 +5,7 @@
body { body {
width: 95%; width: 95%;
margin: auto; margin: auto !important;
} }
h1, h2 { h1, h2 {
@ -13,5 +13,11 @@ h1, h2 {
} }
h2 { h2 {
margin-bottom: 1.5em; padding-bottom: 1.5em;
}
#default {
margin-left: 0 !important;
padding-left: 0 !important;
margin-bottom: 1em;
} }

View File

@ -23,6 +23,7 @@ class MongoDBService:
try: try:
for meta in col.find(): for meta in col.find():
meta['id'] = str(meta['_id'])
del meta['_id'] del meta['_id']
resp.append(meta) resp.append(meta)
except: except:

View File

@ -23,12 +23,12 @@ from app_be.views.rest_api import TestApiClass, ImageEndpoint, HealthEndpoint
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
url(r'^image/get/all$', ImageEndpoint.image_api_get_all), url(r'^image/get/all$', ImageEndpoint.image_api_get_all),
url(r'^image/get/(?P<identifier>[\w-]+)$', ImageEndpoint.image_api_get_single), url(r'^image/get/(?P<identifier>[\w.-]+)$', ImageEndpoint.image_api_get_single),
url(r'^image/get/(?P<identifier>[\w-]+)/version/(?P<version>[\w-]+)$', ImageEndpoint.image_api_get_single_version), url(r'^image/get/(?P<identifier>[\w.-]+)/version/(?P<version>[\w-]+)$', ImageEndpoint.image_api_get_single_version),
url(r'^image/delete/all$', ImageEndpoint.image_api_delete_all), url(r'^image/delete/all$', ImageEndpoint.image_api_delete_all),
url(r'^image/delete/(?P<identifier>[\w-]+)$', ImageEndpoint.image_api_delete), url(r'^image/delete/(?P<identifier>[\w.-]+)$', ImageEndpoint.image_api_delete),
url(r'^image/post$', ImageEndpoint.image_api_post), url(r'^image/post$', ImageEndpoint.image_api_post),
url(r'^image/update/(?P<identifier>[\w-]+)$', ImageEndpoint.image_api_update), url(r'^image/update/(?P<identifier>[\w.-]+)$', ImageEndpoint.image_api_update),
url(r'^check', HealthEndpoint.check_systems) url(r'^check', HealthEndpoint.check_systems)
] ]