github.com/readium/readium-lcp-server@v0.0.0-20240509124024-799e77a0bbd6/frontend/manage/app/components/resource-list-component.ts (about) 1 import { Component, Input, OnInit } from '@angular/core'; 2 3 import { Router } from '@angular/router'; 4 import { User } from './user'; 5 import { Purchase } from './purchase'; 6 import { Resource } from './resource'; 7 import * as lic from './partialLicense'; 8 import { ResourceService } from './resource.service'; 9 import { PurchaseService } from './purchase.service'; 10 11 @Component({ 12 moduleId: module.id, 13 selector: 'resources', 14 templateUrl: '/app/components/resource-list.html', 15 styleUrls: ['../../app/components/resource.css', '../../styles.css'], // from /js/app/components... 16 providers: [ResourceService, PurchaseService] 17 }) 18 19 20 export class ResourcesComponent implements OnInit { 21 resources: Resource[]; 22 selectedResource: Resource; 23 @Input() id: string; 24 @Input() user: User; 25 @Input() hours: string; 26 27 constructor(private resourceService: ResourceService, private purchaseService: PurchaseService, private router: Router) { } 28 29 getResources(): void { 30 this.resourceService.getResources().then(Resources => this.resources = Resources); 31 } 32 33 ngOnInit(): void { 34 this.getResources(); 35 } 36 37 onSelect(resource: Resource): void { 38 this.selectedResource = resource; 39 } 40 41 onBuy(resource: Resource): void { 42 // buy action for selectedResource and user 43 // create partial license 44 let partialLicense = this.createPartialLicense(this.user, undefined); 45 let p = new Purchase; 46 p.label = resource.location; 47 p.partialLicense = JSON.stringify(partialLicense); 48 p.resource = resource.id; 49 p.user = this.user; 50 let rp: Purchase; 51 // create a purchase in database (and get license on lcpserver ) 52 this.purchaseService.create(p) 53 .then(p => this.router.navigate(['/userdetail', this.user.userID])); 54 } 55 56 onLoan(resource: Resource, hours: string): void { 57 // TODO add parameters for loan action (period etc.) 58 let rights = new lic.UserRights; 59 rights.copy = 10; 60 rights.print = 10; 61 rights.start = new Date(); 62 let h: number = parseFloat(hours); 63 64 if (isNaN(h)) { 65 rights.end = new Date( rights.start.valueOf() + 30 * 24 * 3600 * 1000); // + 30 days 66 } else { 67 rights.end = new Date( rights.start.valueOf() + h * 3600 * 1000); // + h hours 68 } 69 // loan action action for selectedResource and user 70 let partialLicense = this.createPartialLicense(this.user, rights); 71 let p = new Purchase; 72 p.label = resource.location; 73 p.partialLicense = JSON.stringify(partialLicense); 74 p.resource = resource.id; 75 p.user = this.user; 76 // create a purchase(loan) in database (and get license on lcpserver ) 77 this.purchaseService.create(p) 78 .then( p => this.router.navigate(['/userdetail', this.user.userID])); 79 } 80 81 private hexToBytes(hex: string) { 82 let bytes: number[] = []; 83 for (let i = 0; i < (hex.length/2); i++) { 84 bytes.push(parseInt(hex.substr(i * 2, 2), 16)); 85 } 86 return bytes; 87 } 88 89 private createPartialLicense(user: User, rights: lic.UserRights): lic.PartialLicense { 90 let partialLicense = new lic.PartialLicense; 91 partialLicense.provider = lic.PROVIDER; 92 partialLicense.user = {id: '_' + String(user.userID), email: user.email, name: user.alias, encrypted: undefined }; 93 partialLicense.rights = rights; 94 partialLicense.encryption = new lic.Encryption; 95 partialLicense.encryption.user_key = new lic.UserKey; 96 partialLicense.encryption.user_key.value = this.hexToBytes( user.password); 97 partialLicense.encryption.user_key.algorithm = lic.USERKEY_ALGO; 98 partialLicense.encryption.user_key.text_hint = 'Enter passphrase'; 99 return partialLicense; 100 } 101 }