Skip to content

Commit

Permalink
Implement dynamic display of annotation/file label import button(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
mzur committed Nov 8, 2024
1 parent d99bb75 commit bf13e02
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 28 deletions.
12 changes: 11 additions & 1 deletion app/Http/Controllers/Api/Volumes/MetadataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public function show($id)
* @apiPermission projectAdmin
* @apiDescription This endpoint allows adding or updating metadata such as geo
* coordinates for volume files. The uploaded metadata file replaces any previously
* uploaded file.
* uploaded file. The JSON response indicates whether the uploaded metadata file
* contained annotations or file labels.
*
* @apiParam {Number} id The volume ID.
*
Expand All @@ -83,6 +84,15 @@ public function store(StoreVolumeMetadata $request)
$request->volume->saveMetadata($request->file('file'));
$request->volume->update(['metadata_parser' => $request->input('parser')]);
Queue::push(new UpdateVolumeMetadata($request->volume));

if ($this->isAutomatedRequest()) {
$metadata = $request->volume->getMetadata();

return [

Check failure on line 91 in app/Http/Controllers/Api/Volumes/MetadataController.php

View workflow job for this annotation

GitHub Actions / lint-php

Method Biigle\Http\Controllers\Api\Volumes\MetadataController::store() with return type void returns array<string, bool> but should not return anything.
'has_annotations' => $metadata->hasAnnotations(),
'has_file_labels' => $metadata->hasFileLabels(),
];
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions app/Traits/HasMetadataFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public function deleteMetadata($noUpdate = false): void
{
if ($this->hasMetadata()) {
Storage::disk($this->getMetadataFileDisk())->delete($this->metadata_file_path);
$disk = $this->getMetadataFileDisk();
Cache::store('array')->forget("metadata-{$disk}-{$this->metadata_file_path}");
if (!$noUpdate) {
$this->update(['metadata_file_path' => null]);
}
Expand Down
23 changes: 22 additions & 1 deletion resources/assets/js/volumes/metadataUpload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export default {
success: false,
message: undefined,
hasMetadata: false,
hasMetadataAnnotations: false,
hasMetadataFileLabels: false,
parsers: [],
selectedParser: null,
importForm: {
Expand All @@ -27,6 +29,17 @@ export default {
},
};
},
computed: {
showImportDropdown() {
return this.hasMetadataAnnotations && this.hasMetadataFileLabels;
},
showImportAnnotations() {
return this.hasMetadataAnnotations && !this.hasMetadataFileLabels;
},
showImportFileLabels() {
return !this.hasMetadataAnnotations && this.hasMetadataFileLabels;
},
},
methods: {
handleSuccess() {
this.error = false;
Expand All @@ -51,7 +64,11 @@ export default {
data.append('file', event.target.files[0]);
data.append('parser', this.selectedParser.parserClass);
MetadataApi.save({id: this.volumeId}, data)
.then(() => this.hasMetadata = true)
.then((response) => {
this.hasMetadata = true
this.hasMetadataAnnotations = response.body.has_annotations;
this.hasMetadataFileLabels = response.body.has_file_labels;
})
.then(this.handleSuccess)
.catch(this.handleError)
.finally(this.finishLoading);
Expand All @@ -64,6 +81,8 @@ export default {
},
handleFileDeleted() {
this.hasMetadata = false;
this.hasMetadataAnnotations = false;
this.hasMetadataFileLabels = false;
MessageStore.success('The metadata file was deleted.');
},
selectFile(parser) {
Expand All @@ -86,6 +105,8 @@ export default {
created() {
this.volumeId = biigle.$require('volumes.id');
this.hasMetadata = biigle.$require('volumes.hasMetadata');
this.hasMetadataAnnotations = biigle.$require('volumes.hasMetadataAnnotations');
this.hasMetadataFileLabels = biigle.$require('volumes.hasMetadataFileLabels');
this.parsers = biigle.$require('volumes.parsers');
},
};
Expand Down
4 changes: 3 additions & 1 deletion resources/views/volumes/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
biigle.$declare('volumes.id', {!! $volume->id !!});
biigle.$declare('volumes.annotationSessions', {!! $annotationSessions !!});
biigle.$declare('volumes.type', '{!! $type !!}');
biigle.$declare('volumes.hasMetadata', '{!! $volume->hasMetadata() !!}');
biigle.$declare('volumes.hasMetadata', {!! $volume->hasMetadata() ? 'true' : 'false' !!});
biigle.$declare('volumes.hasMetadataAnnotations', {!! ($volume->hasMetadata() && $volume->getMetadata()->hasAnnotations()) ? 'true' : 'false' !!});
biigle.$declare('volumes.hasMetadataFileLabels', {!! ($volume->hasMetadata() && $volume->getMetadata()->hasFileLabels()) ? 'true' : 'false' !!});
biigle.$declare('volumes.parsers', {!! $parsers !!});
</script>
@mixin('volumesEditScripts')
Expand Down
4 changes: 3 additions & 1 deletion resources/views/volumes/edit/metadata.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@endif
<span class="pull-right">
<span class="loader" v-bind:class="{'loader--active':loading}"></span>
<dropdown tag="span" v-if="hasMetadata" v-cloak>
<dropdown tag="span" v-if="showImportDropdown" v-cloak>
<button class="btn btn-default btn-xs dropdown-toggle" type="button" title="Import annotations or file labels from the metadata file attached to this volume"><i class="fa fa-file-import"></i> Import <span class="caret"></span></button>
<template slot="dropdown">
<li>
Expand All @@ -18,6 +18,8 @@
</li>
</template>
</dropdown>
<button v-cloak v-if="showImportAnnotations" v-on:click.prevent="importAnnotations" class="btn btn-default btn-xs" type="button" title="Import annotations from the metadata file attached to this volume"><i class="fa fa-file-import"></i> Import annotations</button>
<button v-cloak v-if="showImportFileLabels" v-on:click.prevent="importFileLabels" class="btn btn-default btn-xs" type="button" title="Import file labels from the metadata file attached to this volume"><i class="fa fa-file-import"></i> Import annotations</button>
<dropdown tag="span" v-if="hasMetadata" v-cloak>
<button class="btn btn-default btn-xs dropdown-toggle" type="button" title="Manage the metadata file attached to this volume" :disabled="loading"><i class="fa fa-file-alt"></i> Manage file <span class="caret"></span></button>
<template slot="dropdown">
Expand Down
62 changes: 38 additions & 24 deletions tests/php/Http/Controllers/Api/Volumes/MetadataControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,39 @@ public function testStoreImageMetadata()
$csv = new UploadedFile(__DIR__."/../../../../../files/image-metadata.csv", 'image-metadata.csv', 'text/csv', null, true);
$this->beEditor();
// no permissions
$this->postJson("/api/v1/volumes/{$id}/metadata", [
'file' => $csv,
'parser' => ImageCsvParser::class,
])
$this
->postJson("/api/v1/volumes/{$id}/metadata", [
'file' => $csv,
'parser' => ImageCsvParser::class,
])
->assertStatus(403);

$this->beAdmin();
// file required
$this->postJson("/api/v1/volumes/{$id}/metadata", [
'parser' => ImageCsvParser::class,
])
$this
->postJson("/api/v1/volumes/{$id}/metadata", [
'parser' => ImageCsvParser::class,
])
->assertStatus(422);

// parser required
$this->postJson("/api/v1/volumes/{$id}/metadata", [
'file' => $csv,
])
$this
->postJson("/api/v1/volumes/{$id}/metadata", [
'file' => $csv,
])
->assertStatus(422);


$this->postJson("/api/v1/volumes/{$id}/metadata", [
'file' => $csv,
'parser' => ImageCsvParser::class,
])
->assertStatus(200);
$this
->postJson("/api/v1/volumes/{$id}/metadata", [
'file' => $csv,
'parser' => ImageCsvParser::class,
])
->assertStatus(200)
->assertJson([
'has_annotations' => false,
'has_file_labels' => false,
]);

$this->assertSame(ImageCsvParser::class, $this->volume()->fresh()->metadata_parser);

Expand Down Expand Up @@ -126,11 +134,16 @@ public function testStoreVideoMetadataCsv()
$csv = new UploadedFile(__DIR__."/../../../../../files/video-metadata.csv", 'metadata.csv', 'text/csv', null, true);

$this->beAdmin();
$this->postJson("/api/v1/volumes/{$id}/metadata", [
'file' => $csv,
'parser' => VideoCsvParser::class,
])
->assertSuccessful();
$this
->postJson("/api/v1/volumes/{$id}/metadata", [
'file' => $csv,
'parser' => VideoCsvParser::class,
])
->assertSuccessful()
->assertJson([
'has_annotations' => false,
'has_file_labels' => false,
]);

$this->assertSame(VideoCsvParser::class, $this->volume()->fresh()->metadata_parser);

Expand All @@ -148,10 +161,11 @@ public function testStoreMetadataIncorrectEncoding()
$csv = new UploadedFile(__DIR__."/../../../../../files/image-metadata-strange-encoding.csv", 'metadata.csv', 'text/csv', null, true);

$this->beAdmin();
$this->postJson("/api/v1/volumes/{$id}/metadata", [
'file' => $csv,
'parser' => ImageCsvParser::class,
])
$this
->postJson("/api/v1/volumes/{$id}/metadata", [
'file' => $csv,
'parser' => ImageCsvParser::class,
])
->assertStatus(422);
}

Expand Down

0 comments on commit bf13e02

Please sign in to comment.