<commit_before><?php
require_once("dbconnection.php");
require_once("users.php");
require_once("editors.php");
require_once("return_package.php");

class media extends dbconnection
{
    private static function defaultMediaObject($mediaId)
    {
        $fake_sql_media = new stdClass;
        $fake_sql_media->game_id = 0;
        $fake_sql_media->media_id = $mediaId;
        $fake_sql_media->name = "Default NPC";
        $fake_sql_media->file_folder = "0";
        $fake_sql_media->file_name = "npc.png";
        return media::mediaObjectFromSQL($fake_sql_media);
    }

    //Takes in media JSON, all fields optional except user_id + key
    public static function createMedia($pack)
    {
        $pack->auth->permission = "read_write";
        if(!users::authenticateUser($pack->auth)) return new return_package(6, NULL, "Failed Authentication");

        $filenameext = strtolower(substr($pack->file_name,strrpos($pack->file_name,'.')+1));
        if($filenameext == "jpeg") $filenameext = "jpg"; //sanity
        $filename = md5((string)microtime().$pack->file_name);
        $newfilename = 'aris'.$filename.'.'.$filenameext;
        $resizedfilename = 'aris'.$filename.'_resized.'.$filenameext;
        $newthumbfilename = 'aris'.$filename.'_128.'.$filenameext;

        if(
                //Images
                $filenameext != "jpg" &&
                $filenameext != "png" &&
                $filenameext != "gif" &&
                //Video
                $filenameext != "mp4" &&
                $filenameext != "mov" &&
                $filenameext != "m4v" &&
                $filenameext != "3gp" &&
                //Audio
                $filenameext != "caf" &&
                $filenameext != "mp3" &&
                $filenameext != "aac" &&
                $filenameext != "m4a"
        )
        return new return_package(1,NULL,"Invalid filetype: '{$filenameext}'");

        $filefolder = "";
        if($pack->game_id) $filefolder = $pack->game_id;
        else               $filefolder = "players";
        $fspath      = Config::v2_gamedata_folder."/".$filefolder."/".$newfilename;
        $resizedpath = Config::v2_gamedata_folder."/".$filefolder."/".$resizedfilename;
        $fsthumbpath = Config::v2_gamedata_folder."/".$filefolder."/".$newthumbfilename;

        $fp = fopen($fspath, 'w');
        if(!$fp) return new return_package(1,NULL,"Couldn't open file:$fspath");
        fwrite($fp,base64_decode($pack->data));
        fclose($fp);



        $did_resize = false;
        if($filenameext == "jpg" || $filenameext == "png" || $filenameext == "gif")
        {
            if(isset($pack->resize))
            {
                $image = new Imagick($fspath);

                // Reorient based on EXIF tag
                switch ($image->getImageOrientation()) {
                    case Imagick::ORIENTATION_UNDEFINED:
                        // We assume normal orientation
                        break;
                    case Imagick::ORIENTATION_TOPLEFT:
                        // All good
                        break;
                    case Imagick::ORIENTATION_TOPRIGHT:
                        $image->flopImage();
                        break;
                    case Imagick::ORIENTATION_BOTTOMRIGHT:
                        $image->rotateImage('#000', 180);
                        break;
                    case Imagick::ORIENTATION_BOTTOMLEFT:
                        $image->rotateImage('#000', 180);
                        $image->flopImage();
                        break;
                    case Imagick::ORIENTATION_LEFTTOP:
                        $image->rotateImage('#000', 90);
                        $image->flopImage();
                        break;
                    case Imagick::ORIENTATION_RIGHTTOP:
                        $image->rotateImage('#000', 90);
                        break;
                    case Imagick::ORIENTATION_RIGHTBOTTOM:
                        $image->rotateImage('#000', -90);
                        $image->flopImage();
                        break;
                    case Imagick::ORIENTATION_LEFTBOTTOM:
                        $image->rotateImage('#000', -90);
                        break;
                }
                $image->setImageOrientation(Imagick::ORIENTATION_TOPLEFT);

                // Resize image proportionally so min(width, height) == $pack->resize
                if ($image->getImageWidth() < $image->getImageHeight()) {
                  $image->resizeImage($pack->resize, 0, Imagick::FILTER_LANCZOS, 1);
                }
                else {
                  $image->resizeImage(0, $pack->resize, Imagick::FILTER_LANCZOS, 1);
                }

                $image->setImageCompression(Imagick::COMPRESSION_JPEG);
                $image->setImageCompressionQuality(40);
                $image->writeImage($resizedpath);
                $did_resize = true;
            }

/* //tmp disable to get upload back online
            $image = new Imagick($fspath);
            //aspect fill to 128x128
            $w = $image->getImageWidth();
            $h = $image->getImageHeight();
            if($w < $h) $image->thumbnailImage(128, 0, 1, 1);
            else        $image->thumbnailImage(0, 128, 1, 1);
            //crop around center
            $w = $image->getImageWidth();
            $h = $image->getImageHeight();
            $image->cropImage(128, 128, ($w-128)/2, ($h-128)/2);
            $image->writeImage($fsthumbpath);
*/
        }

        if($did_resize) unlink($fspath); // after making the 128 thumbnail

        $pack->media_id = dbconnection::queryInsert(
            "INSERT INTO media (".
            "file_folder,".
            "file_name,".<commit_msg>sidestep media json decode because it botches base64<commit_after><?php
require_once("dbconnection.php");
require_once("users.php");
require_once("editors.php");
require_once("return_package.php");

class media extends dbconnection
{
    private static function defaultMediaObject($mediaId)
    {
        $fake_sql_media = new stdClass;
        $fake_sql_media->game_id = 0;
        $fake_sql_media->media_id = $mediaId;
        $fake_sql_media->name = "Default NPC";
        $fake_sql_media->file_folder = "0";
        $fake_sql_media->file_name = "npc.png";
        return media::mediaObjectFromSQL($fake_sql_media);
    }

    //Takes in media JSON, all fields optional except user_id + key
    public static function createMedia($pack)
    {
        $data = file_get_contents("php://input"); $pack = json_decode($data);

        $pack->auth->permission = "read_write";
        if(!users::authenticateUser($pack->auth)) return new return_package(6, NULL, "Failed Authentication");

        $filenameext = strtolower(substr($pack->file_name,strrpos($pack->file_name,'.')+1));
        if($filenameext == "jpeg") $filenameext = "jpg"; //sanity
        $filename = md5((string)microtime().$pack->file_name);
        $newfilename = 'aris'.$filename.'.'.$filenameext;
        $resizedfilename = 'aris'.$filename.'_resized.'.$filenameext;
        $newthumbfilename = 'aris'.$filename.'_128.'.$filenameext;

        if(
                //Images
                $filenameext != "jpg" &&
                $filenameext != "png" &&
                $filenameext != "gif" &&
                //Video
                $filenameext != "mp4" &&
                $filenameext != "mov" &&
                $filenameext != "m4v" &&
                $filenameext != "3gp" &&
                //Audio
                $filenameext != "caf" &&
                $filenameext != "mp3" &&
                $filenameext != "aac" &&
                $filenameext != "m4a"
        )
        return new return_package(1,NULL,"Invalid filetype: '{$filenameext}'");

        $filefolder = "";
        if($pack->game_id) $filefolder = $pack->game_id;
        else               $filefolder = "players";
        $fspath      = Config::v2_gamedata_folder."/".$filefolder."/".$newfilename;
        $resizedpath = Config::v2_gamedata_folder."/".$filefolder."/".$resizedfilename;
        $fsthumbpath = Config::v2_gamedata_folder."/".$filefolder."/".$newthumbfilename;

        $fp = fopen($fspath, 'w');
        if(!$fp) return new return_package(1,NULL,"Couldn't open file:$fspath");
        fwrite($fp,base64_decode($pack->data));
        fclose($fp);



        $did_resize = false;
        if($filenameext == "jpg" || $filenameext == "png" || $filenameext == "gif")
        {
            if(isset($pack->resize))
            {
                $image = new Imagick($fspath);

                // Reorient based on EXIF tag
                switch ($image->getImageOrientation()) {
                    case Imagick::ORIENTATION_UNDEFINED:
                        // We assume normal orientation
                        break;
                    case Imagick::ORIENTATION_TOPLEFT:
                        // All good
                        break;
                    case Imagick::ORIENTATION_TOPRIGHT:
                        $image->flopImage();
                        break;
                    case Imagick::ORIENTATION_BOTTOMRIGHT:
                        $image->rotateImage('#000', 180);
                        break;
                    case Imagick::ORIENTATION_BOTTOMLEFT:
                        $image->rotateImage('#000', 180);
                        $image->flopImage();
                        break;
                    case Imagick::ORIENTATION_LEFTTOP:
                        $image->rotateImage('#000', 90);
                        $image->flopImage();
                        break;
                    case Imagick::ORIENTATION_RIGHTTOP:
                        $image->rotateImage('#000', 90);
                        break;
                    case Imagick::ORIENTATION_RIGHTBOTTOM:
                        $image->rotateImage('#000', -90);
                        $image->flopImage();
                        break;
                    case Imagick::ORIENTATION_LEFTBOTTOM:
                        $image->rotateImage('#000', -90);
                        break;
                }
                $image->setImageOrientation(Imagick::ORIENTATION_TOPLEFT);

                // Resize image proportionally so min(width, height) == $pack->resize
                if ($image->getImageWidth() < $image->getImageHeight()) {
                  $image->resizeImage($pack->resize, 0, Imagick::FILTER_LANCZOS, 1);
                }
                else {
                  $image->resizeImage(0, $pack->resize, Imagick::FILTER_LANCZOS, 1);
                }

                $image->setImageCompression(Imagick::COMPRESSION_JPEG);
                $image->setImageCompressionQuality(40);
                $image->writeImage($resizedpath);
                $did_resize = true;
            }

            $image = new Imagick($fspath);
            //aspect fill to 128x128
            $w = $image->getImageWidth();
            $h = $image->getImageHeight();
            if($w < $h) $image->thumbnailImage(128, 0, 1, 1);
            else        $image->thumbnailImage(0, 128, 1, 1);
            //crop around center
            $w = $image->getImageWidth();
            $h = $image->getImageHeight();
            $image->cropImage(128, 128, ($w-128)/2, ($h-128)/2);
            $image->writeImage($fsthumbpath);
        }

        if($did_resize) unlink($fspath); // after making the 128 thumbnail

        $pack->media_id = dbconnection::queryInsert(
            "INSERT INTO media (".
            "file_folder,".
            "file_name,".
{'fraction_non_alphanumeric': 0.13830155979202774, 'fraction_numerical': 0.016291161178509532, 'mean_word_length': 1.6373400365630713, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 95, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6500556', 'n_tokens_mistral': 3427, 'n_tokens_neox': 3151, 'n_words': 543}