program is in server

This commit is contained in:
2023-02-10 10:26:43 +01:00
parent e277358a88
commit 4b7f0b4e3c
8 changed files with 179 additions and 64 deletions

View File

@@ -1,10 +1,17 @@
import express from 'express';
import path from 'path'
import fs from 'fs/promises'
import Jimp from 'jimp'
import crypto from 'crypto'
const app = express()
app.use('/assets',express.static('images'))
/* eslint-disable max-depth */
/* eslint-disable complexity */
import Jimp from 'jimp'
import fs from 'fs/promises'
import crypto from 'crypto'
import path from 'path'
app.listen(3000, () => {
console.log('The application is listening on port 3000!');
})
/**
*
*
@@ -12,9 +19,10 @@ import path from 'path'
*
* @return the list of files included in this folder/subfolders
*/
async function filesList(Path: string): Promise<Array<string>> {
async function filesList(Path: string): Promise<string[]> {
const files: Array<string> = []
const subFolders = await fs.readdir(Path)
for await (const subFolder of subFolders){
const stats = await fs.stat(Path + '/' + subFolder)
if (stats.isDirectory()) {
@@ -27,60 +35,73 @@ async function filesList(Path: string): Promise<Array<string>> {
return files
}
(async (dirPath: string) => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const files = await filesList(dirPath)
console.log(files)
// Filtre
const data = (await fs.readFile('./output.json')).toString('utf8')
app.get('/program', (_req, res) => {
let json: Record<string, string> = {}
try{
json = JSON.parse(data)
}catch{
console.log('handeling...')
}
for await (const file of files) {
const bFile = path.basename(file)
const pName = path.dirname(file)
const hash = crypto.createHash('sha256')
hash.setEncoding('hex')
const readI = await fs.readFile(`${file}`)
hash.write(readI)
hash.end()
const sha256sum = hash.read()
console.log('bfile = ' + bFile)
if (bFile.startsWith('.')) {
continue
}else if (json[file] === sha256sum){
console.log('This picture has already been turned to sepia')
continue
}
// Modification des données
json[file] = sha256sum
// Ré-écriture dans le fichier output
await fs.writeFile('output.json', JSON.stringify(json))
(async (dirPath: string) => {
const files = await filesList(dirPath)
console.log(files)
// Filtre
const data = (await fs.readFile('./output.json')).toString('utf8')
let json: Record<string, string> = {}
try{
const image = await Jimp.read(readI)
image.sepia()
console.log(bFile)
const newFileName = pName + '/.' + bFile
console.log('newfilename:' + newFileName)
// save image with new file name
await image.writeAsync(`${newFileName}`)
console.log('Images have been inverted and rename ' + '.' + file)
}catch (err){
console.log('this file is not an image')
json = JSON.parse(data)
}catch{
console.log('handeling...')
}
}
for await (const file of files) {
const bFile = path.basename(file)
const pName = path.dirname(file)
const hash = crypto.createHash('sha256')
hash.setEncoding('hex')
const readI = await fs.readFile(`${file}`)
hash.write(readI)
hash.end()
const sha256sum = hash.read()
console.log('bfile = ' + bFile)
if (bFile.startsWith('.')) {
continue
}else if (json[file] === sha256sum){
console.log('This picture has already been turned to sepia')
continue
}
// Modification des données
json[file] = sha256sum
// Ré-écriture dans le fichier output
await fs.writeFile('output.json', JSON.stringify(json))
try{
const image = await Jimp.read(readI)
image.sepia()
console.log(bFile)
const newFileName = pName + '/.' + bFile
console.log('newfilename:' + newFileName)
// save image with new file name
await image.writeAsync(`${newFileName}`)
console.log('Images have been inverted and rename ' + '.' + file)
}catch (err){
console.log('this file is not an image')
}
}
res.send('Pictures transformed')
res.end()
})('./images')
})
{
app.get('/', async (_req, res) => {
const list = await filesList('./content')
const item = list[Math.floor(Math.random()*((list)).length)]
const itemB =await fs.readFile(item)
res.send(itemB)
console.log(item)
})
}
})('/images')