Skip to content

Commit

Permalink
Set format from source files with new extension
Browse files Browse the repository at this point in the history
  • Loading branch information
arildm committed Nov 17, 2023
1 parent 3ea2bea commit 20d93ed
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ As this project is a user-facing application, the places in the semantic version

### Changed

- A corpus created by upload now gets its format set to match the uploaded files
- A corpus created by upload (not through the creation form) now gets its name set to the id, instead of remaining empty
- Improved sync between source files and settings: set format from files, set name to corpus id

### Fixed

Expand Down
3 changes: 2 additions & 1 deletion src/corpus/createCorpus.composable.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import useMessenger from "@/message/messenger.composable";
import useConfig from "./config/config.composable";
import useSources from "./sources/sources.composable";
import useCorpus from "./corpus.composable";
import { getFilenameExtension } from "@/util";

export default function useCreateCorpus() {
const corpusStore = useCorpusStore();
Expand All @@ -31,7 +32,7 @@ export default function useCreateCorpus() {
if (!corpusId) return;

// Get file extension of first file, assuming all are using the same extension.
const format = files[0]?.name.split(".").pop();
const format = getFilenameExtension(files[0]?.name);

// Create a minimal config.
const config = {
Expand Down
15 changes: 13 additions & 2 deletions src/corpus/sources/SourceUpload.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@

<script setup>
import { computed } from "vue";
import { getFilenameExtension } from "@/util";
import useMessenger from "@/message/messenger.composable";
import useSources from "./sources.composable";
import Filedrop from "./Filedrop.vue";
import useCorpusIdParam from "@/corpus/corpusIdParam.composable";
import UploadSizeLimits from "./UploadSizeLimits.vue";
import useConfig from "../config/config.composable";
const props = defineProps({
fileHandler: {
Expand All @@ -68,13 +70,22 @@ const props = defineProps({
const corpusId = useCorpusIdParam();
const { uploadSources, extensions } = useSources(corpusId);
const { clear, alertError } = useMessenger();
const { config, uploadConfig } = useConfig(corpusId);
const { alertError, clear } = useMessenger();
const extensionsAccept = computed(() =>
extensions.value?.map((ext) => `.${ext}`).join()
);
async function defaultFileHandler(files) {
return uploadSources(files).catch(alertError);
const requests = [uploadSources(files).catch(alertError)];
// Also update format setting in config if needed
const format = getFilenameExtension(files[0]?.name);
if (format != config.value.format) {
requests.push(uploadConfig({ ...config.value, format }));
}
await Promise.all(requests);
}
const fileHandler = props.fileHandler || defaultFileHandler;
Expand Down
3 changes: 2 additions & 1 deletion src/corpus/sources/sources.composable.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import uniq from "lodash/uniq";
import useMinkBackend from "@/api/backend.composable";
import { useCorpusStore } from "@/store/corpus.store";
import useMessenger from "@/message/messenger.composable";
import { getFilenameExtension } from "@/util";

export default function useSources(corpusId) {
const corpusStore = useCorpusStore();
Expand Down Expand Up @@ -38,7 +39,7 @@ export default function useSources(corpusId) {
const extensions = computed(() =>
uniq(
corpusStore.corpora[corpusId]?.sources?.map((source) =>
source.name.split(".").pop()
getFilenameExtension(source.name)
)
)
);
Expand Down
3 changes: 3 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,6 @@ export const getException = (f) => {
}
return undefined;
};

export const getFilenameExtension = (filename) =>
filename.split(".").slice(1).pop() || "";
19 changes: 19 additions & 0 deletions src/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
formatDate,
formatSeconds,
getException,
getFilenameExtension,
pathJoin,
randomString,
setKeys,
Expand Down Expand Up @@ -81,3 +82,21 @@ describe("getException", () => {
expect(exception.message).toBe("Leverpastej");
});
});

describe("getFilenameExtension", () => {
test("one extension", () => {
const filename = "palakpaneer.txt";
const extension = getFilenameExtension(filename);
expect(extension).toBe("txt");
});
test("no extension", () => {
const filename = "marinara";
const extension = getFilenameExtension(filename);
expect(extension).toBe("");
});
test("two extensions", () => {
const filename = "svt.xml.bz2";
const extension = getFilenameExtension(filename);
expect(extension).toBe("bz2");
});
});

0 comments on commit 20d93ed

Please sign in to comment.