2
2
3
3
namespace BookStack \Entities \Controllers ;
4
4
5
+ use BookStack \Entities \Models \Book ;
6
+ use BookStack \Entities \Models \Bookshelf ;
5
7
use BookStack \Entities \Queries \BookshelfQueries ;
6
8
use BookStack \Entities \Tools \ExportFormatter ;
7
9
use BookStack \Http \Controller ;
10
+ use BookStack \Http \Request ;
11
+ use Illuminate \Support \Facades \Response ;
8
12
use Throwable ;
9
13
10
14
class BookshelfExportController extends Controller
@@ -21,46 +25,106 @@ public function __construct(
21
25
*
22
26
* @throws Throwable
23
27
*/
24
- public function pdf (string $ bookshelfSlug )
28
+ public function pdf (Request $ request , string $ bookshelfSlug )
25
29
{
30
+ dd ($ request ->query ());
26
31
$ bookshelf = $ this ->queries ->findVisibleBySlugOrFail ($ bookshelfSlug );
27
- $ pdfContent = $ this ->exportFormatter ->bookshelfToPdf ($ bookshelf );
32
+ if ($ request ['split ' ] === true ) {
33
+ return $ this ->downloadAllInZip ($ bookshelf , 'pdf ' );
34
+ } else {
35
+ $ htmlContent = $ this ->exportFormatter ->bookshelfToPdf ($ bookshelf );
28
36
29
- return $ this ->download ()->directly ($ pdfContent , $ bookshelfSlug . '.pdf ' );
37
+ return $ this ->download ()->directly ($ htmlContent , $ bookshelfSlug . '.pdf ' );
38
+ }
30
39
}
31
40
32
41
/**
33
42
* Export a book as a contained HTML file.
34
43
*
35
44
* @throws Throwable
36
45
*/
37
- public function html (string $ bookshelfSlug )
46
+ public function html (Request $ request , string $ bookshelfSlug )
38
47
{
39
48
$ bookshelf = $ this ->queries ->findVisibleBySlugOrFail ($ bookshelfSlug );
40
- $ htmlContent = $ this ->exportFormatter ->bookshelfToContainedHtml ($ bookshelf );
49
+ if ($ request ['split ' ] === true ) {
50
+ return $ this ->downloadAllInZip ($ bookshelf , 'html ' );
51
+ } else {
52
+ $ htmlContent = $ this ->exportFormatter ->bookshelfToContainedHtml ($ bookshelf );
41
53
42
- return $ this ->download ()->directly ($ htmlContent , $ bookshelfSlug . '.html ' );
54
+ return $ this ->download ()->directly ($ htmlContent , $ bookshelfSlug . '.html ' );
55
+ }
43
56
}
44
57
45
58
/**
46
59
* Export a book as a plain text file.
47
60
*/
48
- public function plainText (string $ bookshelfSlug )
61
+ public function plainText (Request $ request , string $ bookshelfSlug )
49
62
{
50
63
$ bookshelf = $ this ->queries ->findVisibleBySlugOrFail ($ bookshelfSlug );
51
- $ textContent = $ this ->exportFormatter ->bookshelfToPlainText ($ bookshelf );
64
+ if ($ request ['split ' ] === true ) {
65
+ return $ this ->downloadAllInZip ($ bookshelf , 'txt ' );
66
+ } else {
67
+ $ htmlContent = $ this ->exportFormatter ->bookshelfToPlainText ($ bookshelf );
52
68
53
- return $ this ->download ()->directly ($ textContent , $ bookshelfSlug . '.txt ' );
69
+ return $ this ->download ()->directly ($ htmlContent , $ bookshelfSlug . '.txt ' );
70
+ }
54
71
}
55
72
56
73
/**
57
74
* Export a book as a markdown file.
58
75
*/
59
- public function markdown (string $ bookshelfSlug )
76
+ public function markdown (Request $ request , string $ bookshelfSlug )
60
77
{
61
78
$ bookshelf = $ this ->queries ->findVisibleBySlugOrFail ($ bookshelfSlug );
62
- $ textContent = $ this ->exportFormatter ->bookshelfToMarkdown ($ bookshelf );
79
+ if ($ request ['split ' ] === true ) {
80
+ return $ this ->downloadAllInZip ($ bookshelf , 'md ' );
81
+ } else {
82
+ $ htmlContent = $ this ->exportFormatter ->bookshelfToMarkdown ($ bookshelf );
63
83
64
- return $ this ->download ()->directly ($ textContent , $ bookshelfSlug . '.md ' );
84
+ return $ this ->download ()->directly ($ htmlContent , $ bookshelfSlug . '.md ' );
85
+ }
86
+ }
87
+
88
+ public function downloadAllInZip (Bookshelf $ bookshelf , string $ type )
89
+ {
90
+ $ bookshelf ->load ('books ' );
91
+
92
+ $ zip = new \ZipArchive ();
93
+
94
+ $ tempFilePath = storage_path ('app/public/ ' . $ bookshelf ->slug . '.zip ' );
95
+ if ($ zip ->open ($ tempFilePath , \ZipArchive::CREATE ) === true ) {
96
+ foreach ($ bookshelf ->books as $ book ) {
97
+ $ pdfContent = $ this ->getContentBasedOntype ($ book , $ type );
98
+ $ zip ->addFromString ($ book ->slug , $ pdfContent );
99
+ }
100
+ $ zip ->close ();
101
+
102
+ return Response::download ($ tempFilePath )->deleteFileAfterSend (true );
103
+ }
104
+ }
105
+
106
+ public function getContentBasedOntype (Book $ book , string $ type )
107
+ {
108
+ switch ($ type ) {
109
+ case 'pdf ' :
110
+ return $ this ->exportFormatter ->bookToPdf ($ book );
111
+ break ;
112
+
113
+
114
+ case 'html ' :
115
+ return $ this ->exportFormatter ->bookToContainedHtml ($ book );
116
+ break ;
117
+
118
+ case 'txt ' :
119
+ return $ this ->exportFormatter ->bookToPlainText ($ book );
120
+ break ;
121
+
122
+ case 'md ' :
123
+ return $ this ->exportFormatter ->bookToMarkdown ($ book );
124
+ break ;
125
+ default :
126
+ return "" ;
127
+ break ;
128
+ }
65
129
}
66
130
}
0 commit comments