Add junk-drawer history by IP
This commit is contained in:
@@ -85,7 +85,6 @@ import { ContactModule } from './contact/contact.module';
|
||||
app: 'us.dev api',
|
||||
},
|
||||
}),
|
||||
CacheModule.register({ isGlobal: true }),
|
||||
ParkioModule,
|
||||
IswordModule,
|
||||
AuthModule,
|
||||
|
@@ -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<any> {
|
||||
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}` };
|
||||
}
|
||||
}
|
||||
|
@@ -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<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()) : {};
|
||||
return ipListObject[ip]?.itemIds || [];
|
||||
}
|
||||
}
|
||||
|
@@ -12,3 +12,9 @@ export interface JunkDrawerItem {
|
||||
lastModified: Date;
|
||||
mimetype: string;
|
||||
}
|
||||
|
||||
export interface JunkDrawerIpList {
|
||||
ip: string;
|
||||
lastUpload: Date;
|
||||
itemIds: string[];
|
||||
}
|
||||
|
@@ -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 {}
|
||||
|
@@ -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 {}
|
||||
|
1
src/users/users.json
Normal file
1
src/users/users.json
Normal file
@@ -0,0 +1 @@
|
||||
[]
|
@@ -26,6 +26,10 @@
|
||||
>Private-ish</label>
|
||||
<input type='checkbox' id='private-ish' name='private-ish' />
|
||||
</div>
|
||||
<div>
|
||||
<label for='remember'>Remember for my IP</label>
|
||||
<input type='checkbox' id='remember' name='remember' />
|
||||
</div>
|
||||
<div class='flex flex-col max-w-96'>
|
||||
<label for='description'>Description:</label>
|
||||
<textarea
|
||||
@@ -41,5 +45,15 @@
|
||||
<button class='border-2 rounded-md border-slate-800 p-1'>Submit</button>
|
||||
</div>
|
||||
</form>
|
||||
<div>
|
||||
<h2>Your Items</h2>
|
||||
<div>
|
||||
{{#each items}}
|
||||
<div>
|
||||
<a class="text-blue-500 underline" href='/junk-drawer/{{.}}'>{{.}}</a>
|
||||
</div>
|
||||
{{/each}}
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
Reference in New Issue
Block a user