2
2
3
3
namespace App \GaelO \UseCases \ExportDatabase ;
4
4
5
+ use App \GaelO \Adapters \FrameworkAdapter ;
6
+ use App \GaelO \Adapters \ZipStreamAdapter ;
5
7
use App \GaelO \Exceptions \AbstractGaelOException ;
6
8
use App \GaelO \Exceptions \GaelOForbiddenException ;
7
9
use App \GaelO \Interfaces \Adapters \DatabaseDumperInterface ;
8
10
use App \GaelO \Services \AuthorizationService \AuthorizationUserService ;
9
- use App \GaelO \Util ;
10
11
use Exception ;
11
- use ZipArchive ;
12
12
13
13
class ExportDatabase
14
14
{
@@ -29,25 +29,9 @@ public function execute(ExportDatabaseRequest $exportDatabaseRequest, ExportData
29
29
$ this ->checkAuthorization ($ exportDatabaseRequest ->currentUserId );
30
30
//Operation might be long, set max execution time to 30 minutes
31
31
set_time_limit (1800 );
32
- $ zip = new ZipArchive ();
33
- $ tempZip = tempnam (ini_get ('upload_tmp_dir ' ), 'TMPZIPDB_ ' );
34
- $ zip ->open ($ tempZip , ZipArchive::OVERWRITE );
35
-
36
- $ filePathSql = tempnam (ini_get ('upload_tmp_dir ' ), 'TMPDB_ ' );
37
- $ this ->databaseDumperInterface ->createDatabaseDumpFile ($ filePathSql );
38
-
39
- $ date = Date ('Ymd_His ' );
40
- $ zip ->addFile ($ filePathSql , "export_database_ $ date.sql " );
41
-
42
- Util::addStoredFilesInZipAndClose ($ zip , null );
43
-
44
- //Unlick after lock released by zip close
45
- unlink ($ filePathSql );
46
32
47
33
$ exportDatabaseResponse ->status = 200 ;
48
34
$ exportDatabaseResponse ->statusText = 'OK ' ;
49
- $ exportDatabaseResponse ->zipFile = $ tempZip ;
50
- $ exportDatabaseResponse ->fileName = "export_database_ " . $ date . ".zip " ;
51
35
} catch (AbstractGaelOException $ e ) {
52
36
$ exportDatabaseResponse ->status = $ e ->statusCode ;
53
37
$ exportDatabaseResponse ->statusText = $ e ->statusText ;
@@ -63,4 +47,25 @@ private function checkAuthorization(int $userId): void
63
47
throw new GaelOForbiddenException ();
64
48
}
65
49
}
50
+
51
+ public function readExport ()
52
+ {
53
+ $ date = Date ('Ymd_His ' );
54
+ $ zipStream = new ZipStreamAdapter ();
55
+ $ zipStream ->init ("export_database_ " . $ date . ".zip " );
56
+ //send stored file for this study
57
+ try {
58
+ $ filePathSql = tempnam (ini_get ('upload_tmp_dir ' ), 'TMPDB_ ' );
59
+ $ this ->databaseDumperInterface ->createDatabaseDumpFile ($ filePathSql );
60
+ $ zipStream ->addFileFromStream ("export_database_ $ date.sql " , fopen ($ filePathSql , 'rb ' ), );
61
+ $ files = FrameworkAdapter::getStoredFiles ();
62
+ foreach ($ files as $ file ) {
63
+ $ fileStream = FrameworkAdapter::getFile ($ file , true );
64
+ $ zipStream ->addFileFromStream ($ file , $ fileStream );
65
+ }
66
+ $ zipStream ->finish ();
67
+ } finally {
68
+ unlink ($ filePathSql );
69
+ }
70
+ }
66
71
}
0 commit comments