summaryrefslogtreecommitdiff
path: root/app/Model/AvatarFile.php
blob: 9f47ccc7efdaf46f7001a4ab9ad82348e119b7f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php

namespace Kanboard\Model;

use Exception;

/**
 * Avatar File
 *
 * @package  model
 * @author   Frederic Guillot
 */
class AvatarFile extends Base
{
    /**
     * Path prefix
     *
     * @var string
     */
    const PATH_PREFIX = 'avatars';

    /**
     * Get image filename
     *
     * @access public
     * @param  integer $user_id
     * @return string
     */
    public function getFilename($user_id)
    {
        return $this->db->table(User::TABLE)->eq('id', $user_id)->findOneColumn('avatar_path');
    }

    /**
     * Add avatar in the user profile
     *
     * @access public
     * @param  integer  $user_id    Foreign key
     * @param  string   $path       Path on the disk
     * @return bool
     */
    public function create($user_id, $path)
    {
        $result = $this->db->table(User::TABLE)->eq('id', $user_id)->update(array(
            'avatar_path' => $path,
        ));

        $this->userSession->refresh($user_id);

        return $result;
    }

    /**
     * Remove avatar from the user profile
     *
     * @access public
     * @param  integer  $user_id    Foreign key
     * @return bool
     */
    public function remove($user_id)
    {
        try {
            $filename = $this->getFilename($user_id);

            if (! empty($filename)) {
                $this->objectStorage->remove($filename);
                return $this->db->table(User::TABLE)->eq('id', $user_id)->update(array('avatar_path' => ''));
            }
        } catch (Exception $e) {
            $this->logger->error($e->getMessage());
            return false;
        }

        return true;
    }

    /**
     * Upload avatar image file
     *
     * @access public
     * @param  integer $user_id
     * @param  array   $file
     * @return boolean
     */
    public function uploadImageFile($user_id, array $file)
    {
        try {
            if ($file['error'] == UPLOAD_ERR_OK && $file['size'] > 0) {
                $destinationFilename = $this->generatePath($user_id, $file['name']);
                $this->objectStorage->moveUploadedFile($file['tmp_name'], $destinationFilename);
                $this->create($user_id, $destinationFilename);
            } else {
                throw new Exception('File not uploaded: '.var_export($file['error'], true));
            }

        } catch (Exception $e) {
            $this->logger->error($e->getMessage());
            return false;
        }

        return true;
    }

    /**
     * Upload avatar image content
     *
     * @access public
     * @param  integer $user_id
     * @param  string  $blob
     * @return boolean
     */
    public function uploadImageContent($user_id, &$blob)
    {
        try {
            $destinationFilename = $this->generatePath($user_id, 'imageContent');
            $this->objectStorage->put($destinationFilename, $blob);
            $this->create($user_id, $destinationFilename);
        } catch (Exception $e) {
            $this->logger->error($e->getMessage());
            return false;
        }

        return true;
    }

    /**
     * Generate the path for a new filename
     *
     * @access public
     * @param  integer   $user_id
     * @param  string    $filename
     * @return string
     */
    public function generatePath($user_id, $filename)
    {
        return implode(DIRECTORY_SEPARATOR, array(self::PATH_PREFIX, $user_id, hash('sha1', $filename.time())));
    }
}