1
0
Fork 0
mirror of https://codeberg.org/demostf/api.git synced 2026-06-03 18:04:08 +02:00

remove demo file if saving fails

This commit is contained in:
Robin Appelman 2022-05-18 13:35:10 +02:00
commit eb3c909a93
3 changed files with 37 additions and 4 deletions

View file

@ -43,4 +43,11 @@ class DemoStore {
unlink($demo->getPath());
}
}
public function removeByName(string $name): void {
$path = $this->generatePath($name);
if (file_exists($path)) {
unlink($path);
}
}
}

View file

@ -83,10 +83,15 @@ class UploadProvider extends BaseProvider {
return $error;
}
try {
$storedDemo = $this->store->store($demoFile, $hash . '_' . $name);
$upload = new Upload($name, $red, $blu, $user->getId(), $hash);
$id = $this->demoSaver->saveDemo($parsed, $header, $storedDemo, $upload);
} catch (\Exception $e) {
$this->store->removeByName($name);
return 'Error while saving demo: ' . $e->getMessage();
}
return 'STV available at: ' . $this->baseUrl . '/' . $id;
}

View file

@ -20,6 +20,8 @@ class DemoStoreTest extends TestCase {
$storedDemo = $demoStore->store($file, 'foodemo.dem');
$this->assertTrue(file_exists($storedDemo->getPath()));
$this->assertStringEndsWith('/foodemo.dem', $storedDemo->getUrl());
$this->assertStringStartsWith('https://static.example.com/', $storedDemo->getUrl());
$this->assertEquals('static', $storedDemo->getBackend());
@ -30,4 +32,23 @@ class DemoStoreTest extends TestCase {
rmdir(\dirname($storedDemo->getPath(), 2));
rmdir($targetDir);
}
public function testRemoveByName() {
$targetDir = tempnam(sys_get_temp_dir(), 'dummy_target_');
unlink($targetDir);
mkdir($targetDir);
$demoStore = new DemoStore($targetDir, 'static.example.com');
$file = tempnam(sys_get_temp_dir(), 'dummy_');
file_put_contents($file, 'foobar');
$storedDemo = $demoStore->store($file, 'foodemo.dem');
$this->assertTrue(file_exists($storedDemo->getPath()));
$demoStore->removeByName('foodemo.dem');
$this->assertFalse(file_exists($storedDemo->getPath()));
}
}