from os import path, listdir

from flask import request, abort, jsonify
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': path.exists(folder)
        }

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

        # send result
        return jsonify(result)
