MDL-14591: portfolio - fixing regression I introduced when refactoring
[moodle.git] / lib / file / stored_file.php
CommitLineData
172dd12c 1<?php //$Id$
2
3/**
4 * Class representing local files stored in sha1 file pool
5 */
6class stored_file {
7 private $fs;
8 private $file_record;
9
10 /**
11 * Constructor
12 * @param object $fs file storage instance
13 * @param object $file_record description of file
14 */
15 public function __construct($fs, $file_record) {
16 $this->fs = $fs;
17 $this->file_record = clone($file_record);
18 }
19
20 /**
21 * Is this a directory?
22 * @return bool
23 */
24 public function is_directory() {
25 return $this->file_record->filename === '.';
26 }
27
28 /**
29 * Delete file
30 * @return success
31 */
32 public function delete() {
33 global $DB;
34 $this->fs->mark_delete_candidate($this->file_record->contenthash);
35 return $DB->delete_records('files', array('id'=>$this->file_record->id));
36 }
37
38 /**
39 * Protected - devs must not gain direct access to this function
40 **/
41 protected function get_content_file_location() {
42 // NOTE: do not make this public, we must not modify or delete the pool files directly! ;-)
43 $hashpath = $this->fs->path_from_hash($this->file_record->contenthash);
44 return $hashpath.'/'.$this->file_record->contenthash;
45 }
46
47 /**
48 * Returns file handle - read only mode, no writing allowed into pool files!
49 * @return file handle
50 */
51 public function get_content_file_handle() {
52 $path = $this->get_content_file_location();
53 if (!is_readable($path)) {
54 throw new file_exception('localfilecannotread');
55 }
56 return fopen($path, 'rb'); //binary reading only!!
57 }
58
59 /**
60 * Dumps file content to page
61 * @return file handle
62 */
63 public function readfile() {
64 $path = $this->get_content_file_location();
65 if (!is_readable($path)) {
66 throw new file_exception('localfilecannotread');
67 }
68 readfile($path);
69 }
70
71 /**
72 * Returns file content as string
73 * @return string content
74 */
75 public function get_content() {
76 $path = $this->get_content_file_location();
77 if (!is_readable($path)) {
78 throw new file_exception('localfilecannotread');
79 }
80 return file_get_contents($this->get_content_file_location());
81 }
82
83 public function get_contextid() {
84 return $this->file_record->contextid;
85 }
86
87 public function get_filearea() {
88 return $this->file_record->filearea;
89 }
90
91 public function get_itemid() {
92 return $this->file_record->itemid;
93 }
94
95 public function get_filepath() {
96 return $this->file_record->filepath;
97 }
98
99 public function get_filename() {
100 return $this->file_record->filename;
101 }
102
103 public function get_userid() {
104 return $this->file_record->userid;
105 }
106
107 public function get_filesize() {
108 return $this->file_record->filesize;
109 }
110
111 public function get_mimetype() {
112 return $this->file_record->mimetype;
113 }
114
115 public function get_timecreated() {
116 return $this->file_record->timecreated;
117 }
118
119 public function get_timemodified() {
120 return $this->file_record->timemodified;
121 }
122}