<commit_before>/// <reference path="./swaggerUiExpress.d.ts" />

import * as bodyParser from 'body-parser';
import * as express from 'express';
import * as openapi from 'express-openapi';
import * as fs from 'fs';
import * as yaml from 'js-yaml';
import * as log4js from 'log4js';
import * as mkdirp from 'mkdirp';
import * as multer from 'multer';
import * as path from 'path';
import * as swaggerUi from 'swagger-ui-express';
import Base from '../Base';
import factory from '../Model/ModelFactory';
import { EncodeFinModelInterface } from '../Model/Service/Encode/EncodeFinModel';
import { SocketIoManageModelInterface } from '../Model/Service/SocketIoManageModel';
import Util from '../Util/Util';
import BasicAuth from './BasicAuth';

/**
 * Server
 */
class Server extends Base {
    private app = express();

    constructor() {
        super();

        // log
        this.app.use(log4js.connectLogger(this.log.access, { level: 'info' }));

        // config
        const config = this.config.getConfig();

        // basic auth
        const basicAuthConfig = config.basicAuth;
        if (typeof basicAuthConfig !== 'undefined') {
            this.app.use(BasicAuth(basicAuthConfig.user, basicAuthConfig.password));
        }

        // read pkg
        const pkg = require(path.join('..', '..', '..', 'package.json'));

        // read api.yml
        const api = <openapi.OpenApi.ApiDefinition> yaml.safeLoad(fs.readFileSync(path.join(__dirname, '..', '..', '..', 'api.yml'), 'utf-8'));
        api.info = {
            title: pkg.name,
            version: pkg.version,
        };

        // swagger ui
        this.app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(api));
        this.app.get('/api/debug', (_req, res) => { return res.redirect('/api-docs/?url=/api/docs'); });


        // uploader dir
        const uploadTempDir = config.uploadTempDir || './data/upload';
        try {
            fs.statSync(uploadTempDir);
        } catch (e) {
            // ディレクトリが存在しなければ作成する
            this.log.system.info(`mkdirp: ${ uploadTempDir }`);
            mkdirp.sync(uploadTempDir);
        }

        // uploader
        const storage = multer.diskStorage({
            destination: uploadTempDir,
            filename: (req, file, cb) => {
                const fileName = file.fieldname + '-' + Date.now();
                cb(null, fileName);

                // 切断時はファイルを削除
                (<any> req).on('close', () => {
                    const filePath = path.join(uploadTempDir, fileName);
                    fs.unlink(filePath, (err) => {
                        if (err) {
                            this.log.access.error(`upload file delete error: ${ filePath }`);
                            this.log.access.error(err.message);
                        } else {
                            this.log.access.info(`delete upload file: ${ filePath }`);
                        }
                    });
                });
            },
        });
        const upload = multer({ storage: storage });

        // init express-openapi
        openapi.initialize({
            apiDoc: api,
            app: this.app,
            consumesMiddleware: {
                'application/json': bodyParser.json(),
                'text/text': bodyParser.text(),
                'multipart/form-data': (req, res, next) => {
                    upload.single('file')(req, res, (err) => {
                        if (err) { return next(err.message); }

                        if (typeof req.file !== 'undefined' && typeof req.file.fieldname !== 'undefined') {
                            req.body[req.file.fieldname] = req.file;
                        }

                        return next();
                    });
                },
            },
            docsPath: '/docs',
            errorMiddleware: (err, _req, res) => {
                res.status(400);
                res.json(err);
            },
            errorTransformer: (openApi) => {
                return openApi.message;<commit_msg>upload できる filedname を ts, encode に変更 #83<commit_after>/// <reference path="./swaggerUiExpress.d.ts" />

import * as bodyParser from 'body-parser';
import * as express from 'express';
import * as openapi from 'express-openapi';
import * as fs from 'fs';
import * as yaml from 'js-yaml';
import * as log4js from 'log4js';
import * as mkdirp from 'mkdirp';
import * as multer from 'multer';
import * as path from 'path';
import * as swaggerUi from 'swagger-ui-express';
import Base from '../Base';
import factory from '../Model/ModelFactory';
import { EncodeFinModelInterface } from '../Model/Service/Encode/EncodeFinModel';
import { SocketIoManageModelInterface } from '../Model/Service/SocketIoManageModel';
import Util from '../Util/Util';
import BasicAuth from './BasicAuth';

/**
 * Server
 */
class Server extends Base {
    private app = express();

    constructor() {
        super();

        // log
        this.app.use(log4js.connectLogger(this.log.access, { level: 'info' }));

        // config
        const config = this.config.getConfig();

        // basic auth
        const basicAuthConfig = config.basicAuth;
        if (typeof basicAuthConfig !== 'undefined') {
            this.app.use(BasicAuth(basicAuthConfig.user, basicAuthConfig.password));
        }

        // read pkg
        const pkg = require(path.join('..', '..', '..', 'package.json'));

        // read api.yml
        const api = <openapi.OpenApi.ApiDefinition> yaml.safeLoad(fs.readFileSync(path.join(__dirname, '..', '..', '..', 'api.yml'), 'utf-8'));
        api.info = {
            title: pkg.name,
            version: pkg.version,
        };

        // swagger ui
        this.app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(api));
        this.app.get('/api/debug', (_req, res) => { return res.redirect('/api-docs/?url=/api/docs'); });


        // uploader dir
        const uploadTempDir = config.uploadTempDir || './data/upload';
        try {
            fs.statSync(uploadTempDir);
        } catch (e) {
            // ディレクトリが存在しなければ作成する
            this.log.system.info(`mkdirp: ${ uploadTempDir }`);
            mkdirp.sync(uploadTempDir);
        }

        // uploader
        const storage = multer.diskStorage({
            destination: uploadTempDir,
            filename: (req, file, cb) => {
                const fileName = file.fieldname + '-' + new Date().getTime().toString(16) + Math.floor(100000 * Math.random()).toString(16);
                cb(null, fileName);

                // 切断時はファイルを削除
                (<any> req).on('close', () => {
                    const filePath = path.join(uploadTempDir, fileName);
                    fs.unlink(filePath, (err) => {
                        if (err) {
                            this.log.access.error(`upload file delete error: ${ filePath }`);
                            this.log.access.error(err.message);
                        } else {
                            this.log.access.info(`delete upload file: ${ filePath }`);
                        }
                    });
                });
            },
        });
        const upload = multer({ storage: storage });

        // init express-openapi
        openapi.initialize({
            apiDoc: api,
            app: this.app,
            consumesMiddleware: {
                'application/json': bodyParser.json(),
                'text/text': bodyParser.text(),
                'multipart/form-data': (req, res, next) => {
                    upload.fields([
                        { name: 'ts', maxCount: 1 },
                        { name: 'encode', maxCount: 5 },
                    ])(req, res, (err) => {
                        if (err) { return next(err.message); }

                        if (typeof req.files !== 'undefined') {
                            for (const fieldname in req.files) {
                                req.body[fieldname] = req.files[fieldname];
                            }
                        }

                        return next();
                    });
                },
            },
            docsPath: '/docs',
            errorMiddleware: (err, _req, res) => {
                res.status(400);
                res.json(err);
            },
            errorTransformer: (openApi) => {
                return openApi.message;
{'fraction_non_alphanumeric': 0.14381430964345537, 'fraction_numerical': 0.0033500837520938024, 'mean_word_length': 1.5617529880478087, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7364126', 'n_tokens_mistral': 2371, 'n_tokens_neox': 2266, 'n_words': 496}