<commit_before><?php

namespace Rogue\Services;

use Log;
use finfo;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;

class AWS
{
    /**
     * Store a reportback item (image) in S3.
     *
     * @param \Symfony\Component\HttpFoundation\File\UploadedFile|string $file
     *  File object, or a base-64 encoded data URI
     * @param string $filename - Filename to write image to
     *
     * @return string - URL of stored image
     */
    public function storeImage($file, $filename)
    {
        if (is_string($file)) {
            $data = $this->base64StringToDataString($file);
        } else {
            $data = file_get_contents($file->getPathname());
        }

        $extension = $this->guessExtension($data);

        // Make sure we're only uploading valid image types
        if (! in_array($extension, ['jpeg', 'png', 'gif'])) {
            throw new UnprocessableEntityHttpException('Invalid file type. Upload a JPEG, PNG or GIF.');
        }

        // Add a unique timestamp (e.g. uploads/folder/filename-1456498664.jpeg) to
        // uploads to prevent AWS cache giving the user an old upload.
        $path = '/uploads/reportback-items' . '/' . $filename . '-' . md5($data) . '-' . time() . '.' . $extension;

        // Push file to S3.
        $success = Storage::put($path, $data);

        if (! $success) {
            throw new HttpException(500, 'Unable to save image to S3.');
        }

        return Storage::url($path);
    }

    /**
     * Store a reportback item (image) in S3.
     *
     * @param string $data - File data
     *
     * @return string - URL of stored image
     */
    public function storeImageData($data, $filename)
    {
        $extension = $this->guessExtension($data);

        // Make sure we're only uploading valid image types
        if (! in_array($extension, ['jpeg', 'png', 'gif'])) {
            throw new UnprocessableEntityHttpException('Invalid file type. Upload a JPEG, PNG or GIF.');
        }

        $path = '/uploads/reportback-items/' . $filename . '.' . $extension;

        // Push file to S3.
        $success = Storage::put($path, $data);

        if (! $success) {
            throw new HttpException(500, 'Unable to save image to S3.');
        }

        return Storage::url($path);
    }

    /**
     * Guess the extension from a data buffer string.
     * @param string $data - Data buffer string
     * @return string - file extension
     */
    protected function guessExtension($data)
    {
        $f = new finfo();
        $mimeType = $f->buffer($data, FILEINFO_MIME_TYPE);<commit_msg>Fix issue where extra '/' was added to URL.<commit_after><?php

namespace Rogue\Services;

use Log;
use finfo;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;

class AWS
{
    /**
     * Store a reportback item (image) in S3.
     *
     * @param \Symfony\Component\HttpFoundation\File\UploadedFile|string $file
     *  File object, or a base-64 encoded data URI
     * @param string $filename - Filename to write image to
     *
     * @return string - URL of stored image
     */
    public function storeImage($file, $filename)
    {
        if (is_string($file)) {
            $data = $this->base64StringToDataString($file);
        } else {
            $data = file_get_contents($file->getPathname());
        }

        $extension = $this->guessExtension($data);

        // Make sure we're only uploading valid image types
        if (! in_array($extension, ['jpeg', 'png', 'gif'])) {
            throw new UnprocessableEntityHttpException('Invalid file type. Upload a JPEG, PNG or GIF.');
        }

        // Add a unique timestamp (e.g. uploads/folder/filename-1456498664.jpeg) to
        // uploads to prevent AWS cache giving the user an old upload.
        $path = 'uploads/reportback-items' . '/' . $filename . '-' . md5($data) . '-' . time() . '.' . $extension;

        // Push file to S3.
        $success = Storage::put($path, $data);

        if (! $success) {
            throw new HttpException(500, 'Unable to save image to S3.');
        }

        return Storage::url($path);
    }

    /**
     * Store a reportback item (image) in S3.
     *
     * @param string $data - File data
     *
     * @return string - URL of stored image
     */
    public function storeImageData($data, $filename)
    {
        $extension = $this->guessExtension($data);

        // Make sure we're only uploading valid image types
        if (! in_array($extension, ['jpeg', 'png', 'gif'])) {
            throw new UnprocessableEntityHttpException('Invalid file type. Upload a JPEG, PNG or GIF.');
        }

        $path = 'uploads/reportback-items/' . $filename . '.' . $extension;

        // Push file to S3.
        $success = Storage::put($path, $data);

        if (! $success) {
            throw new HttpException(500, 'Unable to save image to S3.');
        }

        return Storage::url($path);
    }

    /**
     * Guess the extension from a data buffer string.
     * @param string $data - Data buffer string
     * @return string - file extension
     */
    protected function guessExtension($data)
    {
        $f = new finfo();
        $mimeType = $f->buffer($data, FILEINFO_MIME_TYPE);
{'fraction_non_alphanumeric': 0.12911301859799715, 'fraction_numerical': 0.009656652360515022, 'mean_word_length': 2.3075103489059727, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '3785484', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1603, 'n_words': 464}