File size: 4,967 Bytes
a4ed013
 
 
05cee1f
24727a3
 
 
a4ed013
db2ddc0
 
24727a3
a4ed013
 
24727a3
 
 
 
 
 
 
 
 
 
47e2259
24727a3
 
 
 
 
 
05cee1f
24727a3
 
 
 
 
 
05cee1f
24727a3
 
 
 
 
 
05cee1f
24727a3
 
 
 
 
 
 
47e2259
24727a3
 
 
 
 
 
 
db2ddc0
24727a3
 
 
 
 
 
 
05cee1f
24727a3
 
 
 
 
 
05cee1f
24727a3
 
 
 
 
 
05cee1f
24727a3
 
db2ddc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4ed013
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import path from 'path'
import { fileURLToPath } from 'url'
import AdmZip from 'adm-zip'
import { success, created, paginated } from '../../lib/response.js'
import * as templatesService from './templates.service.js'
import { getPagination } from '../../lib/pagination.js'
import { toPublicUrl } from '../../middleware/upload.js'
import { AppError } from '../../lib/AppError.js'
import { parseManifest, applyManifest } from './manifest.service.js'
import fs from 'fs'

const __dirname = path.dirname(fileURLToPath(import.meta.url))

export async function listPublished(req, res, next) {
  try {
    const { page, limit } = getPagination(req.query)
    const result = await templatesService.getPublishedTemplates({
      category: req.query.category,
      search: req.query.search,
      sort: req.query.sort,
      page,
      limit
    })
    paginated(res, { templates: result.templates, page: result.page, totalPages: result.totalPages }, result.total, result.page, limit)
  } catch (err) { next(err) }
}

export async function getById(req, res, next) {
  try {
    const template = await templatesService.getTemplateById(req.params.id)
    success(res, template)
  } catch (err) { next(err) }
}

export async function getPreview(req, res, next) {
  try {
    const template = await templatesService.getTemplateForPreview(req.params.id)
    success(res, template)
  } catch (err) { next(err) }
}

export async function adminGetById(req, res, next) {
  try {
    const template = await templatesService.getTemplateById(req.params.id)
    success(res, template)
  } catch (err) { next(err) }
}

export async function adminList(req, res, next) {
  try {
    const { page, limit } = getPagination(req.query)
    const result = await templatesService.getAllTemplates({ page, limit })
    paginated(res, { templates: result.templates, page: result.page, totalPages: result.totalPages }, result.total, result.page, limit)
  } catch (err) { next(err) }
}

export async function create(req, res, next) {
  try {
    const previewUrl = toPublicUrl(req.file?.path)
    const template = await templatesService.createTemplate(req.validatedData, previewUrl)
    if (template) created(res, template)
  } catch (err) { next(err) }
}

export async function update(req, res, next) {
  try {
    const previewUrl = toPublicUrl(req.file?.path) || null
    const template = await templatesService.updateTemplate(req.params.id, req.validatedData, previewUrl)
    success(res, template)
  } catch (err) { next(err) }
}

export async function remove(req, res, next) {
  try {
    await templatesService.deleteTemplate(req.params.id)
    success(res, null, 'ุชู… ุญุฐู ุงู„ู‚ุงู„ุจ ุจู†ุฌุงุญ')
  } catch (err) { next(err) }
}

export async function publish(req, res, next) {
  try {
    const template = await templatesService.togglePublish(req.params.id)
    success(res, template)
  } catch (err) { next(err) }
}

export async function uploadManifestFile(req, res, next) {
  try {
    if (!req.file) throw new AppError('manifest.json ู…ุทู„ูˆุจ', 400, 'FILE_REQUIRED')
    const content = fs.readFileSync(req.file.path, 'utf-8')
    const manifest = parseManifest(content)
    const result = await applyManifest(req.params.id, manifest)
    fs.unlinkSync(req.file.path)
    success(res, {
      manifest: result,
      templateType: manifest.type,
      framework: manifest.framework,
      deploymentType: manifest.deployment,
      fieldsCount: manifest.fields.length,
    }, 'ุชู… ุฑูุน ูˆุชุทุจูŠู‚ manifest.json')
  } catch (err) { next(err) }
}

export async function uploadSourceCode(req, res, next) {
  try {
    if (!req.file) throw new AppError('ู…ู„ู ุงู„ู…ุตุฏุฑ ู…ุทู„ูˆุจ', 400, 'FILE_REQUIRED')
    const sourcePath = req.file.path
    await templatesService.updateTemplate(req.params.id, {}, null, sourcePath)
    success(res, { sourceFile: toPublicUrl(sourcePath) }, 'ุชู… ุฑูุน ุงู„ูƒูˆุฏ ุงู„ู…ุตุฏุฑูŠ')
  } catch (err) { next(err) }
}

export async function uploadPreviewZip(req, res, next) {
  try {
    if (!req.file) throw new AppError('ู…ู„ู ZIP ู…ุทู„ูˆุจ', 400, 'FILE_REQUIRED')

    const previewDir = path.resolve(__dirname, '../../../uploads/templates-preview', req.params.id)
    if (!fs.existsSync(previewDir)) fs.mkdirSync(previewDir, { recursive: true })

    const zip = new AdmZip(req.file.path)
    zip.extractAllTo(previewDir, true)

    const expectedPath = path.join(previewDir, 'index.html')
    if (!fs.existsSync(expectedPath)) {
      fs.rmSync(previewDir, { recursive: true, force: true })
      throw new AppError('ุงู„ู…ู„ู ุงู„ู…ุถุบูˆุท ูŠุฌุจ ุฃู† ูŠุญุชูˆูŠ ุนู„ู‰ index.html ููŠ ุงู„ุฌุฐุฑ', 400, 'MISSING_INDEX_HTML')
    }

    const previewUrl = `/uploads/templates-preview/${req.params.id}/index.html`
    await templatesService.updateTemplate(req.params.id, {}, previewUrl)

    fs.unlinkSync(req.file.path)
    success(res, { previewUrl }, 'ุชู… ุฑูุน ูˆููƒ ุถุบุท ู…ู„ู ุงู„ู‚ุงู„ุจ')
  } catch (err) { next(err) }
}