@beforesemicolon/site-builder 0.34.0 → 0.36.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,88 @@
1
+ import fs from 'fs'
2
+ import path from 'path'
3
+
4
+ export const handler = async (event, context) => {
5
+ // Only allow this in development/local environment
6
+ if (process.env.NODE_ENV === 'production') {
7
+ return {
8
+ statusCode: 403,
9
+ body: JSON.stringify({
10
+ error: 'File copying only allowed in development',
11
+ }),
12
+ }
13
+ }
14
+
15
+ if (event.httpMethod !== 'POST') {
16
+ return {
17
+ statusCode: 405,
18
+ body: JSON.stringify({ error: 'Method not allowed' }),
19
+ }
20
+ }
21
+
22
+ try {
23
+ // Parse the JSON body
24
+ const body = JSON.parse(event.body)
25
+ const { fileData, targetPath } = body
26
+
27
+ if (!fileData || !targetPath) {
28
+ return {
29
+ statusCode: 400,
30
+ body: JSON.stringify({
31
+ error: 'Missing fileData or targetPath',
32
+ }),
33
+ }
34
+ }
35
+
36
+ // Construct the full path to the public directory
37
+ const rootDir = path.resolve(process.cwd())
38
+ const publicDir = path.resolve(rootDir, 'public')
39
+ const fullTargetPath = path.join(publicDir, targetPath)
40
+ const fullTargetPath2 = path.join(rootDir, targetPath)
41
+
42
+ // Security check: ensure the target path is within the public directory
43
+ if (!fullTargetPath.startsWith(publicDir)) {
44
+ return {
45
+ statusCode: 400,
46
+ body: JSON.stringify({ error: 'Invalid target path' }),
47
+ }
48
+ }
49
+
50
+ // Ensure the target directory exists
51
+ const targetDir = path.dirname(fullTargetPath)
52
+ if (!fs.existsSync(targetDir)) {
53
+ fs.mkdirSync(targetDir, { recursive: true })
54
+ }
55
+
56
+ const targetDir2 = path.dirname(fullTargetPath2)
57
+ if (!fs.existsSync(targetDir2)) {
58
+ fs.mkdirSync(targetDir2, { recursive: true })
59
+ }
60
+
61
+ // Convert base64 back to buffer and write file
62
+ const buffer = Buffer.from(fileData, 'base64')
63
+ fs.writeFileSync(fullTargetPath, buffer)
64
+ fs.writeFileSync(fullTargetPath2, buffer)
65
+
66
+ console.log(
67
+ `File copied locally to: ${fullTargetPath} and ${fullTargetPath2}`
68
+ )
69
+
70
+ return {
71
+ statusCode: 200,
72
+ body: JSON.stringify({
73
+ success: true,
74
+ message: `File uploaded successfully`,
75
+ }),
76
+ }
77
+ } catch (error) {
78
+ console.error('Error copying file locally:', error)
79
+
80
+ return {
81
+ statusCode: 500,
82
+ body: JSON.stringify({
83
+ error: 'Failed to copy file locally',
84
+ details: error.message,
85
+ }),
86
+ }
87
+ }
88
+ }