| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
const License = require( './mmv.model.License.js' ); |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class ImageModel { |
| 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 |
constructor( |
| 52 |
title, |
| 53 |
name, |
| 54 |
size, |
| 55 |
width, |
| 56 |
height, |
| 57 |
mimeType, |
| 58 |
url, |
| 59 |
descriptionUrl, |
| 60 |
descriptionShortUrl, |
| 61 |
pageID, |
| 62 |
repo, |
| 63 |
uploadDateTime, |
| 64 |
anonymizedUploadDateTime, |
| 65 |
creationDateTime, |
| 66 |
description, |
| 67 |
source, |
| 68 |
author, |
| 69 |
authorCount, |
| 70 |
license, |
| 71 |
permission, |
| 72 |
attribution, |
| 73 |
deletionReason, |
| 74 |
latitude, |
| 75 |
longitude, |
| 76 |
restrictions |
| 77 |
) { |
| 78 |
|
| 79 |
this.title = title; |
| 80 |
|
| 81 |
|
| 82 |
this.name = name; |
| 83 |
|
| 84 |
|
| 85 |
this.size = size; |
| 86 |
|
| 87 |
|
| 88 |
this.width = width; |
| 89 |
|
| 90 |
|
| 91 |
this.height = height; |
| 92 |
|
| 93 |
|
| 94 |
this.mimeType = mimeType; |
| 95 |
|
| 96 |
|
| 97 |
this.url = url; |
| 98 |
|
| 99 |
|
| 100 |
this.descriptionUrl = descriptionUrl; |
| 101 |
|
| 102 |
|
| 103 |
this.descriptionShortUrl = descriptionShortUrl; |
| 104 |
|
| 105 |
|
| 106 |
this.pageID = pageID; |
| 107 |
|
| 108 |
|
| 109 |
this.repo = repo; |
| 110 |
|
| 111 |
|
| 112 |
this.uploadDateTime = uploadDateTime; |
| 113 |
|
| 114 |
|
| 115 |
this.anonymizedUploadDateTime = anonymizedUploadDateTime; |
| 116 |
|
| 117 |
|
| 118 |
this.creationDateTime = creationDateTime; |
| 119 |
|
| 120 |
|
| 121 |
this.description = description; |
| 122 |
|
| 123 |
|
| 124 |
this.source = source; |
| 125 |
|
| 126 |
|
| 127 |
this.author = author; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
this.authorCount = authorCount; |
| 134 |
|
| 135 |
|
| 136 |
this.license = license; |
| 137 |
|
| 138 |
|
| 139 |
this.permission = permission; |
| 140 |
|
| 141 |
|
| 142 |
this.attribution = attribution; |
| 143 |
|
| 144 |
|
| 145 |
this.deletionReason = deletionReason; |
| 146 |
|
| 147 |
|
| 148 |
this.latitude = latitude; |
| 149 |
|
| 150 |
|
| 151 |
this.longitude = longitude; |
| 152 |
|
| 153 |
|
| 154 |
this.restrictions = restrictions; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
this.thumbUrls = {}; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
static newFromImageInfo( title, imageInfo ) { |
| 175 |
let name; |
| 176 |
let uploadDateTime; |
| 177 |
let anonymizedUploadDateTime; |
| 178 |
let creationDateTime; |
| 179 |
let description; |
| 180 |
let source; |
| 181 |
let author; |
| 182 |
let authorCount; |
| 183 |
let license; |
| 184 |
let permission; |
| 185 |
let attribution; |
| 186 |
let deletionReason; |
| 187 |
let latitude; |
| 188 |
let longitude; |
| 189 |
let restrictions; |
| 190 |
const innerInfo = imageInfo.imageinfo[ 0 ]; |
| 191 |
const extmeta = innerInfo.extmetadata; |
| 192 |
|
| 193 |
if ( extmeta ) { |
| 194 |
creationDateTime = this.parseExtmeta( extmeta.DateTimeOriginal, 'datetime' ); |
| 195 |
uploadDateTime = this.parseExtmeta( extmeta.DateTime, 'datetime' ); |
| 196 |
|
| 197 |
|
| 198 |
anonymizedUploadDateTime = uploadDateTime.replace( /[^\d]/g, '' ); |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
anonymizedUploadDateTime = `${ anonymizedUploadDateTime.slice( 0, anonymizedUploadDateTime.length - 6 ) }000000`; |
| 203 |
|
| 204 |
name = this.parseExtmeta( extmeta.ObjectName, 'plaintext' ); |
| 205 |
|
| 206 |
description = this.parseExtmeta( extmeta.ImageDescription, 'string' ); |
| 207 |
source = this.parseExtmeta( extmeta.Credit, 'string' ); |
| 208 |
author = this.parseExtmeta( extmeta.Artist, 'string' ); |
| 209 |
authorCount = this.parseExtmeta( extmeta.AuthorCount, 'integer' ); |
| 210 |
|
| 211 |
license = this.newLicenseFromImageInfo( extmeta ); |
| 212 |
permission = this.parseExtmeta( extmeta.Permission, 'string' ); |
| 213 |
attribution = this.parseExtmeta( extmeta.Attribution, 'string' ); |
| 214 |
deletionReason = this.parseExtmeta( extmeta.DeletionReason, 'string' ); |
| 215 |
restrictions = this.parseExtmeta( extmeta.Restrictions, 'list' ); |
| 216 |
|
| 217 |
latitude = this.parseExtmeta( extmeta.GPSLatitude, 'float' ); |
| 218 |
longitude = this.parseExtmeta( extmeta.GPSLongitude, 'float' ); |
| 219 |
} |
| 220 |
|
| 221 |
if ( !name ) { |
| 222 |
name = title.getNameText(); |
| 223 |
} |
| 224 |
|
| 225 |
const imageData = new ImageModel( |
| 226 |
title, |
| 227 |
name, |
| 228 |
innerInfo.size, |
| 229 |
innerInfo.width, |
| 230 |
innerInfo.height, |
| 231 |
innerInfo.mime, |
| 232 |
innerInfo.url, |
| 233 |
innerInfo.descriptionurl, |
| 234 |
innerInfo.descriptionshorturl, |
| 235 |
imageInfo.pageid, |
| 236 |
imageInfo.imagerepository, |
| 237 |
uploadDateTime, |
| 238 |
anonymizedUploadDateTime, |
| 239 |
creationDateTime, |
| 240 |
description, |
| 241 |
source, |
| 242 |
author, |
| 243 |
authorCount, |
| 244 |
license, |
| 245 |
permission, |
| 246 |
attribution, |
| 247 |
deletionReason, |
| 248 |
latitude, |
| 249 |
longitude, |
| 250 |
restrictions |
| 251 |
); |
| 252 |
|
| 253 |
if ( innerInfo.thumburl ) { |
| 254 |
imageData.addThumbUrl( |
| 255 |
innerInfo.thumbwidth, |
| 256 |
innerInfo.thumburl |
| 257 |
); |
| 258 |
} |
| 259 |
|
| 260 |
return imageData; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
static newLicenseFromImageInfo( extmeta ) { |
| 272 |
let license; |
| 273 |
|
| 274 |
if ( extmeta.LicenseShortName ) { |
| 275 |
license = new License( |
| 276 |
this.parseExtmeta( extmeta.LicenseShortName, 'string' ), |
| 277 |
this.parseExtmeta( extmeta.License, 'string' ), |
| 278 |
this.parseExtmeta( extmeta.UsageTerms, 'string' ), |
| 279 |
this.parseExtmeta( extmeta.LicenseUrl, 'string' ), |
| 280 |
this.parseExtmeta( extmeta.AttributionRequired, 'boolean' ), |
| 281 |
this.parseExtmeta( extmeta.NonFree, 'boolean' ) |
| 282 |
); |
| 283 |
} |
| 284 |
|
| 285 |
return license; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
static parseExtmeta( data, type ) { |
| 296 |
let value = data && data.value; |
| 297 |
if ( value === null || value === undefined ) { |
| 298 |
return undefined; |
| 299 |
} else if ( type === 'plaintext' ) { |
| 300 |
return value.toString().replace( /<.*?>/g, '' ); |
| 301 |
} else if ( type === 'string' ) { |
| 302 |
return value.toString(); |
| 303 |
} else if ( type === 'integer' ) { |
| 304 |
return parseInt( value, 10 ); |
| 305 |
} else if ( type === 'float' ) { |
| 306 |
return parseFloat( value ); |
| 307 |
} else if ( type === 'boolean' ) { |
| 308 |
value = value.toString().toLowerCase().replace( /^\s+|\s+$/g, '' ); |
| 309 |
if ( value in { 1: null, yes: null, true: null } ) { |
| 310 |
return true; |
| 311 |
} else if ( value in { 0: null, no: null, false: null } ) { |
| 312 |
return false; |
| 313 |
} else { |
| 314 |
return undefined; |
| 315 |
} |
| 316 |
} else if ( type === 'datetime' ) { |
| 317 |
value = value.toString(); |
| 318 |
|
| 319 |
|
| 320 |
const rfc3339 = /\d{4}-[01]\d-[0-3]\d(T[0-2]\d:[0-5]\d:[0-5]\d(\.\d+)?Z?)?/; |
| 321 |
const match = value.match( rfc3339 ); |
| 322 |
if ( !match ) { |
| 323 |
return value.replace( /<.*?>/g, '' ); |
| 324 |
} |
| 325 |
value = match[ 0 ]; |
| 326 |
if ( value.match( /^\d{4}-00-00/ ) ) { |
| 327 |
|
| 328 |
return value.slice( 0, 4 ); |
| 329 |
} |
| 330 |
return value; |
| 331 |
} else if ( type === 'list' ) { |
| 332 |
return value === '' ? [] : value.split( '|' ); |
| 333 |
} else { |
| 334 |
throw new Error( 'Image.parseExtmeta: unknown type' ); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
addThumbUrl( width, url ) { |
| 345 |
this.thumbUrls[ width ] = url; |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
getThumbUrl( width ) { |
| 355 |
return this.thumbUrls[ width ]; |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
hasCoords() { |
| 364 |
return this.latitude !== undefined && this.latitude !== null && |
| 365 |
this.longitude !== undefined && this.longitude !== null; |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
module.exports = ImageModel; |