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:
parent
86ffe80044
commit
eb3c909a93
3 changed files with 37 additions and 4 deletions
|
|
@ -43,4 +43,11 @@ class DemoStore {
|
||||||
unlink($demo->getPath());
|
unlink($demo->getPath());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function removeByName(string $name): void {
|
||||||
|
$path = $this->generatePath($name);
|
||||||
|
if (file_exists($path)) {
|
||||||
|
unlink($path);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -83,10 +83,15 @@ class UploadProvider extends BaseProvider {
|
||||||
return $error;
|
return $error;
|
||||||
}
|
}
|
||||||
|
|
||||||
$storedDemo = $this->store->store($demoFile, $hash . '_' . $name);
|
try {
|
||||||
$upload = new Upload($name, $red, $blu, $user->getId(), $hash);
|
$storedDemo = $this->store->store($demoFile, $hash . '_' . $name);
|
||||||
|
$upload = new Upload($name, $red, $blu, $user->getId(), $hash);
|
||||||
|
|
||||||
$id = $this->demoSaver->saveDemo($parsed, $header, $storedDemo, $upload);
|
$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;
|
return 'STV available at: ' . $this->baseUrl . '/' . $id;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,8 @@ class DemoStoreTest extends TestCase {
|
||||||
|
|
||||||
$storedDemo = $demoStore->store($file, 'foodemo.dem');
|
$storedDemo = $demoStore->store($file, 'foodemo.dem');
|
||||||
|
|
||||||
|
$this->assertTrue(file_exists($storedDemo->getPath()));
|
||||||
|
|
||||||
$this->assertStringEndsWith('/foodemo.dem', $storedDemo->getUrl());
|
$this->assertStringEndsWith('/foodemo.dem', $storedDemo->getUrl());
|
||||||
$this->assertStringStartsWith('https://static.example.com/', $storedDemo->getUrl());
|
$this->assertStringStartsWith('https://static.example.com/', $storedDemo->getUrl());
|
||||||
$this->assertEquals('static', $storedDemo->getBackend());
|
$this->assertEquals('static', $storedDemo->getBackend());
|
||||||
|
|
@ -30,4 +32,23 @@ class DemoStoreTest extends TestCase {
|
||||||
rmdir(\dirname($storedDemo->getPath(), 2));
|
rmdir(\dirname($storedDemo->getPath(), 2));
|
||||||
rmdir($targetDir);
|
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()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue