@@ -87,13 +87,12 @@ public static function getDownloadURL(Zotero_Item $item, $ttl=60) {
87
87
// http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-s3-2006-03-01.html#headobject,
88
88
// but returning NotFound
89
89
if ($ e ->getAwsErrorCode () == 'NoSuchKey ' || $ e ->getAwsErrorCode () == 'NotFound ' ) {
90
- // Try legacy key format, with zip flag
90
+ // Try to find a legacy file: hash/filename or hash/c/filename
91
91
try {
92
- $ key = self ::getPathPrefix ($ info ['hash ' ], $ info ['zip ' ]);
93
92
$ result = $ s3Client ->listObjects ([
94
93
'Bucket ' => Z_CONFIG ::$ S3_BUCKET ,
95
94
'MaxKeys ' => 1 ,
96
- 'Prefix ' => $ key ,
95
+ 'Prefix ' => $ info [ ' hash ' ] ,
97
96
]);
98
97
99
98
$ contents = $ result ->get ('Contents ' );
@@ -139,13 +138,12 @@ public static function downloadFile(array $localFileItemInfo, $savePath, $filena
139
138
}
140
139
catch (\Aws \S3 \Exception \S3Exception $ e ) {
141
140
if ($ e ->getAwsErrorCode () == 'NoSuchKey ' ) {
142
- // Try legacy key format, with zip flag and filename
141
+ // Try to find a legacy file: hash/filename or hash/c/ filename
143
142
try {
144
- $ key = self ::getPathPrefix ($ localFileItemInfo ['hash ' ], $ localFileItemInfo ['zip ' ]);
145
143
$ result = $ s3Client ->listObjects ([
146
144
'Bucket ' => Z_CONFIG ::$ S3_BUCKET ,
147
145
'MaxKeys ' => 1 ,
148
- 'Prefix ' => $ key ,
146
+ 'Prefix ' => $ localFileItemInfo [ ' hash ' ] ,
149
147
]);
150
148
151
149
$ contents = $ result ->get ('Contents ' );
@@ -188,7 +186,7 @@ public static function logDownload($item, $downloadUserID, $ipAddress) {
188
186
$ size = $ info ['size ' ];
189
187
190
188
$ sql = "INSERT INTO storageDownloadLog
191
- (ownerUserID, downloadUserID, ipAddress, storageFileID, ` size` )
189
+ (ownerUserID, downloadUserID, ipAddress, storageFileID, size)
192
190
VALUES (?, ?, INET_ATON(?), ?, ?) " ;
193
191
Zotero_DB::query (
194
192
$ sql ,
@@ -292,7 +290,7 @@ public static function logUpload($uploadUserID, $item, $key, $ipAddress) {
292
290
Zotero_DB::query ($ sql , $ key );
293
291
294
292
$ sql = "INSERT INTO storageUploadLog
295
- (ownerUserID, uploadUserID, ipAddress, storageFileID, ` size` )
293
+ (ownerUserID, uploadUserID, ipAddress, storageFileID, size)
296
294
VALUES (?, ?, INET_ATON(?), ?, ?) " ;
297
295
Zotero_DB::query ($ sql , array ($ ownerUserID , $ uploadUserID , $ ipAddress , $ storageFileID , $ size ));
298
296
}
@@ -410,92 +408,14 @@ public static function patchFile($item, $info, $algorithm, $patch) {
410
408
return $ storageFileID ;
411
409
}
412
410
413
- public static function duplicateFile ($ storageFileID , $ newName , $ zip , $ contentType =null ) {
414
- if (strlen ($ newName ) == 0 ) {
415
- throw new Exception ("New name not provided " );
416
- }
417
-
418
- $ localInfo = self ::getFileInfoByID ($ storageFileID );
419
- if (!$ localInfo ) {
420
- throw new Exception ("File $ storageFileID not found " );
421
- }
422
-
423
- $ s3Client = Z_Core::$ AWS ->createS3 ();
424
- try {
425
- $ s3Client ->headObject ([
426
- 'Bucket ' => Z_CONFIG ::$ S3_BUCKET ,
427
- 'Key ' => $ localInfo ['hash ' ]
428
- ]);
429
- }
430
- // If file doesn't already exist named with just hash, copy it over
431
- catch (\Aws \S3 \Exception \S3Exception $ e ) {
432
- if ($ e ->getAwsErrorCode () == 'NoSuchKey ' || $ e ->getAwsErrorCode () == 'NotFound ' ) {
433
- try {
434
- $ key = self ::getPathPrefix ($ localInfo ['hash ' ], $ localInfo ['zip ' ]);
435
- $ result = $ s3Client ->listObjects ([
436
- 'Bucket ' => Z_CONFIG ::$ S3_BUCKET ,
437
- 'MaxKeys ' => 1 ,
438
- 'Prefix ' => $ key ,
439
- ]);
440
-
441
- $ contents = $ result ->get ('Contents ' );
442
- if (!$ contents || count ($ contents ) < 1 ) {
443
- return false ;
444
- }
445
-
446
- $ key = $ contents [0 ]['Key ' ];
447
-
448
- try {
449
- $ s3Client ->copyObject ([
450
- 'Bucket ' => Z_CONFIG ::$ S3_BUCKET ,
451
- 'CopySource ' => Z_CONFIG ::$ S3_BUCKET . '/ '
452
- . urlencode ($ key ),
453
- 'Key ' => $ localInfo ['hash ' ],
454
- 'ACL ' => 'private '
455
- ]);
456
- }
457
- catch (\Aws \S3 \Exception \S3Exception $ e ) {
458
- if ($ e ->getAwsErrorCode () == 'NoSuchKey ' ) {
459
- return false ;
460
- }
461
- else {
462
- throw $ e ;
463
- }
464
- }
465
- }
466
- catch (\Aws \S3 \Exception \S3Exception $ e ) {
467
- throw $ e ;
468
- }
469
- }
470
- else {
471
- throw $ e ;
472
- }
473
- }
474
-
475
- $ info = new Zotero_StorageFileInfo ;
476
- foreach ($ localInfo as $ key => $ val ) {
477
- $ info ->$ key = $ val ;
478
- }
479
-
480
- return self ::addFile ($ info );
481
- }
482
-
483
- // TODO: Remove
484
- // Was previously used in ItemController to find a file with a different name,
485
- // if not found with the given filename. Now getLocalFileInfo does the same.
486
- public static function getFileByHash ($ hash , $ zip ) {
487
- $ sql = "SELECT storageFileID FROM storageFiles WHERE hash=? AND zip=? LIMIT 1 " ;
488
- return Zotero_DB::valueQuery ($ sql , array ($ hash , (int ) $ zip ));
489
- }
490
-
491
411
public static function getFileInfoByID ($ storageFileID ) {
492
412
$ sql = "SELECT * FROM storageFiles WHERE storageFileID=? " ;
493
413
return Zotero_DB::rowQuery ($ sql , $ storageFileID );
494
414
}
495
415
496
416
public static function getLocalFileInfo (Zotero_StorageFileInfo $ info ) {
497
- $ sql = "SELECT * FROM storageFiles WHERE hash=? AND zip=? ORDER BY storageFileID LIMIT 1 " ;
498
- return Zotero_DB::rowQuery ($ sql , array ($ info ->hash , ( int ) $ info -> zip ));
417
+ $ sql = "SELECT * FROM storageFiles WHERE hash=? ORDER BY storageFileID LIMIT 1 " ;
418
+ return Zotero_DB::rowQuery ($ sql , array ($ info ->hash ));
499
419
}
500
420
501
421
public static function getRemoteFileInfo (Zotero_StorageFileInfo $ info ) {
@@ -509,13 +429,12 @@ public static function getRemoteFileInfo(Zotero_StorageFileInfo $info) {
509
429
}
510
430
catch (\Aws \S3 \Exception \S3Exception $ e ) {
511
431
if ($ e ->getAwsErrorCode () == 'NoSuchKey ' || $ e ->getAwsErrorCode () == 'NotFound ' ) {
512
- // Try legacy key format, with zip flag
432
+ // Try to find a legacy file: hash/filename or hash/c/filename
513
433
try {
514
- $ key = self ::getPathPrefix ($ info ->hash , $ info ->zip );
515
434
$ result = $ s3Client ->listObjects ([
516
435
'Bucket ' => Z_CONFIG ::$ S3_BUCKET ,
517
436
'MaxKeys ' => 1 ,
518
- 'Prefix ' => $ key ,
437
+ 'Prefix ' => $ info -> hash ,
519
438
]);
520
439
521
440
$ contents = $ result ->get ('Contents ' );
@@ -562,12 +481,12 @@ public static function getLocalFileItemInfo($item) {
562
481
/**
563
482
* Get items associated with a unique file on S3
564
483
*/
565
- public static function getFileItems ($ hash, $ filename , $ zip ) {
484
+ public static function getFileItems ($ hash ) {
566
485
throw new Exception ("Unimplemented " ); // would need to work across shards
567
486
568
487
$ sql = "SELECT itemID FROM storageFiles JOIN storageFileItems USING (storageFileID)
569
- WHERE hash=? AND filename=? AND zip=? " ;
570
- $ itemIDs = Zotero_DB::columnQuery ($ sql , array ($ hash, $ filename , ( int ) $ zip ));
488
+ WHERE hash=? " ;
489
+ $ itemIDs = Zotero_DB::columnQuery ($ sql , array ($ hash ));
571
490
if (!$ itemIDs ) {
572
491
return array ();
573
492
}
@@ -576,7 +495,7 @@ public static function getFileItems($hash, $filename, $zip) {
576
495
577
496
578
497
public static function addFile (Zotero_StorageFileInfo $ info ) {
579
- $ sql = "INSERT INTO storageFiles (hash, ` size` , zip) VALUES (?,?,?) " ;
498
+ $ sql = "INSERT INTO storageFiles (hash, size, zip) VALUES (?,?,?) " ;
580
499
return Zotero_DB::query ($ sql , array ($ info ->hash , $ info ->size , (int ) $ info ->zip ));
581
500
}
582
501
@@ -677,11 +596,6 @@ public static function deleteFileLibraryReference($storageFileID, $libraryID) {
677
596
}
678
597
679
598
680
- public static function getPathPrefix ($ hash , $ zip =false ) {
681
- return "$ hash/ " . ($ zip ? "c/ " : '' );
682
- }
683
-
684
-
685
599
public static function getUploadPOSTData ($ item , Zotero_StorageFileInfo $ info ) {
686
600
$ params = self ::generateUploadPOSTParams ($ item , $ info );
687
601
$ boundary = "--------------------------- " . md5 (uniqid ());
0 commit comments