Skip to content

Commit 2227c6b

Browse files
committed
MDL-83986 file: Add replace file methods to file_storage.
1 parent 20bb0b2 commit 2227c6b

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
issueNumber: MDL-83986
2+
notes:
3+
core_files:
4+
- message: >
5+
The file_storage class now has two methods to enable updating the
6+
content of a file:
7+
8+
- `replace_file_from_file()`
9+
10+
- `replace_file_from_string()`
11+
type: improved

public/lib/filestorage/file_storage.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2467,6 +2467,60 @@ public function update_references($referencefileid, $lastsync, $lifetime, $conte
24672467
$DB->update_record('files_reference', (object)$data);
24682468
}
24692469

2470+
/**
2471+
* Replace the content of an existing file with that from a given real file.
2472+
*
2473+
* @param stored_file $file The file to replace
2474+
* @param string $filepath The path to the file on disk
2475+
*
2476+
* @return stored_file The new file
2477+
*
2478+
* @throws moodle_exception
2479+
*/
2480+
public function replace_file_from_file(stored_file $file, string $filepath): stored_file {
2481+
global $DB;
2482+
2483+
$tx = $DB->start_delegated_transaction();
2484+
try {
2485+
$filerecord = $DB->get_record('files', ['id' => $file->get_id()], '*', MUST_EXIST);
2486+
$file->delete();
2487+
$newfile = $this->create_file_from_pathname($filerecord, $filepath);
2488+
} catch (file_exception $e) {
2489+
$tx->rollback($e);
2490+
}
2491+
2492+
$tx->allow_commit();
2493+
2494+
return $newfile;
2495+
}
2496+
2497+
/**
2498+
* Replace the content of an existing file with a given string.
2499+
*
2500+
* @param stored_file $file The file to replace
2501+
* @param string $content The new content
2502+
*
2503+
* @return stored_file The new file
2504+
*
2505+
* @throws moodle_exception
2506+
*/
2507+
public function replace_file_from_string(stored_file $file, string $content): stored_file {
2508+
global $DB;
2509+
2510+
$tx = $DB->start_delegated_transaction();
2511+
try {
2512+
$filerecord = $DB->get_record('files', ['id' => $file->get_id()], '*', MUST_EXIST);
2513+
$file->delete();
2514+
$newfile = $this->create_file_from_string($filerecord, $content);
2515+
} catch (file_exception $e) {
2516+
$tx->rollback($e);
2517+
}
2518+
2519+
$tx->allow_commit();
2520+
2521+
return $newfile;
2522+
}
2523+
24702524
/**
24712525
* Calculate and return the contenthash of the supplied file.
24722526
*

public/lib/filestorage/tests/file_storage_test.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2367,6 +2367,46 @@ public function test_before_file_created_hook_executed_filecontent(): void {
23672367
$file->get_contenthash(),
23682368
);
23692369
}
2370+
2371+
/**
2372+
* Tests the replace_file_from_file method.
2373+
*
2374+
* @covers \file_storage::replace_file_from_file
2375+
*
2376+
* @return void
2377+
*/
2378+
public function test_replace_file_from_file() {
2379+
$this->resetAfterTest(true);
2380+
$fs = get_file_storage();
2381+
2382+
$record = $this->generate_file_record();
2383+
2384+
$file1 = $fs->create_file_from_string($record, 'text contents');
2385+
$newfile1 = $fs->replace_file_from_file($file1, __DIR__ . '/fixtures/test.txt');
2386+
2387+
$this->assertEquals('test', $newfile1->get_content());
2388+
$this->assertEquals($file1->get_timecreated(), $newfile1->get_timecreated());
2389+
}
2390+
2391+
/**
2392+
* Tests the replace_file_from_string method.
2393+
*
2394+
* @covers \file_storage::replace_file_from_string
2395+
*
2396+
* @return void
2397+
*/
2398+
public function test_replace_file_from_string() {
2399+
$this->resetAfterTest(true);
2400+
$fs = get_file_storage();
2401+
2402+
$record = $this->generate_file_record();
2403+
2404+
$file1 = $fs->create_file_from_string($record, 'text contents');
2405+
$newfile1 = $fs->replace_file_from_string($file1, 'new text contents');
2406+
2407+
$this->assertEquals('new text contents', $newfile1->get_content());
2408+
$this->assertEquals($file1->get_timecreated(), $newfile1->get_timecreated());
2409+
}
23702410
}
23712411

23722412
class test_stored_file_inspection extends stored_file {

0 commit comments

Comments
 (0)