-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocialProfileFileBackend.php
178 lines (161 loc) · 5.46 KB
/
SocialProfileFileBackend.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
<?php
/**
* Decides which file backend to use for storing custom images used by
* SocialProfile which are not treated as normal MediaWiki images.
* Such images are:
* -user avatars
* -system gift (award) images
* -user-to-user gift images
*
* @file
* @ingroup Extensions
*/
use MediaWiki\MediaWikiServices;
class SocialProfileFileBackend {
/** @var string The name of the container to use */
private $container;
/**
* @param string $container The name of the container to use.
* System gifts (awards) and user-to-user gifts both use 'awards'; user
* avatars use 'avatars'.
*/
public function __construct( string $container ) {
$this->container = $container;
}
/**
* Get a FileBackend class.
*
* @return FileBackend
*/
public function getFileBackend() {
$services = MediaWikiServices::getInstance();
$mainConfig = $services->getMainConfig();
if ( !empty( $mainConfig->get( 'SocialProfileFileBackend' ) ) ) {
$backend = $services->getFileBackendGroup()->get(
$mainConfig->get( 'SocialProfileFileBackend' )
);
} else {
$backend = new FSFileBackend( [
// We just set the backend name to match the container to
// avoid having to set another variable of the same value
'name' => "{$this->container}-backend",
'wikiId' => WikiMap::getCurrentWikiId(),
'lockManager' => new NullLockManager( [] ),
'containerPaths' => [ $this->container => "{$mainConfig->get( 'UploadDirectory' )}/{$this->container}" ],
'fileMode' => 0777,
'obResetFunc' => 'wfResetOutputBuffers',
'streamMimeFunc' => [ 'StreamFile', 'contentTypeFromPath' ],
'statusWrapper' => [ 'Status', 'wrap' ],
] );
}
if ( !$backend->directoryExists( [ 'dir' => $backend->getContainerStoragePath( $this->container ) ] ) ) {
$backend->prepare( [ 'dir' => $backend->getContainerStoragePath( $this->container ) ] );
}
return $backend;
}
/**
* Get the path to an image (e.g. avatar, award or gift one).
*
* @param string $prefix The prefix to use that goes in front of $id
* @param int $id User ID for avatars; internal identifier (sg_id/ug_id) for awards/gifts
* @param string $size Size of the image to get
* - s for small
* - m for medium
* - ml for medium-large
* - l for large
* @param string $ext File extension (currently can be only png, jpg, jpeg, or gif)
*
* @return string|null Normalized full storage path or null on failure
*/
public function getPath( $prefix, $id, $size, $ext ) {
return $this->getFileBackend()->normalizeStoragePath(
$this->getContainerStoragePath() .
'/' . $this->getFileName( $prefix, $id, $size, $ext )
);
}
/**
* Get the file name of an image.
*
* @param string $prefix The prefix to use that goes in front of $id
* @param int $id User ID for avatars; internal identifier (sg_id/ug_id) for awards/gifts
* @param string $size Size of the image to get
* - s for small
* - m for medium
* - ml for medium-large
* - l for large
* @param string $ext File extension (currently can be only png, jpg, jpeg, or gif)
*
* @return string file name
*/
public function getFileName( $prefix, $id, $size, $ext ) {
return $prefix . (string)$id . '_' . $size . '.' . $ext;
}
/**
* Get the backend container storage path.
*
* @return string Storage path
*/
public function getContainerStoragePath() {
return $this->getFileBackend()->getContainerStoragePath( $this->container );
}
/**
* Get the HTTP URL for a file.
*
* @param string $prefix The prefix to use that goes in front of $id
* @param int $id User ID for avatars; internal identifier (sg_id/ug_id) for awards/gifts
* @param string $size Size of the image to get
* - s for small
* - m for medium
* - ml for medium-large
* - l for large
* @param string $ext File extension (can be only png, jpg, jpeg, or gif)
*
* @return string|null URL or null on failure
*/
public function getFileHttpUrl( $prefix, $id, $size, $ext ) {
return $this->getDefaultUrlPath( $this->getFileName( $prefix, $id, $size, $ext ) );
}
/**
* Get the HTTP URL for a file by full name of the file.
*
* @param string $fileName the file name
* @return string|null URL or null on failure
*/
public function getFileHttpUrlFromName( $fileName ) {
return $this->getDefaultUrlPath( $fileName );
}
/**
* Get the HTTP URL for a file by full name of the file.
* If getFileHttpUrl() returns null, we fallback to this.
*
* @param string $fileName the file name
* @return string URL
*/
public function getDefaultUrlPath( $fileName ) {
$mainConfig = MediaWikiServices::getInstance()->getMainConfig();
$uploadPath = $mainConfig->get( 'UploadBaseUrl' ) ? $mainConfig->get( 'UploadBaseUrl' ) .
$mainConfig->get( 'UploadPath' ) :
$mainConfig->get( 'UploadPath' );
return $uploadPath . '/' . $this->container . '/' . $fileName;
}
/**
* Check if a file exists in the given backend.
*
* @param string $prefix The prefix to use that goes in front of $id
* @param int $id User ID for avatars; internal identifier (sg_id/ug_id) for awards/gifts
* @param string $size Size of the image to get
* - s for small
* - m for medium
* - ml for medium-large
* - l for large
* @param string $ext File extension (can be only png, jpg, jpeg, or gif)
* @return bool|null Whether the file exists or null on failure
*/
public function fileExists( $prefix, $id, $size, $ext ) {
return $this->getFileBackend()->fileExists( [
'src' => $this->getPath(
$prefix, $id, $size, $ext
)
] );
}
}