Skip to content

Commit

Permalink
create auth and upload
Browse files Browse the repository at this point in the history
  • Loading branch information
GJHack committed Dec 29, 2023
1 parent 9b69302 commit fb9aa27
Showing 1 changed file with 69 additions and 21 deletions.
90 changes: 69 additions & 21 deletions server/controllers/file_cheker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
const unzipper = require('unzipper');
const fs = require('fs')
const convert = require('xml-js');

const ABS_PATH = "/usr/share/nginx/html/back/public/uploads/exchangeStrapi/tempXMLS/"

Expand All @@ -23,11 +24,19 @@ module.exports = {
console.log(`Результат поиска полученного от 1С архива: ${(searchZipResult) ? "НАЙДЕН" : "НЕ НАЙДЕН"}`);

if(await searchZipResult) {

const resultUnzip = await unzip(ABS_PATH + filename, ABS_PATH, filename)
console.log("Результат распаковки полученного от 1С архива: " + resultUnzip)

if(await resultUnzip) {
const searchFolderResult = await searchInFolder(ctx, ABS_PATH, filename.split('.')[0], false)

const searchFolderResult = await searchInFolder(ctx, ABS_PATH, filename.split('.')[0], false, false)
console.log(`Результат поиска папки с файлами: ${(await searchFolderResult) ? "НАЙДЕН" : "НЕ НАЙДЕН"}`);

if(await searchFolderResult) {
const searchImportXMLs = await getAllFiles(ABS_PATH, filename.split('.')[0])
console.log(`Результат получения всех файлов внутри: ${await searchImportXMLs}`);
}
}
}

Expand All @@ -48,9 +57,9 @@ async function unzip(filePath = "",absPath = "/", fileName = "text.zip") {

if(!filePath || typeof filePath !== "string") return

console.log("старт распаковки....")
//console.log("старт распаковки....")
const newFolderName = fileName.split('.')[0];
console.log("Файл будет разархивирован по пути: " + newFolderName)
//console.log("Файл будет разархивирован по пути: " + newFolderName)

const promise = new Promise((resolve, reject) => {
try {
Expand Down Expand Up @@ -80,16 +89,16 @@ async function unzip(filePath = "",absPath = "/", fileName = "text.zip") {
* @param {String} extension - Расширение файла ( по дефолту будет искать папки )
* @returns {boolean}
*/
async function searchInFolder(ctx, filePath = "", fileName = "text", extension = "") {
async function searchInFolder(ctx, filePath = "", fileName = "text", extension = "",getAllNames = false) {
if(!filePath || typeof filePath !== "string") return

const searchPath = (extension) ? filePath : filePath + fileName;

console.log(getAllNames)
try{
if(!extension) {
console.log(`Поиск папки ! ${searchPath}`)
// console.log(`Поиск папки ! ${searchPath}`)
if(fs.existsSync(searchPath)) {
console.log(`Папка по пути ${searchPath} cуществует`)
//console.log(`Папка по пути ${searchPath} cуществует`)
ctx.body = JSON.stringify({
error: false,
result: true,
Expand All @@ -101,26 +110,65 @@ async function searchInFolder(ctx, filePath = "", fileName = "text", extension =
return false;
}
} else {
console.log(`Поиск файла в ${searchPath}`)
const promise = new Promise((resolve, reject) => {
fs.readdirSync(searchPath).map(checkName => {
const formatSearchString = (extension) ? "." + extension : extension
const checkString = fileName + formatSearchString
if(checkName === checkString) {

console.log(`
if(!getAllNames) {
console.log("IN SINGLE")

//console.log(`Поиск файла в ${searchPath}`)
const promise = new Promise((resolve, reject) => {
fs.readdirSync(searchPath).map(checkName => {
const formatSearchString = (extension) ? "." + extension : extension
const checkString = fileName + formatSearchString
if(checkName === checkString) {
console.log(`
"Файл" с именем ${fileName} найден!
`)
console.log(searchPath)
resolve(true);
}
})
});
return await promise
resolve(true);
}
})
});
return await promise
} else {

}

}
} catch(e) {
console.log(e)
return false;
}

}
async function getAllFiles(absPath = "/", folderName = "test") {
const searchPath = absPath + folderName;

console.log(searchPath)

const promise = new Promise(async (resolve, reject) => {
console.log("Получение всех файлов в папке.")
const allFileNames = []

fs.readdirSync(searchPath).map(async checkName => {
const checkString = folderName

allFileNames.push({
name: checkName,
dir: searchPath,
path: `${searchPath}/${checkName}`,
jsonData: await parseXML(`${searchPath}/${checkName}`)
})
})

console.log(allFileNames)
resolve(allFileNames)
});

return await promise
}


async function parseXML(path = "/") {
let result;

console.log(result);
return await result;
}

0 comments on commit fb9aa27

Please sign in to comment.