-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupload.php
71 lines (57 loc) · 2.63 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
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Include necessary libraries
require_once 'lib.required.php';
// Get the chat_id if present
$chat_id = isset($_REQUEST['chat_id']) ? $_REQUEST['chat_id'] : '';
// Create a new chat session if no chat ID is provided
if (empty($chat_id)) {
$chat_id = $new_chat_id = create_chat($user, 'New auto-generated Chat', '', $_SESSION['deployment'], '', '');
}
// Check if there's a request to remove the uploaded file
if (isset($_GET['remove']) && $_GET['remove'] == '1') {
// Clear the session variables
unset($_SESSION['document_text']);
unset($_SESSION['document_type']);
unset($_SESSION['document_name']);
update_chat_document($user, $chat_id, '', '','');
// Redirect to the main page with chat_id
#header('Location: index.php?chat_id=' . urlencode($chat_id));
header('Location: ' . urlencode($chat_id));
exit;
}
if (isset($_FILES['uploadDocument'])) {
$file = $_FILES['uploadDocument'];
$mimeType = mime_content_type($file['tmp_name']);
// Check if the uploaded file is an image or a document
if (strpos($mimeType, 'image/') === 0) {
// Handle image uploads
$base64Image = local_image_to_data_url($file['tmp_name'], $mimeType);
// Save the base64 image to the session and the database
$_SESSION['document_text'] = $base64Image;
$_SESSION['document_type'] = $mimeType;
$_SESSION['document_name'] = basename($file['name']);
update_chat_document($user, $chat_id, $_SESSION['document_name'], $_SESSION['document_type'], $_SESSION['document_text']);
} else {
// Handle document uploads via Python script
$command = __DIR__ . "/parser_multi.py \"" . $file['tmp_name'] . "\" \"" . basename($file['name']) . "\" 2>&1";
$output = shell_exec($command);
if (strpos($output, 'ValueError') === false) {
// Store the text and the original filename in session variables
$_SESSION['document_text'] = $output;
$_SESSION['document_type'] = $mimeType;
$_SESSION['document_name'] = basename($file['name']);
update_chat_document($user, $chat_id, $_SESSION['document_name'], $_SESSION['document_type'], $_SESSION['document_text']);
} else {
$_SESSION['error'] = 'There was an error parsing the uploaded document. Please ensure it is the correct file type.';
}
}
// Redirect back to the index page
header('Location: ' . urlencode($chat_id));
} else {
header('Location: ' . urlencode($chat_id));
}
// Prevent accidental output by stopping the script here
exit;