-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
127 lines (112 loc) · 4.24 KB
/
lib.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
<?php
require_once($CFG->dirroot . '/repository/lib.php');
/**
* Repository plugin for handling facial uploads.
*
* @package auth_faceauth
*/
class repository_upload_face extends repository {
private $mimetypes = array();
/**
* Print the login form.
*
* @return array
*/
public function print_login() {
return $this->get_listing();
}
/**
* Process uploaded facial data.
*
* @param string $saveas_filename File name to save as.
* @param int $maxbytes Maximum allowed file size.
* @return array|bool
*/
public function upload($saveas_filename, $maxbytes) {
global $CFG;
$types = optional_param('accepted_types', '*', PARAM_RAW);
$savepath = optional_param('savepath', '/', PARAM_PATH);
$itemid = optional_param('itemid', 0, PARAM_INT);
$license = optional_param('license', $CFG->sitedefaultlicense, PARAM_TEXT);
$author = optional_param('author', '', PARAM_TEXT);
$areamaxbytes = optional_param('areamaxbytes', FILE_AREA_MAX_BYTES_UNLIMITED, PARAM_INT);
$overwriteexisting = optional_param('overwrite', false, PARAM_BOOL);
return $this->process_upload_face($saveas_filename, $maxbytes, $types, $savepath, $itemid, $license, $author, $overwriteexisting, $areamaxbytes);
}
/**
* Handles the actual file upload process.
*
* @param string $saveas_filename File name to save as.
* @param int $maxbytes Maximum allowed file size.
* @param mixed $types Accepted file types.
* @param string $savepath Path to save the file to.
* @param int $itemid ID for the file item.
* @param string $license License for the file.
* @param string $author Author of the file.
* @param bool $overwriteexisting Overwrite flag.
* @param int $areamaxbytes Maximum size for the file area.
* @return array Information about the uploaded file.
*/
public function process_upload_face($saveas_filename, $maxbytes, $types = '*', $savepath = '/', $itemid = 0,
$license = null, $author = '', $overwriteexisting = false, $areamaxbytes = FILE_AREA_MAX_BYTES_UNLIMITED) {
global $USER, $CFG;
$context = context_user::instance($USER->id); // Updated to use context_user.
$fs = get_file_storage();
if (empty($types) || $types == '*') {
$this->mimetypes = '*';
} else {
foreach ((array) $types as $type) {
$this->mimetypes[] = mimeinfo('type', $type);
}
}
$record = (object) [
'filearea' => 'draft',
'component' => 'user',
'filepath' => file_correct_filepath($savepath),
'itemid' => $itemid,
'license' => $license ?? $CFG->sitedefaultlicense,
'author' => $author,
'filename' => $saveas_filename . '.jpg',
'contextid' => $context->id,
'userid' => $USER->id,
];
// Simulate file creation for testing.
$testfile = "/var/moodledata20/{$saveas_filename}.jpg";
if (!file_exists($testfile)) {
return false; // File does not exist; abort.
}
$stored_file = $fs->create_file_from_pathname($record, $testfile);
unlink($testfile); // Clean up temporary file.
return [
'url' => moodle_url::make_draftfile_url($record->itemid, $record->filepath, $record->filename)->out(false),
'id' => $record->itemid,
'file' => $record->filename,
];
}
/**
* Get a listing for the repository.
*
* @param string $path Current path.
* @param string $page Current page.
* @return array Repository listing.
*/
public function get_listing($path = '', $page = '') {
return [
'nologin' => true,
'nosearch' => true,
'norefresh' => true,
'list' => [],
'dynload' => false,
'upload' => ['label' => get_string('attachment', 'repository'), 'id' => 'repo-form-face'],
'allowcaching' => true,
];
}
/**
* Supported return types.
*
* @return int
*/
public function supported_returntypes() {
return FILE_INTERNAL;
}
}