import os

from flask import abort
from flask import jsonify
from flask import request
from flask.views import View


class FolderInformation(View):
    """
    receive a directory, check if it exists and return this as a boolean value
    """
    methods = ['POST']

    def dispatch_request(self):
        # extract request data
        data = request.get_json(force=True)

        if 'folder' not in data:
            return abort(400)

        folder = data['folder']

        # check if directory exists
        result = {
            'exists': os.path.exists(folder)
        }

        # count files
        if result['exists']:
            result['count'] = len(os.listdir(folder))

        # send result
        return jsonify(result)
