MDL-14589 more local --> stored renaming
[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 /**
6c0e2d08 39 * Protected - developers must not gain direct access to this function
17d9269f 40 * NOTE: do not make this public, we must not modify or delete the pool files directly! ;-)
41 * @return ful path to pool file with file content
172dd12c 42 **/
43 protected function get_content_file_location() {
744b64ff 44 $filedir = $this->fs->get_filedir();
17d9269f 45 $contenthash = $this->file_record->contenthash;
46 $l1 = $contenthash[0].$contenthash[1];
47 $l2 = $contenthash[2].$contenthash[3];
48 $l3 = $contenthash[4].$contenthash[5];
49 return "$filedir/$l1/$l2/$l3/$contenthash";
172dd12c 50 }
51
52 /**
53 * Returns file handle - read only mode, no writing allowed into pool files!
54 * @return file handle
55 */
56 public function get_content_file_handle() {
57 $path = $this->get_content_file_location();
58 if (!is_readable($path)) {
59 throw new file_exception('localfilecannotread');
60 }
61 return fopen($path, 'rb'); //binary reading only!!
62 }
63
64 /**
65 * Dumps file content to page
66 * @return file handle
67 */
68 public function readfile() {
69 $path = $this->get_content_file_location();
70 if (!is_readable($path)) {
71 throw new file_exception('localfilecannotread');
72 }
73 readfile($path);
74 }
75
76 /**
77 * Returns file content as string
78 * @return string content
79 */
80 public function get_content() {
81 $path = $this->get_content_file_location();
82 if (!is_readable($path)) {
83 throw new file_exception('localfilecannotread');
84 }
85 return file_get_contents($this->get_content_file_location());
86 }
87
6c0e2d08 88 /**
89 * Copy content of file to give npathname
17d9269f 90 * @param string $pathnema rela path to new file
6c0e2d08 91 * @return bool success
92 */
93 public function copy_content_to($pathname) {
94 $path = $this->get_content_file_location();
95 if (!is_readable($path)) {
96 throw new file_exception('localfilecannotread');
97 }
98 return copy($path, $pathname);
99 }
100
17d9269f 101 /**
102 * Unzip file to given file path (real OS filesystem), existing files are overwrited
103 * @param string $path target directory
104 * @return mixed list of processed files; false if error
105 */
106 public function unzip_files_to_pathname($path) {
107 $packer = get_file_packer();
108 $zipfile = $this->get_content_file_location();
109 return $packer->unzip_files_to_pathname($path, $path);
110 }
111
112 /**
113 * Unzip file to given file path (real OS filesystem), existing files are overwrited
114 * @param int $contextid
115 * @param string $filearea
116 * @param int $itemid
117 * @param string $pathbase
118 * @param int $userid
119 * @return mixed list of processed files; false if error
120 */
121 public function unzip_files_to_storage($contextid, $filearea, $itemid, $pathbase, $userid=null) {
122 $packer = get_file_packer();
123 $zipfile = $this->get_content_file_location();
124 return $packer->unzip_files_to_storage($zipfile, $contextid, $filearea, $itemid, $pathbase);
125 }
126
b1897a6d 127 /**
128 * Add file/directory into zip archive
129 * @param object $ziparchive
130 * @param string $archivepath pathname in zip archive
131 * @return bool success
132 */
133 public function add_to_ziparchive(zip_archive $ziparch, $archivepath) {
134 if ($this->is_directory()) {
135 return $ziparch->addEmptyDir($archivepath);
136 } else {
137 $path = $this->get_content_file_location();
138 if (!is_readable($path)) {
139 return false;
140 }
141 return $ziparch->addFile($path, $archivepath);
142 }
143 }
144
172dd12c 145 public function get_contextid() {
146 return $this->file_record->contextid;
147 }
148
149 public function get_filearea() {
150 return $this->file_record->filearea;
151 }
152
153 public function get_itemid() {
154 return $this->file_record->itemid;
155 }
156
157 public function get_filepath() {
158 return $this->file_record->filepath;
159 }
160
161 public function get_filename() {
162 return $this->file_record->filename;
163 }
164
165 public function get_userid() {
166 return $this->file_record->userid;
167 }
168
169 public function get_filesize() {
170 return $this->file_record->filesize;
171 }
172
173 public function get_mimetype() {
174 return $this->file_record->mimetype;
175 }
176
177 public function get_timecreated() {
178 return $this->file_record->timecreated;
179 }
180
181 public function get_timemodified() {
182 return $this->file_record->timemodified;
183 }
6c0e2d08 184
185 public function get_status() {
186 return $this->file_record->status;
187 }
ee03a651 188
189 public function get_id() {
190 return $this->file_record->id;
191 }
172dd12c 192}