Genesis::__construct()   B
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
ccs 0
cts 23
cp 0
rs 8.5806
cc 4
eloc 19
nc 8
nop 1
crap 20
1
<?php
2
/**
3
 * ownCloud - Documents App
4
 *
5
 * @author Victor Dubiniuk
6
 * @copyright 2013 Victor Dubiniuk [email protected]
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
10
 * License as published by the Free Software Foundation; either 
11
 * version 3 of the License, or any later version.
12
 * 
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
17
 *  
18
 * You should have received a copy of the GNU Affero General Public
19
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
20
 * 
21
 */
22
23
namespace OCA\Documents;
24
25
use \OC\Files\View;
26
27
class Genesis {
28
	
29
	const DOCUMENTS_DIRNAME='/documents';
30
	
31
	protected $view;
32
	
33
	protected $path;
34
	
35
	protected $hash;
36
	
37
	
38
	/**
39
	 * Create new genesis document
40
	 * @param File $file 
41
	 * */	
42
	public function __construct(File $file){
43
		$view = $file->getOwnerView();
44
		$path = $file->getPath();
45
		$owner = $file->getOwner();
46
		
47
		$this->view = new View('/' . $owner);
48
		
49
		if (!$this->view->file_exists(self::DOCUMENTS_DIRNAME)){
50
			$this->view->mkdir(self::DOCUMENTS_DIRNAME );
51
		}
52
		$this->validate($view, $path);
53
		
54
		$this->hash = $view->hash('sha1', $path, false);
55
		$this->path = self::DOCUMENTS_DIRNAME . '/' . $this->hash . '.odt';
56
		if (!$this->view->file_exists($this->path)){
57
			//copy new genesis to /user/documents/{hash}.odt
58
			// get decrypted content
59
			$content = $view->file_get_contents($path);
60
			$mimetype = $view->getMimeType($path);
61
			
62
			$data = Filter::read($content, $mimetype);
63
			$this->view->file_put_contents($this->path, $data['content']);
64
		}
65
		
66
		try {
67
			$this->validate($this->view, $this->path);
68
		} catch (\Exception $e){
69
			throw new \Exception('Failed to copy genesis');
70
		}
71
	}
72
	
73
	public function getPath(){
74
		return $this->path;
75
	}
76
	
77
	public function getHash(){
78
		return $this->hash;
79
	}
80
	
81
	/**
82
	 * Check if genesis is valid
83
	 * @param \OC\Files\View $view 
84
	 * @param string $path relative to the view
85
	 * @throws \Exception
86
	 */
87
	protected function validate($view, $path){
88
		if (!$view->file_exists($path)){
89
			throw new \Exception('Document not found ' . $path);
90
		}
91
		if (!$view->is_file($path)){
92
			throw new \Exception('Object ' . $path . ' is not a file.');
93
		}
94
		//TODO check if it is a valid odt
95
	}
96
97
}
98