Overwrite existing filename on reupload.

This commit is contained in:
flash 2024-04-02 19:10:31 +00:00
parent 116202929b
commit bca1630ea9
4 changed files with 53 additions and 24 deletions

View file

@ -121,6 +121,10 @@ class UploadInfo {
return $this->bump; return $this->bump;
} }
public function getBumpAmountForUpdate(): ?int {
return $this->expires !== null && $this->bump > 0 ? $this->bump : null;
}
public function getName(): string { public function getName(): string {
return $this->name; return $this->name;
} }

View file

@ -65,8 +65,8 @@ class UploadsContext {
return sprintf('%s.t', $this->getFileUrlV1($uploadInfo, $forceApiDomain)); return sprintf('%s.t', $this->getFileUrlV1($uploadInfo, $forceApiDomain));
} }
public function convertToClientJsonV1(UploadInfo $uploadInfo): array { public function convertToClientJsonV1(UploadInfo $uploadInfo, array $overrides = []): array {
return [ return array_merge([
'id' => $uploadInfo->getId(), 'id' => $uploadInfo->getId(),
'url' => $this->getFileUrlV1($uploadInfo), 'url' => $this->getFileUrlV1($uploadInfo),
'urlf' => $this->getFileUrlV1($uploadInfo, true), '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 // These can never be reached, and in situation where they technically could it's because of an outdated local record
'deleted' => null, 'deleted' => null,
'dmca' => null, 'dmca' => null,
]; ], $overrides);
} }
public function supportsThumbnailing(UploadInfo $uploadInfo): bool { public function supportsThumbnailing(UploadInfo $uploadInfo): bool {

View file

@ -121,23 +121,39 @@ class UploadsData {
return $this->getUpload(uploadId: $uploadId); return $this->getUpload(uploadId: $uploadId);
} }
public function bumpUploadAccess(UploadInfo $uploadInfo): void { public function updateUpload(
$stmt = $this->cache->get('UPDATE prm_uploads SET upload_accessed = NOW() WHERE upload_id = ?'); UploadInfo|string $uploadInfo,
$stmt->addParameter(1, $uploadInfo->getId()); ?string $fileName = null,
$stmt->execute(); 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($accessedAt !== false) {
if(!$uploadInfo->hasExpiryTime()) $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; return;
$bumpAmount = $uploadInfo->getBumpAmount(); $args = 0;
if($bumpAmount < 1) $stmt = $this->cache->get(sprintf('UPDATE prm_uploads SET %s WHERE upload_id = ?', implode(', ', $fields)));
return; foreach($values as $value)
$stmt->addParameter(++$args, $value);
$stmt = $this->cache->get('UPDATE prm_uploads SET upload_expires = NOW() + INTERVAL ? SECOND WHERE upload_id = ?'); $stmt->addParameter(++$args, $uploadInfo instanceof UploadInfo ? $uploadInfo->getId() : $uploadInfo);
$stmt->addParameter(1, $bumpAmount);
$stmt->addParameter(2, $uploadInfo->getId());
$stmt->execute(); $stmt->execute();
} }

View file

@ -81,10 +81,12 @@ class UploadsRoutes implements IRouteHandler {
return 404; return 404;
} }
if(!$isThumbnail) { if(!$isThumbnail)
$uploadsData->bumpUploadAccess($uploadInfo); $uploadsData->updateUpload(
$uploadsData->bumpUploadExpires($uploadInfo); $uploadInfo,
} accessedAt: true,
expiresAt: $uploadInfo->getBumpAmountForUpdate(),
);
$fileName = $uploadInfo->getName(); $fileName = $uploadInfo->getName();
$contentType = $uploadInfo->getMediaTypeString(); $contentType = $uploadInfo->getMediaTypeString();
@ -164,11 +166,12 @@ class UploadsRoutes implements IRouteHandler {
if($blInfo !== null) if($blInfo !== null)
return 451; return 451;
$fileName = $file->getSuggestedFileName();
$uploadInfo = $uploadsData->getUpload(appInfo: $appInfo, userInfo: $userInfo, hashString: $hash); $uploadInfo = $uploadsData->getUpload(appInfo: $appInfo, userInfo: $userInfo, hashString: $hash);
if($uploadInfo === null) { if($uploadInfo === null) {
$uploadInfo = $uploadsData->createUpload( $uploadInfo = $uploadsData->createUpload(
$appInfo, $userInfo, $_SERVER['REMOTE_ADDR'], $appInfo, $userInfo, $_SERVER['REMOTE_ADDR'],
$file->getSuggestedFileName(), mime_content_type($localFile), $fileName, mime_content_type($localFile),
$fileSize, $hash, $appInfo->getBumpAmount(), true $fileSize, $hash, $appInfo->getBumpAmount(), true
); );
$filePath = $this->uploadsCtx->getFileDataPath($uploadInfo); $filePath = $this->uploadsCtx->getFileDataPath($uploadInfo);
@ -178,13 +181,19 @@ class UploadsRoutes implements IRouteHandler {
if($uploadInfo->isDeleted()) if($uploadInfo->isDeleted())
$uploadsData->restoreUpload($uploadInfo); $uploadsData->restoreUpload($uploadInfo);
$uploadsData->bumpUploadExpires($uploadInfo); $uploadsData->updateUpload(
$uploadInfo,
fileName: $fileName,
expiresAt: $uploadInfo->getBumpAmountForUpdate(),
);
} }
$response->setStatusCode(201); $response->setStatusCode(201);
$response->setHeader('Content-Type', 'application/json; charset=utf-8'); $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\-_]+)')] #[HttpDelete('/uploads/([A-Za-z0-9\-_]+)')]