Completed
Pull Request — master (#210)
by korelstar
35:08
created

Note::fromFile()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.2888
c 0
b 0
f 0
cc 5
nc 3
nop 3
1
<?php
2
/**
3
 * Nextcloud - Notes
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Bernhard Posselt <[email protected]>
9
 * @copyright Bernhard Posselt 2012, 2014
10
 */
11
12
namespace OCA\Notes\Db;
13
14
use OCP\Files\File;
15
use OCP\Files\Folder;
16
use OCP\AppFramework\Db\Entity;
17
use League\Flysystem\FileNotFoundException;
18
/**
19
 * Class Note
20
 * @method integer getId()
21
 * @method void setId(integer $value)
22
 * @method string getEtag()
23
 * @method void setEtag(string $value)
24
 * @method integer getModified()
25
 * @method void setModified(integer $value)
26
 * @method string getTitle()
27
 * @method void setTitle(string $value)
28
 * @method string getCategory()
29
 * @method void setCategory(string $value)
30
 * @method string getContent()
31
 * @method void setContent(string $value)
32
 * @method boolean getFavorite()
33
 * @method void setFavorite(boolean $value)
34
 * @method boolean getError()
35
 * @method void setError(boolean $value)
36
 * @method string getErrorMessage()
37
 * @method void setErrorMessage(string $value)
38
 * @package OCA\Notes\Db
39
 */
40
class Note extends Entity {
41
    public $etag;
42
    public $modified;
43
    public $title;
44
    public $category;
45
    public $content;
46
    public $favorite = false;
47
    public $error = false;
48
    public $errorMessage='';
49
50
    public function __construct() {
51
        $this->addType('modified', 'integer');
52
        $this->addType('favorite', 'boolean');
53
    }
54
55
    /**
56
     * @param File $file
57
     * @return static
58
     */
59
    public static function fromFile(File $file, Folder $notesFolder, $tags=[]){
60
        $note = new static();
61
        $note->setId($file->getId());
62
        $fileContent=$file->getContent();
63
        if($fileContent===false){
64
            throw new FileNotFoundException("File not found");
65
        }
66
        $note->setContent(self::convertEncoding($fileContent));
67
        $note->setModified($file->getMTime());
68
        $note->setTitle(pathinfo($file->getName(),PATHINFO_FILENAME)); // remove extension
69
        $subdir = substr(dirname($file->getPath()), strlen($notesFolder->getPath())+1);
70
        $note->setCategory($subdir ? $subdir : '');
71
        if(is_array($tags) && in_array(\OC\Tags::TAG_FAVORITE, $tags)) {
72
            $note->setFavorite(true);
73
            //unset($tags[array_search(\OC\Tags::TAG_FAVORITE, $tags)]);
74
        }
75
        $note->updateETag();
76
        $note->resetUpdatedFields();
77
        return $note;
78
    }
79
    /**
80
     * @param File $file
81
     * @return static
82
     */
83
    public static function fromException($message,File $file,Folder $notesFolder,$tags=[]){
84
        $note = new static();
85
        $note->setId($file->getId());
86
        $note->setErrorMessage($message);
87
        $note->setError(true);
88
        $note->setContent($message);
89
        $note->setModified(null);
90
        $note->setTitle(pathinfo($file->getName(),PATHINFO_FILENAME)); // remove extension
91
        $subdir = substr(dirname($file->getPath()), strlen($notesFolder->getPath())+1);
92
        $note->setCategory($subdir ? $subdir : null);
93
        if(is_array($tags) && in_array(\OC\Tags::TAG_FAVORITE, $tags)) {
94
            $note->setFavorite(true);
95
            //unset($tags[array_search(\OC\Tags::TAG_FAVORITE, $tags)]);
96
        }
97
        $note->updateETag();
98
        $note->resetUpdatedFields();
99
        return $note;
100
    }
101
102
    private static function convertEncoding($str) {
103
        if(!mb_check_encoding($str, 'UTF-8')) {
104
            $str = mb_convert_encoding($str, 'UTF-8');
105
        }
106
        return $str;
107
    }
108
109
    private function updateETag() {
110
        // collect all relevant attributes
111
        $data = '';
112
        foreach(get_object_vars($this) as $key => $val) {
113
            if($key!=='etag') {
114
                $data .= $val;
115
            }
116
        }
117
        $etag = md5($data);
118
        $this->setEtag($etag);
119
    }
120
}
121