<?php
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social.  If not, see <http://www.gnu.org/licenses/>.

if (!defined('GNUSOCIAL')) { exit(1); }

/**
 * Retrieve user avatar by filename action class.
 *
 * @category Action
 * @package  GNUsocial
 * @author   Hugo Sales <someonewithpc@gmail.com>
 * @license  http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
 * @link     http://www.gnu.org/software/social/
 */

class AvatarAction extends Action
{
    public $filename = null;
    protected function prepare(array $args = [])
    {
        parent::prepare($args);
        if (empty($this->filename = $this->trimmed('file'))) {
            // TRANS: Client error displayed trying to get a non-existing avatar.
            $this->clientError(_m('No such avatar.'), 404);
        }
        return true;
    }


    /**
     * Class handler.
     *
     * @param array $args query arguments
     */
    protected function handle()
    {
        parent::handle();

        if (is_string(common_config('avatar', 'server'))) {
            common_log(LOG_INFO, "Requested avatar is stored remotely");
            common_redirect(Avatar::url($file), 302);
        } else {
            $attachment = new AttachmentAction();
            $attachment->filepath = common_config('avatar', 'dir') . $this->filename;
            common_log(LOG_ERR, $attachment->filepath);
            $attachment->sendFile();
        }
        return true;
    }
}
