{"size": 1306, "ext": "js", "max_stars_repo_path": "backend/get/index.js", "max_stars_repo_name": "Giancarl021/Tutorial-Filipe-Newsletter", "max_stars_repo_stars_event_min_datetime": "2021-08-25T02:52:44.000Z", "max_stars_repo_stars_event_max_datetime": "2021-09-13T16:15:43.000Z", "max_issues_repo_path": "backend/get/index.js", "max_issues_repo_name": "Giancarl021/Tutorial-Filipe-Newsletter", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "backend/get/index.js", "max_forks_repo_name": "Giancarl021/Tutorial-Filipe-Newsletter", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 3, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 25.6078431373, "max_line_length": 111, "alphanum_fraction": 0.5229709035}
const Blob = require('../common/blob');

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    const { date } = req.query;
    const blob = await Blob();

    context.log('Validating date');

    if (!date) {
        context.log('Invalid date');
        context.res = {
            status: 400,
            body: {
                error: 'Invalid date'
            }
        };
        return context.done();
    }

    context.log('Getting file');

    const file = await blob.download(`${date}.wav`);

    if (!file) {
        context.log('Blob does not exist');
        context.res = {
            status: 400,
            body: {
                error: 'This date does not exist in the archive'
            }
        };
        return context.done();  
    }

    context.log('Sending file');

    context.res = {
        body: file,
        headers: {
            'Content-Disposition': `attachment; filename=${date}.wav`,
            'Content-Type': 'audio/wav',
            'Content-Length': file.length,
            'Content-Range': `bytes 0-${file.length}/${file.length}`,
            'Access-Control-Expose-Headers': 'Content-Disposition, Content-Type, Content-Length, Content-Range'
        }
    }

    context.done();
}