forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_storage.php
873 lines (776 loc) · 29.7 KB
/
file_storage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Class \core_h5p\file_storage.
*
* @package core_h5p
* @copyright 2019 Victor Deniz <[email protected]>, base on code by Joubel AS
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_h5p;
use stored_file;
use Moodle\H5PCore;
use Moodle\H5peditorFile;
use Moodle\H5PFileStorage;
/**
* Class to handle storage and export of H5P Content.
*
* @package core_h5p
* @copyright 2019 Victor Deniz <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class file_storage implements H5PFileStorage {
/** The component for H5P. */
public const COMPONENT = 'core_h5p';
/** The library file area. */
public const LIBRARY_FILEAREA = 'libraries';
/** The content file area */
public const CONTENT_FILEAREA = 'content';
/** The cached assest file area. */
public const CACHED_ASSETS_FILEAREA = 'cachedassets';
/** The export file area */
public const EXPORT_FILEAREA = 'export';
/** The icon filename */
public const ICON_FILENAME = 'icon.svg';
/**
* The editor file area.
* @deprecated since Moodle 3.10 MDL-68909. Please do not use this constant any more.
* @todo MDL-69530 This will be deleted in Moodle 4.2.
*/
public const EDITOR_FILEAREA = 'editor';
/**
* @var \context $context Currently we use the system context everywhere.
* Don't feel forced to keep it this way in the future.
*/
protected $context;
/** @var \file_storage $fs File storage. */
protected $fs;
/**
* Initial setup for file_storage.
*/
public function __construct() {
// Currently everything uses the system context.
$this->context = \context_system::instance();
$this->fs = get_file_storage();
}
/**
* Stores a H5P library in the Moodle filesystem.
*
* @param array $library Library properties.
*/
public function saveLibrary($library) {
$options = [
'contextid' => $this->context->id,
'component' => self::COMPONENT,
'filearea' => self::LIBRARY_FILEAREA,
'filepath' => '/' . H5PCore::libraryToString($library, true) . '/',
'itemid' => $library['libraryId']
];
// Easiest approach: delete the existing library version and copy the new one.
$this->delete_library($library);
$this->copy_directory($library['uploadDirectory'], $options);
}
/**
* Store the content folder.
*
* @param string $source Path on file system to content directory.
* @param array $content Content properties
*/
public function saveContent($source, $content) {
$options = [
'contextid' => $this->context->id,
'component' => self::COMPONENT,
'filearea' => self::CONTENT_FILEAREA,
'itemid' => $content['id'],
'filepath' => '/',
];
$this->delete_directory($this->context->id, self::COMPONENT, self::CONTENT_FILEAREA, $content['id']);
// Copy content directory into Moodle filesystem.
$this->copy_directory($source, $options);
}
/**
* Remove content folder.
*
* @param array $content Content properties
*/
public function deleteContent($content) {
$this->delete_directory($this->context->id, self::COMPONENT, self::CONTENT_FILEAREA, $content['id']);
}
/**
* Creates a stored copy of the content folder.
*
* @param string $id Identifier of content to clone.
* @param int $newid The cloned content's identifier
*/
public function cloneContent($id, $newid) {
// Not implemented in Moodle.
}
/**
* Get path to a new unique tmp folder.
* Please note this needs to not be a directory.
*
* @return string Path
*/
public function getTmpPath(): string {
return make_request_directory() . '/' . uniqid('h5p-');
}
/**
* Fetch content folder and save in target directory.
*
* @param int $id Content identifier
* @param string $target Where the content folder will be saved
*/
public function exportContent($id, $target) {
$this->export_file_tree($target, $this->context->id, self::CONTENT_FILEAREA, '/', $id);
}
/**
* Fetch library folder and save in target directory.
*
* @param array $library Library properties
* @param string $target Where the library folder will be saved
*/
public function exportLibrary($library, $target) {
$folder = H5PCore::libraryToString($library, true);
$this->export_file_tree($target . '/' . $folder, $this->context->id, self::LIBRARY_FILEAREA,
'/' . $folder . '/', $library['libraryId']);
}
/**
* Save export in file system
*
* @param string $source Path on file system to temporary export file.
* @param string $filename Name of export file.
*/
public function saveExport($source, $filename) {
global $USER;
// Remove old export.
$this->deleteExport($filename);
$filerecord = [
'contextid' => $this->context->id,
'component' => self::COMPONENT,
'filearea' => self::EXPORT_FILEAREA,
'itemid' => 0,
'filepath' => '/',
'filename' => $filename,
'userid' => $USER->id
];
$this->fs->create_file_from_pathname($filerecord, $source);
}
/**
* Removes given export file
*
* @param string $filename filename of the export to delete.
*/
public function deleteExport($filename) {
$file = $this->get_export_file($filename);
if ($file) {
$file->delete();
}
}
/**
* Check if the given export file exists
*
* @param string $filename The export file to check.
* @return boolean True if the export file exists.
*/
public function hasExport($filename) {
return !!$this->get_export_file($filename);
}
/**
* Will concatenate all JavaScrips and Stylesheets into two files in order
* to improve page performance.
*
* @param array $files A set of all the assets required for content to display
* @param string $key Hashed key for cached asset
*/
public function cacheAssets(&$files, $key) {
foreach ($files as $type => $assets) {
if (empty($assets)) {
continue;
}
// Create new file for cached assets.
$ext = ($type === 'scripts' ? 'js' : 'css');
$filename = $key . '.' . $ext;
$fileinfo = [
'contextid' => $this->context->id,
'component' => self::COMPONENT,
'filearea' => self::CACHED_ASSETS_FILEAREA,
'itemid' => 0,
'filepath' => '/',
'filename' => $filename
];
// Store concatenated content.
$this->fs->create_file_from_string($fileinfo, $this->concatenate_files($assets, $type, $this->context));
$files[$type] = [
(object) [
'path' => '/' . self::CACHED_ASSETS_FILEAREA . '/' . $filename,
'version' => ''
]
];
}
}
/**
* Will check if there are cache assets available for content.
*
* @param string $key Hashed key for cached asset
* @return array
*/
public function getCachedAssets($key) {
$files = [];
$js = $this->fs->get_file($this->context->id, self::COMPONENT, self::CACHED_ASSETS_FILEAREA, 0, '/', "{$key}.js");
if ($js && $js->get_filesize() > 0) {
$files['scripts'] = [
(object) [
'path' => '/' . self::CACHED_ASSETS_FILEAREA . '/' . "{$key}.js",
'version' => ''
]
];
}
$css = $this->fs->get_file($this->context->id, self::COMPONENT, self::CACHED_ASSETS_FILEAREA, 0, '/', "{$key}.css");
if ($css && $css->get_filesize() > 0) {
$files['styles'] = [
(object) [
'path' => '/' . self::CACHED_ASSETS_FILEAREA . '/' . "{$key}.css",
'version' => ''
]
];
}
return empty($files) ? null : $files;
}
/**
* Remove the aggregated cache files.
*
* @param array $keys The hash keys of removed files
*/
public function deleteCachedAssets($keys) {
if (empty($keys)) {
return;
}
foreach ($keys as $hash) {
foreach (['js', 'css'] as $type) {
$cachedasset = $this->fs->get_file($this->context->id, self::COMPONENT, self::CACHED_ASSETS_FILEAREA, 0, '/',
"{$hash}.{$type}");
if ($cachedasset) {
$cachedasset->delete();
}
}
}
}
/**
* Read file content of given file and then return it.
*
* @param string $filepath
* @return string contents
*/
public function getContent($filepath) {
list(
'filearea' => $filearea,
'filepath' => $filepath,
'filename' => $filename,
'itemid' => $itemid
) = $this->get_file_elements_from_filepath($filepath);
if (!$itemid) {
throw new \file_serving_exception('Could not retrieve the requested file, check your file permissions.');
}
// Locate file.
$file = $this->fs->get_file($this->context->id, self::COMPONENT, $filearea, $itemid, $filepath, $filename);
// Return content.
return $file->get_content();
}
/**
* Save files uploaded through the editor.
*
* @param H5peditorFile $file
* @param int $contentid
*
* @return int The id of the saved file.
*/
public function saveFile($file, $contentid) {
global $USER;
$context = $this->context->id;
$component = self::COMPONENT;
$filearea = self::CONTENT_FILEAREA;
if ($contentid === 0) {
$usercontext = \context_user::instance($USER->id);
$context = $usercontext->id;
$component = 'user';
$filearea = 'draft';
}
$record = array(
'contextid' => $context,
'component' => $component,
'filearea' => $filearea,
'itemid' => $contentid,
'filepath' => '/' . $file->getType() . 's/',
'filename' => $file->getName()
);
$storedfile = $this->fs->create_file_from_pathname($record, $_FILES['file']['tmp_name']);
return $storedfile->get_id();
}
/**
* Copy a file from another content or editor tmp dir.
* Used when copy pasting content in H5P.
*
* @param string $file path + name
* @param string|int $fromid Content ID or 'editor' string
* @param \stdClass $tocontent Target Content
*
* @return void
*/
public function cloneContentFile($file, $fromid, $tocontent): void {
// Determine source filearea and itemid.
if ($fromid === 'editor') {
$sourcefilearea = 'draft';
$sourceitemid = 0;
} else {
$sourcefilearea = self::CONTENT_FILEAREA;
$sourceitemid = (int)$fromid;
}
$filepath = '/' . dirname($file) . '/';
$filename = basename($file);
// Check to see if source exists.
$sourcefile = $this->get_file($sourcefilearea, $sourceitemid, $file);
if ($sourcefile === null) {
return; // Nothing to copy from.
}
// Check to make sure that file doesn't exist already in target.
$targetfile = $this->get_file(self::CONTENT_FILEAREA, $tocontent->id, $file);
if ( $targetfile !== null) {
return; // File exists, no need to copy.
}
// Create new file record.
$record = [
'contextid' => $this->context->id,
'component' => self::COMPONENT,
'filearea' => self::CONTENT_FILEAREA,
'itemid' => $tocontent->id,
'filepath' => $filepath,
'filename' => $filename,
];
$this->fs->create_file_from_storedfile($record, $sourcefile);
}
/**
* Copy content from one directory to another.
* Defaults to cloning content from the current temporary upload folder to the editor path.
*
* @param string $source path to source directory
* @param string $contentid Id of content
*
*/
public function moveContentDirectory($source, $contentid = null) {
$contentidint = (int)$contentid;
if ($source === null) {
return;
}
// Get H5P and content json.
$contentsource = $source . '/content';
// Move all temporary content files to editor.
$it = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($contentsource,\RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST
);
$it->rewind();
while ($it->valid()) {
$item = $it->current();
$pathname = $it->getPathname();
if (!$item->isDir() && !($item->getFilename() === 'content.json')) {
$this->move_file($pathname, $contentidint);
}
$it->next();
}
}
/**
* Get the file URL or given library and then return it.
*
* @param int $itemid
* @param string $machinename
* @param int $majorversion
* @param int $minorversion
* @return string url or false if the file doesn't exist
*/
public function get_icon_url(int $itemid, string $machinename, int $majorversion, int $minorversion) {
$filepath = '/' . "{$machinename}-{$majorversion}.{$minorversion}" . '/';
if ($file = $this->fs->get_file(
$this->context->id,
self::COMPONENT,
self::LIBRARY_FILEAREA,
$itemid,
$filepath,
self::ICON_FILENAME)
) {
$iconurl = \moodle_url::make_pluginfile_url(
$this->context->id,
self::COMPONENT,
self::LIBRARY_FILEAREA,
$itemid,
$filepath,
$file->get_filename());
// Return image URL.
return $iconurl->out();
}
return false;
}
/**
* Checks to see if an H5P content has the given file.
*
* @param string $file File path and name.
* @param int $content Content id.
*
* @return int|null File ID or NULL if not found
*/
public function getContentFile($file, $content): ?int {
if (is_object($content)) {
$content = $content->id;
}
$contentfile = $this->get_file(self::CONTENT_FILEAREA, $content, $file);
return ($contentfile === null ? null : $contentfile->get_id());
}
/**
* Remove content files that are no longer used.
*
* Used when saving content.
*
* @param string $file File path and name.
* @param int $contentid Content id.
*
* @return void
*/
public function removeContentFile($file, $contentid): void {
// Although the interface defines $contentid as int, object given in H5peditor::processParameters.
if (is_object($contentid)) {
$contentid = $contentid->id;
}
$existingfile = $this->get_file(self::CONTENT_FILEAREA, $contentid, $file);
if ($existingfile !== null) {
$existingfile->delete();
}
}
/**
* Check if server setup has write permission to
* the required folders
*
* @return bool True if server has the proper write access
*/
public function hasWriteAccess() {
// Moodle has access to the files table which is where all of the folders are stored.
return true;
}
/**
* Check if the library has a presave.js in the root folder
*
* @param string $libraryname
* @param string $developmentpath
* @return bool
*/
public function hasPresave($libraryname, $developmentpath = null) {
return false;
}
/**
* Check if upgrades script exist for library.
*
* @param string $machinename
* @param int $majorversion
* @param int $minorversion
* @return string Relative path
*/
public function getUpgradeScript($machinename, $majorversion, $minorversion) {
$path = '/' . "{$machinename}-{$majorversion}.{$minorversion}" . '/';
$file = 'upgrade.js';
$itemid = $this->get_itemid_for_file(self::LIBRARY_FILEAREA, $path, $file);
if ($this->fs->get_file($this->context->id, self::COMPONENT, self::LIBRARY_FILEAREA, $itemid, $path, $file)) {
return '/' . self::LIBRARY_FILEAREA . $path. $file;
} else {
return null;
}
}
/**
* Store the given stream into the given file.
*
* @param string $path
* @param string $file
* @param resource $stream
* @return bool|int
*/
public function saveFileFromZip($path, $file, $stream) {
$fullpath = $path . '/' . $file;
check_dir_exists(pathinfo($fullpath, PATHINFO_DIRNAME));
return file_put_contents($fullpath, $stream);
}
/**
* Deletes a library from the file system.
*
* @param array $library Library details
*/
public function delete_library(array $library): void {
global $DB;
// A library ID of false would result in all library files being deleted, which we don't want. Return instead.
if ($library['libraryId'] === false) {
return;
}
$areafiles = $this->fs->get_area_files($this->context->id, self::COMPONENT, self::LIBRARY_FILEAREA, $library['libraryId']);
$this->delete_directory($this->context->id, self::COMPONENT, self::LIBRARY_FILEAREA, $library['libraryId']);
$librarycache = \cache::make('core', 'h5p_library_files');
foreach ($areafiles as $file) {
if (!$DB->record_exists('files', array('contenthash' => $file->get_contenthash(),
'component' => self::COMPONENT,
'filearea' => self::LIBRARY_FILEAREA))) {
$librarycache->delete($file->get_contenthash());
}
}
}
/**
* Remove an H5P directory from the filesystem.
*
* @param int $contextid context ID
* @param string $component component
* @param string $filearea file area or all areas in context if not specified
* @param int $itemid item ID or all files if not specified
*/
private function delete_directory(int $contextid, string $component, string $filearea, int $itemid): void {
$this->fs->delete_area_files($contextid, $component, $filearea, $itemid);
}
/**
* Copy an H5P directory from the temporary directory into the file system.
*
* @param string $source Temporary location for files.
* @param array $options File system information.
*/
private function copy_directory(string $source, array $options): void {
$librarycache = \cache::make('core', 'h5p_library_files');
$it = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST);
$root = $options['filepath'];
$it->rewind();
while ($it->valid()) {
$item = $it->current();
$subpath = $it->getSubPath();
if (!$item->isDir()) {
$options['filename'] = $it->getFilename();
if (!$subpath == '') {
$options['filepath'] = $root . $subpath . '/';
} else {
$options['filepath'] = $root;
}
$file = $this->fs->create_file_from_pathname($options, $item->getPathName());
if ($options['filearea'] == self::LIBRARY_FILEAREA) {
if (!$librarycache->has($file->get_contenthash())) {
$librarycache->set($file->get_contenthash(), file_get_contents($item->getPathName()));
}
}
}
$it->next();
}
}
/**
* Copies files from storage to temporary folder.
*
* @param string $target Path to temporary folder
* @param int $contextid context where the files are found
* @param string $filearea file area
* @param string $filepath file path
* @param int $itemid Optional item ID
*/
private function export_file_tree(string $target, int $contextid, string $filearea, string $filepath, int $itemid = 0): void {
// Make sure target folder exists.
check_dir_exists($target);
// Read source files.
$files = $this->fs->get_directory_files($contextid, self::COMPONENT, $filearea, $itemid, $filepath, true);
$librarycache = \cache::make('core', 'h5p_library_files');
foreach ($files as $file) {
$path = $target . str_replace($filepath, DIRECTORY_SEPARATOR, $file->get_filepath());
if ($file->is_directory()) {
check_dir_exists(rtrim($path));
} else {
if ($filearea == self::LIBRARY_FILEAREA) {
$cachedfile = $librarycache->get($file->get_contenthash());
if (empty($cachedfile)) {
$file->copy_content_to($path . $file->get_filename());
$librarycache->set($file->get_contenthash(), file_get_contents($path . $file->get_filename()));
} else {
file_put_contents($path . $file->get_filename(), $cachedfile);
}
} else {
$file->copy_content_to($path . $file->get_filename());
}
}
}
}
/**
* Adds all files of a type into one file.
*
* @param array $assets A list of files.
* @param string $type The type of files in assets. Either 'scripts' or 'styles'
* @param \context $context Context
* @return string All of the file content in one string.
*/
private function concatenate_files(array $assets, string $type, \context $context): string {
$content = '';
foreach ($assets as $asset) {
// Find location of asset.
list(
'filearea' => $filearea,
'filepath' => $filepath,
'filename' => $filename,
'itemid' => $itemid
) = $this->get_file_elements_from_filepath($asset->path);
if ($itemid === false) {
continue;
}
// Locate file.
$file = $this->fs->get_file($context->id, self::COMPONENT, $filearea, $itemid, $filepath, $filename);
// Get file content and concatenate.
if ($type === 'scripts') {
$content .= $file->get_content() . ";\n";
} else {
// Rewrite relative URLs used inside stylesheets.
$content .= preg_replace_callback(
'/url\([\'"]?([^"\')]+)[\'"]?\)/i',
function ($matches) use ($filearea, $filepath, $itemid) {
if (preg_match("/^(data:|([a-z0-9]+:)?\/)/i", $matches[1]) === 1) {
return $matches[0]; // Not relative, skip.
}
// Find "../" in matches[1].
// If it exists, we have to remove "../".
// And switch the last folder in the filepath for the first folder in $matches[1].
// For instance:
// $filepath: /H5P.Question-1.4/styles/
// $matches[1]: ../images/plus-one.svg
// We want to avoid this: H5P.Question-1.4/styles/ITEMID/../images/minus-one.svg
// We want this: H5P.Question-1.4/images/ITEMID/minus-one.svg.
if (preg_match('/\.\.\//', $matches[1], $pathmatches)) {
$path = preg_split('/\//', $filepath, -1, PREG_SPLIT_NO_EMPTY);
$pathfilename = preg_split('/\//', $matches[1], -1, PREG_SPLIT_NO_EMPTY);
// Remove the first element: ../.
array_shift($pathfilename);
// Replace pathfilename into the filepath.
$path[count($path) - 1] = $pathfilename[0];
$filepath = '/' . implode('/', $path) . '/';
// Remove the element used to replace.
array_shift($pathfilename);
$matches[1] = implode('/', $pathfilename);
}
return 'url("../' . $filearea . $filepath . $itemid . '/' . $matches[1] . '")';
},
$file->get_content()) . "\n";
}
}
return $content;
}
/**
* Get files ready for export.
*
* @param string $filename File name to retrieve.
* @return bool|\stored_file Stored file instance if exists, false if not
*/
public function get_export_file(string $filename) {
return $this->fs->get_file($this->context->id, self::COMPONENT, self::EXPORT_FILEAREA, 0, '/', $filename);
}
/**
* Converts a relative system file path into Moodle File API elements.
*
* @param string $filepath The system filepath to get information from.
* @return array File information.
*/
private function get_file_elements_from_filepath(string $filepath): array {
$sections = explode('/', $filepath);
// Get the filename.
$filename = array_pop($sections);
// Discard first element.
if (empty($sections[0])) {
array_shift($sections);
}
// Get the filearea.
$filearea = array_shift($sections);
$itemid = array_shift($sections);
// Get the filepath.
$filepath = implode('/', $sections);
$filepath = '/' . $filepath . '/';
return ['filearea' => $filearea, 'filepath' => $filepath, 'filename' => $filename, 'itemid' => $itemid];
}
/**
* Returns the item id given the other necessary variables.
*
* @param string $filearea The file area.
* @param string $filepath The file path.
* @param string $filename The file name.
* @return mixed the specified value false if not found.
*/
private function get_itemid_for_file(string $filearea, string $filepath, string $filename) {
global $DB;
return $DB->get_field('files', 'itemid', ['component' => self::COMPONENT, 'filearea' => $filearea, 'filepath' => $filepath,
'filename' => $filename]);
}
/**
* Helper to make it easy to load content files.
*
* @param string $filearea File area where the file is saved.
* @param int $itemid Content instance or content id.
* @param string $file File path and name.
*
* @return stored_file|null
*/
private function get_file(string $filearea, int $itemid, string $file): ?stored_file {
global $USER;
$component = self::COMPONENT;
$context = $this->context->id;
if ($filearea === 'draft') {
$itemid = 0;
$component = 'user';
$usercontext = \context_user::instance($USER->id);
$context = $usercontext->id;
}
$filepath = '/'. dirname($file). '/';
$filename = basename($file);
// Load file.
$existingfile = $this->fs->get_file($context, $component, $filearea, $itemid, $filepath, $filename);
if (!$existingfile) {
return null;
}
return $existingfile;
}
/**
* Move a single file
*
* @param string $sourcefile Path to source file
* @param int $contentid Content id or 0 if the file is in the editor file area
*
* @return void
*/
private function move_file(string $sourcefile, int $contentid): void {
$pathparts = pathinfo($sourcefile);
$filename = $pathparts['basename'];
$filepath = $pathparts['dirname'];
$foldername = basename($filepath);
// Create file record for content.
$record = array(
'contextid' => $this->context->id,
'component' => $contentid > 0 ? self::COMPONENT : 'user',
'filearea' => $contentid > 0 ? self::CONTENT_FILEAREA : 'draft',
'itemid' => $contentid > 0 ? $contentid : 0,
'filepath' => '/' . $foldername . '/',
'filename' => $filename
);
$file = $this->fs->get_file(
$record['contextid'], $record['component'],
$record['filearea'], $record['itemid'], $record['filepath'],
$record['filename']
);
if ($file) {
// Delete it to make sure that it is replaced with correct content.
$file->delete();
}
$this->fs->create_file_from_pathname($record, $sourcefile);
}
}