84 lines
3.1 KiB
PHP
Executable file
84 lines
3.1 KiB
PHP
Executable file
#!/usr/bin/env php
|
|
<?php
|
|
use Index\XNumber;
|
|
|
|
require_once __DIR__ . '/../misuzu.php';
|
|
|
|
if(empty($msz->storageCtx->filesCtx->storage->localPath))
|
|
die(sprintf('No storage path is specified.%s', PHP_EOL));
|
|
if(!is_dir($msz->storageCtx->filesCtx->storage->localPath))
|
|
die(sprintf('Storage directory does not exist.%s', PHP_EOL));
|
|
if(fileowner($msz->storageCtx->filesCtx->storage->localPath) !== posix_getuid())
|
|
die(sprintf('Script must be run as the owner of the storage directory.%s', PHP_EOL));
|
|
|
|
$options = getopt('s', [
|
|
'base62',
|
|
], $restIndex);
|
|
|
|
if($argc <= $restIndex)
|
|
die(sprintf('No upload ID specified.%s', PHP_EOL));
|
|
|
|
$uploadId = $argv[$restIndex];
|
|
$isBase62 = isset($options['s']) || isset($options['base62']);
|
|
if($isBase62)
|
|
$uploadId = XNumber::fromBase62($uploadId);
|
|
|
|
try {
|
|
$uploadInfo = $msz->uploadsCtx->uploads->getUpload(uploadId: $uploadId);
|
|
} catch(RuntimeException $ex) {
|
|
$hint = $isBase62 ? 'trim last four characters from base62 id' : 'specify -s for base62 ids';
|
|
die(sprintf('The requested upload does not exist. (hint: %s)%s', $hint, PHP_EOL));
|
|
}
|
|
|
|
printf(
|
|
'Found upload #%d (%s) in pool #%d uploaded by user #%d (%s) at %s with canonical name "%s".%s',
|
|
$uploadInfo->id,
|
|
$uploadInfo->id62,
|
|
$uploadInfo->poolId,
|
|
$uploadInfo->userId,
|
|
$uploadInfo->remoteAddress,
|
|
$uploadInfo->createdAt->toIso8601ZuluString(),
|
|
htmlspecialchars($uploadInfo->name),
|
|
PHP_EOL
|
|
);
|
|
|
|
$variants = [];
|
|
foreach($msz->uploadsCtx->uploads->getUploadVariants($uploadInfo) as $variantInfo) {
|
|
$variants[] = $variantInfo;
|
|
printf(
|
|
'Found variant "%s" linking to file #%d with media type %s.%s',
|
|
$variantInfo->variant,
|
|
$variantInfo->fileId,
|
|
htmlspecialchars($variantInfo->type ?? '(none)'),
|
|
PHP_EOL
|
|
);
|
|
}
|
|
|
|
$displayStorageDeleteHint = false;
|
|
|
|
if(count($variants) < 1) {
|
|
printf('This upload has no variants to delete.%s', PHP_EOL);
|
|
} else {
|
|
$variants = array_reverse($variants);
|
|
foreach($variants as $variantInfo)
|
|
try {
|
|
printf('Deleting variant "%s" for upload #%d...%s', $variantInfo->variant, $variantInfo->uploadId, PHP_EOL);
|
|
$msz->storageCtx->uploadsCtx->uploads->deleteUploadVariant($variantInfo);
|
|
|
|
printf('Attempting to delete file...%s', PHP_EOL);
|
|
$msz->storageCtx->filesCtx->deleteFile($variantInfo->fileId, false);
|
|
} catch(Exception $ex) {
|
|
printf('Exception occurred while deleting variant:%s', PHP_EOL);
|
|
printf('%s%s', $ex->getMessage(), PHP_EOL);
|
|
}
|
|
}
|
|
|
|
try {
|
|
printf('Deleting upload #%d...%s', $uploadInfo->id, PHP_EOL);
|
|
$msz->uploadsCtx->uploads->deleteUpload($uploadInfo);
|
|
} catch(Exception $ex) {
|
|
printf('Exception occurred while deleting upload:%s%s%s', PHP_EOL, $ex->getMessage(), PHP_EOL);
|
|
}
|
|
|
|
if($displayStorageDeleteHint)
|
|
printf('NOTE: if the exception(s) that occurred while deleting variants were related to database constraints, an upload linking to the same file exists, if they occurred due to filesystem permissions, the file will linger but will be removed during the next scheduled clean up.%s', PHP_EOL);
|