-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3fs.install
224 lines (205 loc) · 6.49 KB
/
s3fs.install
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
<?php
/**
* @file
* Install, update and uninstall functions for the S3 File System module.
*/
use Drupal\Core\Database\SchemaObjectExistsException;
use Drupal\Core\Site\Settings;
use Drupal\Core\Url;
/**
* Implements hook_requirements().
*/
function s3fs_requirements($phase) {
$requirements = [];
if ($phase == 'install') {
if (!class_exists('\Aws\S3\S3Client')) {
$requirements['aws_library'] = [
'description' => t('S3fs require AWS library.'),
'severity' => REQUIREMENT_ERROR,
];
}
}
if ($phase == 'runtime') {
$config = \Drupal::config('s3fs.settings');
$use_instance_profile = $config->get('use_instance_profile');
$access_key = Settings::get('s3fs.access_key');
$secret_key = Settings::get('s3fs.secret_key');
if (
($config->get('access_key') && !$access_key)
|| ($config->get('secret_key') && !$secret_key)
) {
$requirements['s3fs_keys'] = [
'title' => t('S3 File System access and secret keys'),
'severity' => REQUIREMENT_WARNING,
'description' => t('Access and secret key now must be in settings.php. Please copy your values from <a href=":settings">S3 File System module settings page</a> or $config (in settings.php) and set it up in $settings array in your settings.php before 8.x-3.0-beta1.', [
':settings' => Url::fromRoute('s3fs.admin_settings')->toString(),
]),
];
}
if (
!$use_instance_profile
&& (
(!$access_key && !$secret_key)
|| (!$access_key && $secret_key)
|| ($access_key && !$secret_key)
)
) {
$requirements['s3fs'] = [
'title' => t('S3 File System'),
'severity' => REQUIREMENT_WARNING,
'description' => t('S3 File System has miss configuration. Please set it up at the <a href=":settings">S3 File System module settings page</a> or in your settings.php file.', [
':settings' => Url::fromRoute('s3fs.admin_settings')->toString(),
]),
];
}
if (ini_get('allow_url_fopen')) {
$requirements['s3fs_allow_url_fopen'] = [
'severity' => REQUIREMENT_OK,
'title' => t('allow_url_fopen'),
'value' => t('Enabled'),
];
}
else {
$requirements['s3fs_allow_url_fopen'] = [
'severity' => REQUIREMENT_ERROR,
'title' => t('allow_url_fopen'),
'value' => t('Disabled'),
'description' => t('The S3 File System module requires that the allow_url_fopen setting be turned on in php.ini.'),
];
}
if (PHP_INT_SIZE === 8) {
$requirements['s3fs_int64'] = [
'title' => t('PHP architecture'),
'value' => t('64-bit'),
'severity' => REQUIREMENT_OK,
];
}
else {
$requirements['s3fs_int64'] = [
'title' => t('PHP architecture'),
'value' => t('32-bit'),
'description' => t('A 64-bit PHP installation is required in order to support files larger than 2GB.'),
'severity' => REQUIREMENT_WARNING,
];
}
}
return $requirements;
}
/**
* Implements hook_schema().
*/
function s3fs_schema() {
$schema = [];
$schema['s3fs_file'] = [
'description' => 'Stores metadata about files in S3.',
'fields' => [
'uri' => [
'description' => 'The S3 URI of the file.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
],
'filesize' => [
'description' => 'The size of the file in bytes.',
'type' => 'int',
'size' => 'big',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'timestamp' => [
'description' => 'UNIX timestamp for when the file was added.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
'dir' => [
'description' => 'Boolean indicating whether or not this object is a directory.',
'type' => 'int',
'not null' => TRUE,
'default' => 0,
],
'version' => [
'description' => 'The S3 VersionId of the object.',
'type' => 'varchar',
'length' => 32,
'not null' => FALSE,
'default' => '',
],
],
'indexes' => [
'timestamp' => ['timestamp'],
],
//'primary key' => ['uri'],
// As mentioned on http://drupal.org/node/2193059, a bug in Drupal core's
// MySQL driver prevents this setting from actually being applied. So we
// manually fix that in hook_install().
'collation' => 'utf8_bin',
];
return $schema;
}
/**
* Implements hook_install().
*/
function s3fs_install() {
s3fs_fix_table_indexes();
}
/**
* Fix s3fs file table indexes.
*
* Because hook_schema() doesn't respect the 'collation' setting, we have to
* set the collation manually. This hook is run after the table is created.
*
* Also adds s3:// to the the core file module's list of public schema.
* See https://www.drupal.org/node/2305017 for more info.
*
* @param $table
* Allowed values: s3fs_file | s3fs_file_temp
*/
function s3fs_fix_table_indexes($table = 's3fs_file') {
// We can't use query placeholders for table names in \Drupal::database()->query(),
// so we only allow certain tables.
$allowed_tables = ['s3fs_file', 's3fs_file_temp'];
if (in_array($table, $allowed_tables)) {
$options = \Drupal::database()->getConnectionOptions();
switch ($options['driver']) {
case 'pgsql':
// Postgres uses binary collation by default.
break;
case 'sqlite':
// SQLite uses binary collation by default.
break;
case 'mysql':
// As stated here:
// http://forums.mysql.com/read.php?103,19380,200971#msg-200971
// MySQL doesn't directly support case sensitive UTF8 collation.
// Fortunately, 'utf8_bin' collation works for our purposes.
\Drupal::database()
->query('ALTER TABLE {' . $table . '} CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin');
break;
}
\Drupal::database()->schema()->addPrimaryKey($table, ['uri']);
}
}
/**
* Set new config property "redirect_styles_ttl" to default value.
*/
function s3fs_update_8301() {
\Drupal::configFactory()
->getEditable('s3fs.settings')
->set('redirect_styles_ttl', 0)
->save();
}
/**
* Fix s3fs_file table indexes.
*/
function s3fs_update_8302() {
try {
s3fs_fix_table_indexes();
}
catch (SchemaObjectExistsException $exception) {
\Drupal::messenger()->addStatus('"uri" primary key already exists in s3fs_file table.');
}
}