Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emil Fataliev #15

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
node_modules/
*.log
.idea/
tmp-6IMpLe/
tmp-AxgP6Y/
tmp-YXiaok/
83 changes: 83 additions & 0 deletions Transformator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Created by emilfataliev on 03.11.16.
*/
const Transform = require('stream').Transform;

class Transformator extends Transform {

constructor(options) {
super(options);
this.current_str = "";
}


_transform(chunk, callback) {
var str = chunk.toString('utf8');
var new_str = this.translit(str);
this.current_str = new_str;
this.push(new_str);
callback();
}

translit(string) {
if (isRus(string))
for (let symbol in rus_to_eng) {
string = string.replace(new RegExp(symbol, 'g'), rus_to_eng[symbol]);
}
string = '{ "content" : "' + string;

return string;

}

_flush(callback) {

let str = this.current_str;
str = str.toString('utf8');
let new_str = this.translit(str);
this.push(new_str + '"}');


callback();
}

}
const rus_to_eng = {
'А': 'A', 'Б': 'B', 'В': 'V', 'Г': 'G', 'Д': 'D', 'Е': 'E', 'Ё': 'Jo', 'Ж': 'Zh', 'З': 'Z',
'И': 'I', 'Й': 'J', 'К': 'K', 'Л': 'L', 'М': 'M', 'Н': 'N', 'О': 'O', 'П': 'P', 'Р': 'R',
'С': 'S', 'Т': 'T', 'У': 'U', 'Ф': 'F', 'Х': 'H', 'Ц': 'C', 'Ч': 'Ch', 'Ш': 'Sh', 'Щ': 'Shh',
'Ъ': '#', 'Ы': 'Y', 'Ь': '\'', 'Э': 'Je', 'Ю': 'Ju', 'Я': 'Ja',
'а': 'a', 'б': 'b', 'в': 'v', 'г': 'g', 'д': 'd', 'е': 'e', 'ё': 'jo', 'ж': 'zh', 'з': 'z',
'и': 'i', 'й': 'j', 'к': 'k', 'л': 'l', 'м': 'm', 'н': 'n', 'о': 'o', 'п': 'p', 'р': 'r',
'с': 's', 'т': 't', 'у': 'u', 'ф': 'f', 'х': 'h', 'ц': 'c', 'ч': 'ch', 'ш': 'sh', 'щ': 'shh',
'ъ': '#', 'ы': 'y', 'ь': '\'', 'э': 'je', 'ю': 'ju', 'я': 'ja'
};

const additional_eng = {
'JO': 'Ё', 'JU': 'Ю', 'YO': 'Ё', 'CH': 'Ч',
'YA': 'Я', 'JE': 'Э', 'SHH': 'Щ', 'SH': 'Ш', 'ZH': 'Ж',
'#': 'ъ',
'jo': 'ё', 'ju': 'ю', 'yo': 'ё', 'ch': 'ч',
'ya': 'я', 'je': 'э', 'shh': 'щ', 'sh': 'ш', 'zh': 'ж', 'ja': 'я',
'Jo': 'Ё', 'Ju': 'Ю', 'Yo': 'Ё', 'Ch': 'Ч',
'Ya': 'Я', 'Je': 'Э', 'Shh': 'Щ', 'Sh': 'Ш', 'Zh': 'Ж'
};

const eng_to_rus = {
'A': 'А', 'B': 'Б', 'C': 'Ц', 'D': 'Д', 'E': 'Е', 'F': 'Ф', 'G': 'Г', 'H': 'Х', 'I': 'И',
'J': 'Й', 'K': 'К', 'L': 'Л', 'M': 'М', 'N': 'Н', 'O': 'О', 'P': 'П', 'Q': 'Я', 'R': 'Р',
'S': 'С', 'T': 'Т', 'U': 'У', 'V': 'В', 'W': 'Щ', 'X': 'Х', 'Y': 'Ы', 'Z': 'З',

'a': 'а', 'b': 'б', 'c': 'ц', 'd': 'д', 'e': 'е', 'f': 'ф', 'g': 'г', 'h': 'х', 'i': 'и',
'j': 'й', 'k': 'к', 'l': 'л', 'm': 'м', 'n': 'н', 'o': 'о', 'p': 'п', 'q': 'я', 'r': 'р',
's': 'с', 't': 'т', 'u': 'у', 'v': 'в', 'w': 'щ', 'x': 'х', 'y': 'ы', 'z': 'з', '\'': 'ь'
};

function isRus(str) {
return (!!/[А-Я-Ё]/gi.test(str));
}

function isEng(str) {
return (!!/[A-Z]/gi.test(str));
}
module.exports = Transformator;
124 changes: 121 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,131 @@
'use strict';

var fs = require('fs');
var path = require('path');
const Transformator = require('./Transformator');
const express = require('express');
const app = express();

const PORT = process.env.PORT || 4000;

app.listen(PORT, function () {
console.log(`App is listen on ${PORT}`);
console.log('App is listen on ${PORT}');
});

app.use(function (req, res, next) {
req.startAt = process.hrtime();
next();
});


app.use(function (req, res, next) {
var method = req.method;
var url = req.url;
res.setHeader('X-Request-Url', method + ' ' + url);
next();
});

app.use(function (req, res, next) {

var current_cookie = req.headers.cookie;

if (!current_cookie) {
res.sendStatus(403);
return;
}
function prepare_cookies(cookie) {

var result = [];

cookie = cookie.split('\n');
for (var i = 0; i < cookie.length; ++i) {
var temp = cookie[i].split('=');
result[temp[0]] = temp;
}
if (result && result['authorize'])
return true;
else return false;
}

if (prepare_cookies(current_cookie)) {
next();
}
else
res.sendStatus(403);


});


app.use(function (req, res, next) {

var x_time = process.hrtime(req.startAt);
x_time = (x_time[1] / 1000).toFixed(3);
res.setHeader('X-Time', x_time);
next();
});


app.get('/v1/', function (req, res) {

work_with_files(req, res);
});

app.get('/v1/(:arr)*', function (req, res, next) {
work_with_files(req, res);
});
app.use((error, req, res, next) => {
res.writeHead(503, {'X-Request-Error': error.toString()});
res.end();
});

app.use(function (req, res, next) {
res.writeHead(503, {'X-Request-Error': "Unknown request"});
res.end();
});

app.use(function (req, res, next) {
res.end();
});

function work_with_files(req, res) {

var path = '';
if (req.params[0] != undefined) {
path = req.params['arr'] + req.params[0];
}

if (path === '../') {
res.status(503).setHeader('x-request-error', path).end();
return;
}

path = path.replace(new RegExp('.+/\.\./', 'gi'), '');

path = './' + path;

if (fs.lstatSync(path).isDirectory()) {

fs.readdir(path, function (error, items) {
let items_list = ['.', '..'] + items;
res.status(200).send({'content': items_list});
});

} else if (fs.lstatSync(path).isFile()) {

var stream = fs.createReadStream(path);
transformStream(stream, res);

} else {
res.sendStatus(503).end();
}

}
function transformStream(streamIn, streamOut) {

var t = new Transformator();
streamIn.pipe(t).pipe(streamOut);
streamOut.end(JSON.stringify(streamOut));

}

// IMPORTANT. Это строка должна возвращать инстанс сервера
module.exports = app;