Skip to content

Commit c2db63a

Browse files
committed
Allow retrieval of attachment content
1 parent 1591fd0 commit c2db63a

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/libraries/Attachment.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,29 @@ public static function getAll($parent_type, $parent_id) {
112112
}
113113

114114
public function getContent() {
115-
return "NOT YET IMPLEMENTED"; // TODO: Retrieve file from database
115+
if (!isset(Common::$database)) {
116+
Common::$database = DatabaseDriver::getDatabaseObject();
117+
}
118+
try {
119+
$stmt = Common::$database->prepare("
120+
SELECT `content`
121+
FROM `attachments`
122+
WHERE `id` = :id
123+
LIMIT 1;
124+
");
125+
$stmt->bindParam(":id", $this->id, PDO::PARAM_INT);
126+
if (!$stmt->execute()) {
127+
throw new QueryException("Cannot get attachment content");
128+
} else if ($stmt->rowCount() == 0) {
129+
throw new AttachmentNotFoundException($this->id);
130+
}
131+
$row = $stmt->fetch(PDO::FETCH_OBJ);
132+
$stmt->closeCursor();
133+
return $row->content;
134+
} catch (PDOException $e) {
135+
throw new QueryException("Cannot get attachment content", $e);
136+
}
137+
return false;
116138
}
117139

118140
public function getContentSize($format = false) {

0 commit comments

Comments
 (0)