Skip to content

Commit

Permalink
CyberBuddy - Add the ability to upload many files at once in a given …
Browse files Browse the repository at this point in the history
…collection.
  • Loading branch information
csavelief committed Oct 14, 2024
1 parent a31f66b commit 10d9939
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,19 @@ public function uploadManyFiles(UploadManyFilesRequest $request)
$collection = \App\Modules\CyberBuddy\Models\Collection::create(['name' => $request->string('collection')]);
}

$files = $request->file('files');
$files = $request->allFiles();
$successes = [];
$errors = [];

foreach ($files as $file) {
foreach ($files['files'] as $file) {
$url = $this->saveOneFile($collection, $file);
if ($url) {
$successes[] = $url;
} else {
$errors[] = $file->getClientOriginalName();
}
}
if (count($errors) > 0) {
if (count($errors) <= 0) {
return response()->json([
'success' => 'All files have been saved and will be processed soon.',
'urls' => $successes,
Expand Down
19 changes: 11 additions & 8 deletions resources/views/components/knowledge-base.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
</div>
<script>
let file = null;
let files = null;
let collection = null;
const elSubmit = new com.computablefacts.blueprintjs.MinimalButton(document.getElementById('submit'),
Expand All @@ -85,10 +85,13 @@
elSubmit.disabled = true;
const formData = new FormData();
formData.append('file', file);
formData.append('collection', collection);
axios.post('/cb/web/files/one', formData, {
for (let i = 0; i < files.length; i++) {
formData.append('files[]', files[i]);
}
axios.post('/cb/web/files/many', formData, {
headers: {
'Content-Type': 'multipart/form-data',
}
Expand All @@ -100,18 +103,18 @@
});
});
const elFile = new com.computablefacts.blueprintjs.MinimalFileInput(document.getElementById('files'));
elFile.onSelectionChange(item => {
file = item;
elSubmit.disabled = !file || !collection;
const elFile = new com.computablefacts.blueprintjs.MinimalFileInput(document.getElementById('files'), true);
elFile.onSelectionChange(items => {
files = items;
elSubmit.disabled = !files || !collection;
});
elFile.buttonText = "{{ __('Browse') }}";
const elCollections = new com.computablefacts.blueprintjs.MinimalSelect(document.getElementById('collections'), null,
null, null, query => query);
elCollections.onSelectionChange(item => {
collection = item;
elSubmit.disabled = !file || !collection;
elSubmit.disabled = !files || !collection;
});
elCollections.defaultText = "{{ __('Select or create collection...') }}";
Expand Down

0 comments on commit 10d9939

Please sign in to comment.