Overwrite existing filename on reupload.
This commit is contained in:
parent
116202929b
commit
bca1630ea9
4 changed files with 53 additions and 24 deletions
|
@ -121,6 +121,10 @@ class UploadInfo {
|
|||
return $this->bump;
|
||||
}
|
||||
|
||||
public function getBumpAmountForUpdate(): ?int {
|
||||
return $this->expires !== null && $this->bump > 0 ? $this->bump : null;
|
||||
}
|
||||
|
||||
public function getName(): string {
|
||||
return $this->name;
|
||||
}
|
||||
|
|
|
@ -65,8 +65,8 @@ class UploadsContext {
|
|||
return sprintf('%s.t', $this->getFileUrlV1($uploadInfo, $forceApiDomain));
|
||||
}
|
||||
|
||||
public function convertToClientJsonV1(UploadInfo $uploadInfo): array {
|
||||
return [
|
||||
public function convertToClientJsonV1(UploadInfo $uploadInfo, array $overrides = []): array {
|
||||
return array_merge([
|
||||
'id' => $uploadInfo->getId(),
|
||||
'url' => $this->getFileUrlV1($uploadInfo),
|
||||
'urlf' => $this->getFileUrlV1($uploadInfo, true),
|
||||
|
@ -84,7 +84,7 @@ class UploadsContext {
|
|||
// These can never be reached, and in situation where they technically could it's because of an outdated local record
|
||||
'deleted' => null,
|
||||
'dmca' => null,
|
||||
];
|
||||
], $overrides);
|
||||
}
|
||||
|
||||
public function supportsThumbnailing(UploadInfo $uploadInfo): bool {
|
||||
|
|
|
@ -121,23 +121,39 @@ class UploadsData {
|
|||
return $this->getUpload(uploadId: $uploadId);
|
||||
}
|
||||
|
||||
public function bumpUploadAccess(UploadInfo $uploadInfo): void {
|
||||
$stmt = $this->cache->get('UPDATE prm_uploads SET upload_accessed = NOW() WHERE upload_id = ?');
|
||||
$stmt->addParameter(1, $uploadInfo->getId());
|
||||
$stmt->execute();
|
||||
public function updateUpload(
|
||||
UploadInfo|string $uploadInfo,
|
||||
?string $fileName = null,
|
||||
int|null|bool $accessedAt = false,
|
||||
int|null|false $expiresAt = false
|
||||
): void {
|
||||
$fields = [];
|
||||
$values = [];
|
||||
|
||||
if($fileName !== null) {
|
||||
$fields[] = 'upload_name = ?';
|
||||
$values[] = $fileName;
|
||||
}
|
||||
|
||||
public function bumpUploadExpires(UploadInfo|string $uploadInfo): void {
|
||||
if(!$uploadInfo->hasExpiryTime())
|
||||
if($accessedAt !== false) {
|
||||
$fields[] = sprintf('upload_accessed = %s', $accessedAt === true ? 'NOW()' : 'FROM_UNIXTIME(?)');
|
||||
if($accessedAt !== true)
|
||||
$values[] = $accessedAt;
|
||||
}
|
||||
|
||||
if($expiresAt !== false) {
|
||||
$fields[] = 'upload_expires = NOW() + INTERVAL ? SECOND';
|
||||
$values[] = $expiresAt;
|
||||
}
|
||||
|
||||
if(empty($fields))
|
||||
return;
|
||||
|
||||
$bumpAmount = $uploadInfo->getBumpAmount();
|
||||
if($bumpAmount < 1)
|
||||
return;
|
||||
|
||||
$stmt = $this->cache->get('UPDATE prm_uploads SET upload_expires = NOW() + INTERVAL ? SECOND WHERE upload_id = ?');
|
||||
$stmt->addParameter(1, $bumpAmount);
|
||||
$stmt->addParameter(2, $uploadInfo->getId());
|
||||
$args = 0;
|
||||
$stmt = $this->cache->get(sprintf('UPDATE prm_uploads SET %s WHERE upload_id = ?', implode(', ', $fields)));
|
||||
foreach($values as $value)
|
||||
$stmt->addParameter(++$args, $value);
|
||||
$stmt->addParameter(++$args, $uploadInfo instanceof UploadInfo ? $uploadInfo->getId() : $uploadInfo);
|
||||
$stmt->execute();
|
||||
}
|
||||
|
||||
|
|
|
@ -81,10 +81,12 @@ class UploadsRoutes implements IRouteHandler {
|
|||
return 404;
|
||||
}
|
||||
|
||||
if(!$isThumbnail) {
|
||||
$uploadsData->bumpUploadAccess($uploadInfo);
|
||||
$uploadsData->bumpUploadExpires($uploadInfo);
|
||||
}
|
||||
if(!$isThumbnail)
|
||||
$uploadsData->updateUpload(
|
||||
$uploadInfo,
|
||||
accessedAt: true,
|
||||
expiresAt: $uploadInfo->getBumpAmountForUpdate(),
|
||||
);
|
||||
|
||||
$fileName = $uploadInfo->getName();
|
||||
$contentType = $uploadInfo->getMediaTypeString();
|
||||
|
@ -164,11 +166,12 @@ class UploadsRoutes implements IRouteHandler {
|
|||
if($blInfo !== null)
|
||||
return 451;
|
||||
|
||||
$fileName = $file->getSuggestedFileName();
|
||||
$uploadInfo = $uploadsData->getUpload(appInfo: $appInfo, userInfo: $userInfo, hashString: $hash);
|
||||
if($uploadInfo === null) {
|
||||
$uploadInfo = $uploadsData->createUpload(
|
||||
$appInfo, $userInfo, $_SERVER['REMOTE_ADDR'],
|
||||
$file->getSuggestedFileName(), mime_content_type($localFile),
|
||||
$fileName, mime_content_type($localFile),
|
||||
$fileSize, $hash, $appInfo->getBumpAmount(), true
|
||||
);
|
||||
$filePath = $this->uploadsCtx->getFileDataPath($uploadInfo);
|
||||
|
@ -178,13 +181,19 @@ class UploadsRoutes implements IRouteHandler {
|
|||
if($uploadInfo->isDeleted())
|
||||
$uploadsData->restoreUpload($uploadInfo);
|
||||
|
||||
$uploadsData->bumpUploadExpires($uploadInfo);
|
||||
$uploadsData->updateUpload(
|
||||
$uploadInfo,
|
||||
fileName: $fileName,
|
||||
expiresAt: $uploadInfo->getBumpAmountForUpdate(),
|
||||
);
|
||||
}
|
||||
|
||||
$response->setStatusCode(201);
|
||||
$response->setHeader('Content-Type', 'application/json; charset=utf-8');
|
||||
|
||||
return $this->uploadsCtx->convertToClientJsonV1($uploadInfo);
|
||||
return $this->uploadsCtx->convertToClientJsonV1($uploadInfo, [
|
||||
'name' => $fileName,
|
||||
]);
|
||||
}
|
||||
|
||||
#[HttpDelete('/uploads/([A-Za-z0-9\-_]+)')]
|
||||
|
|
Loading…
Reference in a new issue