diff --git a/src/app.module.ts b/src/app.module.ts index 6f09b73..3967299 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -85,7 +85,6 @@ import { ContactModule } from './contact/contact.module'; app: 'us.dev api', }, }), - CacheModule.register({ isGlobal: true }), ParkioModule, IswordModule, AuthModule, diff --git a/src/junk-drawer/junk-drawer.controller.ts b/src/junk-drawer/junk-drawer.controller.ts index b149ea1..7f9f2bb 100644 --- a/src/junk-drawer/junk-drawer.controller.ts +++ b/src/junk-drawer/junk-drawer.controller.ts @@ -6,6 +6,7 @@ import { Post, Redirect, Render, + Req, Res, UploadedFile, UploadedFiles, @@ -16,7 +17,7 @@ import { ApiConsumes, ApiTags } from '@nestjs/swagger'; import { FilesInterceptor } from '@nestjs/platform-express'; import { generateUniqueSlug } from 'src/utils/slug'; import { JunkDrawerMetadata } from './types'; -import { Response } from 'express'; +import { Request, Response } from 'express'; @Controller('junk-drawer') @ApiTags('junk-drawer') @@ -25,8 +26,12 @@ export class JunkDrawerController { @Get('') @Render('junk-drawer/upload') - generateUploadForm() { - return {}; + async generateUploadForm(@Req() req: Request) { + let items: string[] = []; + if (req.ip) { + items = await this.junkDrawerService.getItemsForIp(req.ip); + } + return { items }; } @Get(':slug.json') @@ -80,8 +85,10 @@ export class JunkDrawerController { @UseInterceptors(FilesInterceptor('files')) async handleFileUpload( @UploadedFiles() files: Express.Multer.File[], + @Req() request: Request, @Body('description') description: string, @Body('private-ish') privateIsh: boolean, + @Body('remember') remember: boolean, ): Promise { const uniqueSlug = generateUniqueSlug({ random: privateIsh, @@ -106,6 +113,9 @@ export class JunkDrawerController { ); } await this.junkDrawerService.storeJunkDrawerMetadata(metadata); + if (remember && request.ip && !privateIsh) { + await this.junkDrawerService.recordItemForIp(request.ip, uniqueSlug); + } return { url: `/junk-drawer/${uniqueSlug}` }; } } diff --git a/src/junk-drawer/junk-drawer.service.ts b/src/junk-drawer/junk-drawer.service.ts index 26c2885..30bfadc 100644 --- a/src/junk-drawer/junk-drawer.service.ts +++ b/src/junk-drawer/junk-drawer.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, Logger } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { MinioService } from 'src/minio/minio.service'; import { JunkDrawerMetadata } from './types'; @@ -6,6 +6,7 @@ import { generateUniqueSlug } from 'src/utils/slug'; @Injectable() export class JunkDrawerService { + private readonly logger: Logger = new Logger(JunkDrawerService.name); constructor( private readonly minioService: MinioService, private readonly configService: ConfigService, @@ -92,4 +93,43 @@ export class JunkDrawerService { this.pathForFile(`${slug}/${filename}`), ); } + + public async recordItemForIp(ip: string, itemId: string) { + let ipList = Buffer.from(JSON.stringify({})); + try { + ipList = await this.minioService.getBuffer( + this.junkDrawerBucketName(), + this.pathForFile(`ip-list.json`), + ); + } catch (error) { + this.logger.warn('IP list is empty'); + } + let ipListObject = ipList ? JSON.parse(ipList.toString()) : {}; + if (!ipListObject[ip]) { + ipListObject[ip] = { + lastUpload: new Date(), + itemIds: [], + }; + } + ipListObject[ip].itemIds.push(itemId); + await this.minioService.uploadBuffer( + this.junkDrawerBucketName(), + this.pathForFile(`ip-list.json`), + Buffer.from(JSON.stringify(ipListObject)), + ); + } + + public async getItemsForIp(ip: string): Promise { + let ipList = Buffer.from(JSON.stringify({})); + try { + ipList = await this.minioService.getBuffer( + this.junkDrawerBucketName(), + this.pathForFile(`ip-list.json`), + ); + } catch (error) { + this.logger.warn('IP list is empty'); + } + let ipListObject = ipList ? JSON.parse(ipList.toString()) : {}; + return ipListObject[ip]?.itemIds || []; + } } diff --git a/src/junk-drawer/types.ts b/src/junk-drawer/types.ts index 60f029d..52c4573 100644 --- a/src/junk-drawer/types.ts +++ b/src/junk-drawer/types.ts @@ -12,3 +12,9 @@ export interface JunkDrawerItem { lastModified: Date; mimetype: string; } + +export interface JunkDrawerIpList { + ip: string; + lastUpload: Date; + itemIds: string[]; +} diff --git a/src/minio/minio.module.ts b/src/minio/minio.module.ts index 23cbe4f..bbba766 100644 --- a/src/minio/minio.module.ts +++ b/src/minio/minio.module.ts @@ -1,7 +1,8 @@ import { Module } from '@nestjs/common'; import { MinioService } from './minio.service'; +import { CacheModule } from '@nestjs/cache-manager'; @Module({ - providers: [MinioService] + providers: [MinioService], }) export class MinioModule {} diff --git a/src/parkio/parkio.module.ts b/src/parkio/parkio.module.ts index 5dd94fc..168bc68 100644 --- a/src/parkio/parkio.module.ts +++ b/src/parkio/parkio.module.ts @@ -2,9 +2,10 @@ import { Module } from '@nestjs/common'; import { ParkioService } from './parkio.service'; import { ParkioController } from './parkio.controller'; import { IswordService } from 'src/isword/isword.service'; +import { CacheModule } from '@nestjs/cache-manager'; @Module({ providers: [ParkioService, IswordService], - controllers: [ParkioController] + controllers: [ParkioController], }) export class ParkioModule {} diff --git a/src/users/users.json b/src/users/users.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/src/users/users.json @@ -0,0 +1 @@ +[] diff --git a/views/junk-drawer/upload.hbs b/views/junk-drawer/upload.hbs index 55e86ce..9fe8295 100644 --- a/views/junk-drawer/upload.hbs +++ b/views/junk-drawer/upload.hbs @@ -26,6 +26,10 @@ >Private-ish +
+ + +