forked from getbookshelf/bookshelf-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.php
103 lines (90 loc) · 3.95 KB
/
upload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
require_once __DIR__ . '/inc/base.php';
insertHeader();
$current_step = $_POST['step'];
$additional_scripts = '';
switch($current_step) {
case '':
?>
<h1>Upload eBook</h1>
<form enctype="multipart/form-data" action="#" method="POST">
<input name="step" type="hidden" value="search">
<input name="file" type="file"><br>
<input type="submit" value="Upload">
</form>
<?php
break;
case 'search':
if(isset($_FILES['file'])) {
$file_manager = new \Bookshelf\DataIo\FileManager();
$config = new \Bookshelf\Core\Configuration(true);
$uploaded_book_id = $file_manager->uploadBook($_FILES);
$base_url = $config->getBaseUrl();
$library_mgr = new \Bookshelf\Core\LibraryManager();
$book = $library_mgr->getBookById($uploaded_book_id);
if($book === null) {
\Bookshelf\Utility\ErrorHandler::throwError('Book does not exist.', \Bookshelf\Utility\ErrorLevel::ERROR);
header('Location: index.php');
exit();
}
echo '<h1>Set book metadata</h1>
<p>Your book <span class="italic">' . $_FILES['file']['name'] . '</span> has been uploaded successfully. You can now set its metadata. If none of the API\'s metadata matches your book, you can always <a href="' . $base_url . '/edit-book.php?id=' . $id . '">manually edit</a> the metadata.</p>';
echo '<p>
Query: <input id="query_string" name="query_string" type="text" value="' . $book->getQueryString() . '"> <button id="btn-search" name="btn-search">Search</button>
</p>';
echo '<div id="searchform"></div>';
$additional_scripts = '$("#btn-search").click(function() {
$("#searchform").html("<p>Loading...</p>");
$.ajax({
context: this,
dataType: "html",
type: "POST",
data: {
id: "' . $uploaded_book_id . '",
query_string: $("#query_string").val(),
},
url: "' . $base_url . '/inc/searchform.php",
success: function(data) {
$("#searchform").html(data);
}
});
});
$(document).ready(function() {
$("#btn-search").click();
});';
}
break;
case 'save_meta':
if(!isset($_POST['id']) || !isset($_POST['chosen_book'])) {
\Bookshelf\Utility\ErrorHandler::throwError('No request.', \Bookshelf\Utility\ErrorLevel::WARNING);
header('Location: index.php');
exit();
}
$lib_man = new \Bookshelf\Core\LibraryManager();
$id = $_POST['id'];
$used_api = explode('.', $_POST['chosen_book'][0], 2)[0];
$api_id = explode('.', $_POST['chosen_book'][0], 2)[1];
switch($used_api) {
case 'GoogleBooks':
$api_request = new \Bookshelf\ExternalApi\GoogleBooksApiRequest();
$api_request->getBookByIdentifier($api_id);
$result = $api_request->results()->getResults()[0]['metadata'];
$cover_image = $result->cover_image;
$title = $result->title;
$author = $result->author;
$description = $result->description;
$language = $result->language;
$identifier = $result->identifier;
$to_update = array('cover_image' => $cover_image, 'title' => $title, 'author' => $author, 'description' => $description, 'language' => $language, 'identifier' => $identifier);
$lib_man->updateBook($id, $to_update);
header('Location: index.php');
exit();
break;
default:
\Bookshelf\Utility\ErrorHandler::throwError('Did not recognize API result.', \Bookshelf\Utility\ErrorLevel::WARNING);
}
break;
default:
header('Location: upload.php');
}
insertFooter($additional_scripts);