Skip to content

Commit

Permalink
Summary
Browse files Browse the repository at this point in the history
  • Loading branch information
EuJinnLucaShow committed Aug 13, 2023
1 parent 89c19cd commit 854beb4
Show file tree
Hide file tree
Showing 7 changed files with 1,037 additions and 41 deletions.
40 changes: 19 additions & 21 deletions files.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,57 @@
const fs = require('fs/promises');
const path = require('path');
const chalk = require('chalk');
const dataValidator = require('./helpers/dataValidator')
const checkExtension = require('./helpers/checkExtension')

const creatFile = async (filename, content) => {
const file = {
const creatFile = async (req, res) => {
const {
filename,
content
}
const result = dataValidator(file)
} = req.body

const result = dataValidator(req.body)
if (result.error) {
const error = result.error.details;
const [path] = error[0].path
console.log(chalk.red(`please specify '${path}' parametr`))
return;
res.status(400).json({ message: `please specify '${path}' parametr` });
}
const chack = checkExtension(filename);
if (!chack.result) {
console.log(chalk.red(`sorry this app dont support '${chack.extation}' extension`))
return;
res.status(400).json({ message: `sorry this app dont support '${chack.extation}' extension` });
}
const filePhas = path.join(__dirname, 'files', filename)
try {
await fs.writeFile(filePhas, content, 'utf-8')
console.log(chalk.green('file created successful'))
res.status(201).json({ message: 'file created successful' })
}
catch (error) {
console.log(error)
}
}
const getFiles = async () => {

const getFiles = async (req, res) => {
const pathFolder = path.join(__dirname, 'files')
const wrFile = await fs.readdir(pathFolder)
if (wrFile.length === 0) {
return console.log('No files in this folder')
res.status(404).json({ message: 'No files in this folder'})
}
console.log(wrFile)
res.json(wrFile)
}

const getInfo = async (filename) => {
const getInfo = async (req, res) => {
const { filename } = req.params
const pathFolder = path.join(__dirname, 'files')
const wrFile = await fs.readdir(pathFolder)

if (!wrFile.includes(filename)) {
return console.log(chalk.red(`${filename} is not in this folder `))
res.status(404).json({ message: `${filename} is not in this folder ` })
}
const pathFile = path.join(__dirname, 'files', filename )
const infoData = await fs.readFile(pathFile, 'utf8')
console.log(infoData)



}
const extName = path.extname(pathFile)
const baseName = path.basename(pathFile, extName);
res.json({name:baseName, extension:extName.slice(1), content:infoData })
}

module.exports = {
creatFile,
Expand Down
1 change: 0 additions & 1 deletion files/data.js

This file was deleted.

1 change: 1 addition & 0 deletions files/text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
qwerty
28 changes: 10 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
const argv = require('yargs').argv;
const { creatFile, getFiles, getInfo } = require('./files')
const morgan = require('morgan')
const express = require('express');
const app = express();

function invokeAction({ action, filename, content }) {
switch (action) {
case 'create':
creatFile(filename, content)
break;
const router = require('./router')

case 'get':
getFiles()
break;
app.use(morgan('tiny'))

case 'getInfo':
getInfo(filename)
break;
app.use(express.json())

default:
console.warn('\x1B[31m Unknown action type!');
}
}
app.use('/files', router)

invokeAction(argv);
app.listen(3000, () => {
console.log('server run')
} )
Loading

0 comments on commit 854beb4

Please sign in to comment.