-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathplugin.controller.ts
More file actions
105 lines (95 loc) · 3.33 KB
/
plugin.controller.ts
File metadata and controls
105 lines (95 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import {
Controller,
Post,
Body,
Get,
Param,
Dependencies,
} from '@nestjs/common';
import { PluginService } from './pdf-plugin.service';
import { PluginOutput } from './pdf-plugin.interfaces';
import { PdfOutputPlugin } from './pdf-output-plugin';
import { PdfInputPlugin } from './pdf-input-plugin';
import { DocxOutputPlugin } from './docsx-output-plugin';
import { DocxInputPlugin } from './docsx-input-plugin';
import { ImageOutputPlugin } from './image-output-plugin'; // Import the ImageOutputPlugin
@Controller('plugin')
@Dependencies(PluginService)
export class PluginController {
private pdfOutputPlugin!: PdfOutputPlugin;
private pdfInputPlugin!: PdfInputPlugin;
private docxOutputPlugin!: DocxOutputPlugin;
private docxInputPlugin!: DocxInputPlugin;
private imageOutputPlugin!: ImageOutputPlugin; // Add the ImageOutputPlugin
constructor(private readonly pluginService: PluginService) {}
onModuleInit() {
this.pdfOutputPlugin = new PdfOutputPlugin();
this.pdfInputPlugin = new PdfInputPlugin();
this.docxOutputPlugin = new DocxOutputPlugin();
this.docxInputPlugin = new DocxInputPlugin();
this.imageOutputPlugin = new ImageOutputPlugin(); // Initialize the ImageOutputPlugin
}
@Post('generate-doc/:outputType')
async generateDocument(
@Param('outputType') outputType: string,
): Promise<PluginOutput> {
try {
if (outputType === 'PDF') {
return this.pdfOutputPlugin.generateDoc(outputType);
} else if (outputType === 'DOCX') {
return this.docxOutputPlugin.generateDoc(outputType);
} else if (outputType === 'IMG') {
// Add this condition for image generation
return this.imageOutputPlugin.generateImage(outputType);
} else {
throw new Error('Unsupported output type');
}
} catch (error: any) {
console.error('Error generating document:', error.message);
throw new Error('Failed to generate document');
}
}
@Post('convert-docx-to-pdf/:docxFilePath')
async convertDocxToPdf(
@Param('docxFilePath') docxFilePath: string,
): Promise<PluginOutput> {
try {
return this.docxInputPlugin.convertDocxToPdf(docxFilePath);
} catch (error: any) {
console.error('Error converting DOCX to PDF:', error.message);
throw new Error('Failed to convert DOCX to PDF');
}
}
@Get()
getPluginStatus(): string {
return 'Plugin is running!';
}
@Get('/pdf-to-image')
async convertPdfToImage(): Promise<{ images?: { url: string }[] }> {
const pdfFilePath = './generatedDocument.pdf';
try {
const pluginOutput = await this.pdfInputPlugin.transformPdfToImage(
pdfFilePath,
);
if (pluginOutput.images) {
const images = pluginOutput.images;
images.forEach((image: { url: string }) => {
console.log('Image URL:', image.url);
});
}
return { images: pluginOutput.images };
} catch (error) {
console.error('Error converting PDF to image:', error);
throw new Error('PDF to image conversion failed');
}
}
@Post('create-default-pdf')
async createDefaultPdf(): Promise<PluginOutput> {
try {
return this.pdfOutputPlugin.createDefaultPdf();
} catch (error: any) {
console.error('Error creating default PDF:', error.message);
throw new Error('Failed to create default PDF');
}
}
}