1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* @file Attachment.php |
5
|
|
|
* @brief This file contains the Attachment class. |
6
|
|
|
* @details |
7
|
|
|
* @author Filippo F. Fadda |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
//! The CouchDB's attachments namespace. |
12
|
|
|
namespace EoC\Doc\Attachment; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
use Meta\Extension; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @brief |
20
|
|
|
* @todo To be documented. |
21
|
|
|
*/ |
22
|
|
|
final class Attachment { |
23
|
|
|
use Extension\TProperty; // This is a trait, not a namespace or a class. |
24
|
|
|
|
25
|
|
|
// Default CouchDB attachment content type. Here just for documentation. |
26
|
|
|
//const DEFAULT_ATTACHMENT_CONTENT_TYPE = "application/octet-stream"; |
|
|
|
|
27
|
|
|
|
28
|
|
|
private $name; |
29
|
|
|
private $stub; |
30
|
|
|
private $contentLength; |
31
|
|
|
private $contentType; |
32
|
|
|
private $data; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
private function __construct() { |
36
|
|
|
$this->stub = FALSE; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
public static function fromFile($fileName) { |
41
|
|
|
$instance = new self(); |
42
|
|
|
|
43
|
|
|
if (file_exists($fileName)) { |
44
|
|
|
if (is_dir($fileName)) |
45
|
|
|
throw new \Exception("The file $fileName is a directory."); |
46
|
|
|
|
47
|
|
|
$instance->name = basename($fileName); |
48
|
|
|
|
49
|
|
|
$fd = @fopen($fileName, "rb"); |
50
|
|
|
if (is_resource($fd)) { |
51
|
|
|
$instance->contentLength = filesize($fileName); |
52
|
|
|
|
53
|
|
|
$instance->data = fread($fd, $instance->contentLength); |
54
|
|
|
|
55
|
|
|
$finfo = finfo_open(FILEINFO_MIME_TYPE); |
56
|
|
|
$instance->contentType = finfo_file($finfo, $fileName); |
57
|
|
|
|
58
|
|
|
finfo_close($finfo); |
59
|
|
|
fclose($fd); |
60
|
|
|
|
61
|
|
|
if ($instance->data === FALSE) |
62
|
|
|
throw new \Exception("Error reading the file $fileName."); |
63
|
|
|
} |
64
|
|
|
else |
65
|
|
|
throw new \Exception("Cannot open the file $fileName."); |
66
|
|
|
} |
67
|
|
|
else |
68
|
|
|
throw new \Exception("The file $fileName doesn't exist."); |
69
|
|
|
|
70
|
|
|
return $instance; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
public static function fromArray(array $array) { |
75
|
|
|
$instance = new self(); |
76
|
|
|
|
77
|
|
|
$instance->name = key($array); |
78
|
|
|
$meta = reset($array); |
79
|
|
|
$instance->stub = (array_key_exists("stub", $meta)) ? TRUE : FALSE; |
80
|
|
|
$instance->contentLength = $meta["lenght"]; |
81
|
|
|
$instance->contentType = $meta["content_type"]; |
82
|
|
|
$instance->data = base64_decode($meta["data"]); |
83
|
|
|
|
84
|
|
|
return $instance; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
public function asArray() { |
89
|
|
|
return [ |
90
|
|
|
"content_type" => $this->contentType, |
91
|
|
|
"data" => base64_encode($this->data) |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
public function save($overwrite = TRUE) { |
97
|
|
|
$mode = ($overwrite) ? "wb" : "xb"; |
98
|
|
|
|
99
|
|
|
$fd = @fopen($this->name, $mode); |
100
|
|
|
if (is_resource($fd)) { |
101
|
|
|
$bytes = fwrite($fd, $this->data); |
102
|
|
|
fclose($fd); |
103
|
|
|
|
104
|
|
|
if ($bytes === FALSE) |
105
|
|
|
throw new \Exception("Error writing the file `$this->name`."); |
106
|
|
|
} |
107
|
|
|
else |
108
|
|
|
throw new \Exception("Cannot create the file `$this->name`."); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
|
113
|
|
|
public function getName() { |
114
|
|
|
return $this->name; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
|
118
|
|
|
public function setName($value) { |
119
|
|
|
$this->name = (string)$value; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
|
123
|
|
|
public function getStub() { |
124
|
|
|
return $this->stub; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
public function getContentType() { |
129
|
|
|
return $this->contentType; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
public function getContentLength() { |
134
|
|
|
return $this->contentLength; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
public function getData() { |
139
|
|
|
return $this->data; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
} |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.