Skip to content

Commit 3d84586

Browse files
committed
Remove 'duplicateFile'; remove S3 key prefixing depending on 'zip' field; remove 'getFileByHash' remove unnecessary backticks; remove any 'storageFiles' filtering on 'zip' or 'filename' fields
1 parent 58c089d commit 3d84586

File tree

1 file changed

+14
-100
lines changed

1 file changed

+14
-100
lines changed

model/Storage.inc.php

+14-100
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,12 @@ public static function getDownloadURL(Zotero_Item $item, $ttl=60) {
8787
// http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-s3-2006-03-01.html#headobject,
8888
// but returning NotFound
8989
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
9191
try {
92-
$key = self::getPathPrefix($info['hash'], $info['zip']);
9392
$result = $s3Client->listObjects([
9493
'Bucket' => Z_CONFIG::$S3_BUCKET,
9594
'MaxKeys' => 1,
96-
'Prefix' => $key,
95+
'Prefix' => $info['hash'],
9796
]);
9897

9998
$contents = $result->get('Contents');
@@ -139,13 +138,12 @@ public static function downloadFile(array $localFileItemInfo, $savePath, $filena
139138
}
140139
catch (\Aws\S3\Exception\S3Exception $e) {
141140
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
143142
try {
144-
$key = self::getPathPrefix($localFileItemInfo['hash'], $localFileItemInfo['zip']);
145143
$result = $s3Client->listObjects([
146144
'Bucket' => Z_CONFIG::$S3_BUCKET,
147145
'MaxKeys' => 1,
148-
'Prefix' => $key,
146+
'Prefix' => $localFileItemInfo['hash'],
149147
]);
150148

151149
$contents = $result->get('Contents');
@@ -188,7 +186,7 @@ public static function logDownload($item, $downloadUserID, $ipAddress) {
188186
$size = $info['size'];
189187

190188
$sql = "INSERT INTO storageDownloadLog
191-
(ownerUserID, downloadUserID, ipAddress, storageFileID, `size`)
189+
(ownerUserID, downloadUserID, ipAddress, storageFileID, size)
192190
VALUES (?, ?, INET_ATON(?), ?, ?)";
193191
Zotero_DB::query(
194192
$sql,
@@ -292,7 +290,7 @@ public static function logUpload($uploadUserID, $item, $key, $ipAddress) {
292290
Zotero_DB::query($sql, $key);
293291

294292
$sql = "INSERT INTO storageUploadLog
295-
(ownerUserID, uploadUserID, ipAddress, storageFileID, `size`)
293+
(ownerUserID, uploadUserID, ipAddress, storageFileID, size)
296294
VALUES (?, ?, INET_ATON(?), ?, ?)";
297295
Zotero_DB::query($sql, array($ownerUserID, $uploadUserID, $ipAddress, $storageFileID, $size));
298296
}
@@ -410,92 +408,14 @@ public static function patchFile($item, $info, $algorithm, $patch) {
410408
return $storageFileID;
411409
}
412410

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-
491411
public static function getFileInfoByID($storageFileID) {
492412
$sql = "SELECT * FROM storageFiles WHERE storageFileID=?";
493413
return Zotero_DB::rowQuery($sql, $storageFileID);
494414
}
495415

496416
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));
499419
}
500420

501421
public static function getRemoteFileInfo(Zotero_StorageFileInfo $info) {
@@ -509,13 +429,12 @@ public static function getRemoteFileInfo(Zotero_StorageFileInfo $info) {
509429
}
510430
catch (\Aws\S3\Exception\S3Exception $e) {
511431
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
513433
try {
514-
$key = self::getPathPrefix($info->hash, $info->zip);
515434
$result = $s3Client->listObjects([
516435
'Bucket' => Z_CONFIG::$S3_BUCKET,
517436
'MaxKeys' => 1,
518-
'Prefix' => $key,
437+
'Prefix' => $info->hash,
519438
]);
520439

521440
$contents = $result->get('Contents');
@@ -562,12 +481,12 @@ public static function getLocalFileItemInfo($item) {
562481
/**
563482
* Get items associated with a unique file on S3
564483
*/
565-
public static function getFileItems($hash, $filename, $zip) {
484+
public static function getFileItems($hash) {
566485
throw new Exception("Unimplemented"); // would need to work across shards
567486

568487
$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));
571490
if (!$itemIDs) {
572491
return array();
573492
}
@@ -576,7 +495,7 @@ public static function getFileItems($hash, $filename, $zip) {
576495

577496

578497
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 (?,?,?)";
580499
return Zotero_DB::query($sql, array($info->hash, $info->size, (int) $info->zip));
581500
}
582501

@@ -677,11 +596,6 @@ public static function deleteFileLibraryReference($storageFileID, $libraryID) {
677596
}
678597

679598

680-
public static function getPathPrefix($hash, $zip=false) {
681-
return "$hash/" . ($zip ? "c/" : '');
682-
}
683-
684-
685599
public static function getUploadPOSTData($item, Zotero_StorageFileInfo $info) {
686600
$params = self::generateUploadPOSTParams($item, $info);
687601
$boundary = "---------------------------" . md5(uniqid());

0 commit comments

Comments
 (0)