Skip to content

Commit

Permalink
chore: project assets for text files are now documents (not artifacts) (
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom-Szendrey authored Oct 11, 2023
1 parent 4e13711 commit bb724b9
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,23 @@
:active="documentView === DocumentView.EXRACTIONS"
/>
<Button
v-if="documentType === DocumentView.PDF"
class="p-button-secondary p-button-sm"
label="PDF"
icon="pi pi-file"
:loading="!pdfLink"
@click="documentView = DocumentView.PDF"
:active="documentView === DocumentView.PDF"
/>
<Button
v-if="documentType === DocumentView.TXT"
class="p-button-secondary p-button-sm"
label="txt"
icon="pi pi-file"
:loading="!pdfLink"
@click="documentView = DocumentView.TXT"
:active="documentView === DocumentView.TXT"
/>
</span>
</template>
<template #info-bar>
Expand Down Expand Up @@ -106,6 +116,7 @@
:pdf-link="pdfLink"
:title="doc.name || ''"
/>
<code-editor v-else-if="documentView === DocumentView.TXT" :initial-code="code" />
</tera-asset>
</template>

Expand All @@ -121,13 +132,19 @@ import TeraPdfEmbed from '@/components/widgets/tera-pdf-embed.vue';
import { DocumentAsset } from '@/types/Types';
import * as textUtil from '@/utils/text';
import TeraAsset from '@/components/asset/tera-asset.vue';
import { downloadDocumentAsset, getDocumentAsset } from '@/services/document-assets';
import {
downloadDocumentAsset,
getDocumentAsset,
getDocumentFileAsText
} from '@/services/document-assets';
import Image from 'primevue/image';
import TeraShowMoreText from '@/components/widgets/tera-show-more-text.vue';
import CodeEditor from '@/page/project/components/code-editor.vue';

enum DocumentView {
EXRACTIONS = 'extractions',
PDF = 'pdf'
PDF = 'pdf',
TXT = 'txt'
}

const props = defineProps<{
Expand All @@ -140,6 +157,7 @@ const props = defineProps<{
const doc = ref<DocumentAsset | null>(null);
const pdfLink = ref<string | null>(null);
const documentView = ref(DocumentView.EXRACTIONS);
const code = ref<string>();

const docLink = computed(() =>
doc.value?.fileNames && doc.value.fileNames.length > 0 ? doc.value.fileNames[0] : null
Expand All @@ -155,6 +173,13 @@ const equations = computed(
() => doc.value?.assets?.filter((asset) => asset.assetType === 'equation') || []
);

const documentType = computed(() => {
if (doc.value?.fileNames?.at(0)?.endsWith('.pdf')) {
return DocumentView.PDF;
}
return DocumentView.TXT;
});

const emit = defineEmits(['close-preview', 'asset-loaded']);

// Highlight strings based on props.highlight
Expand All @@ -165,13 +190,21 @@ function highlightSearchTerms(text: string | undefined): string {
return text ?? '';
}

async function openTextDocument() {
const filename: string | undefined = doc.value?.fileNames?.at(0);
const res: string | null = await getDocumentFileAsText(props.assetId!, filename!);
if (!res) return;
code.value = res;
}

watch(
() => props.assetId,
async () => {
if (props.assetId) {
const document = await getDocumentAsset(props.assetId);
if (document) {
doc.value = document;
openTextDocument();
}
} else {
doc.value = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,16 @@ import Button from 'primevue/button';
import { AcceptedTypes, AcceptedExtensions } from '@/types/common';
import { uploadCodeToProject } from '@/services/code';
import { useProjects } from '@/composables/project';
import { Artifact, AssetType, Dataset } from '@/types/Types';
import { uploadArtifactToProject } from '@/services/artifact';
import { DocumentAsset, AssetType, Dataset } from '@/types/Types';
import { uploadDocumentAssetToProject } from '@/services/document-assets';
import { createNewDatasetFromCSV } from '@/services/dataset';
import useAuthStore from '@/stores/auth';
import { ref } from 'vue';
import TeraDragAndDropImporter from '@/components/extracting/tera-drag-n-drop-importer.vue';
import InputText from 'primevue/inputtext';
import { useToastService } from '@/services/toast';
import TeraImportGithubFile from '@/components/widgets/tera-import-github-file.vue';
import { extractPDF } from '@/services/knowledge';
defineProps<{
visible: boolean;
Expand All @@ -90,7 +91,7 @@ async function processFiles(files: File[], csvDescription: string) {
case AcceptedTypes.PDF:
case AcceptedTypes.TXT:
case AcceptedTypes.MD:
return processArtifact(file);
return processDocument(file);
case AcceptedTypes.PY:
case AcceptedTypes.R:
case AcceptedTypes.JL:
Expand All @@ -115,15 +116,23 @@ async function processCode(file: File) {
* Process a pdf, txt, md file into an artifact
* @param file
*/
async function processArtifact(file: File) {
async function processDocument(file: File) {
// This is pdf, txt, md files
const artifact: Artifact | null = await uploadArtifactToProject(
const document: DocumentAsset | null = await uploadDocumentAssetToProject(
file,
useProjects().activeProject.value?.username ?? '',
'',
progress
);
return { id: artifact?.id ?? '', assetType: AssetType.Artifacts };
let newAsset;
if (document && document.id) {
newAsset = await useProjects().addAsset(AssetType.Documents, document.id);
}
if (document && newAsset && file.name.toLowerCase().endsWith('.pdf')) {
await extractPDF(document);
return { file, error: false, response: { text: '', images: [] } };
}
return { file, error: true, response: { text: '', images: [] } };
}
/**
Expand Down
17 changes: 16 additions & 1 deletion packages/client/hmi-client/src/services/document-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,25 @@ async function downloadDocumentAsset(documentId: string, fileName: string): Prom
}
}

async function getDocumentFileAsText(documentId: string, fileName: string): Promise<string | null> {
const response = await API.get(
`/document-asset/${documentId}/download-document-as-text?filename=${fileName}`,
{}
);

if (!response || response.status >= 400) {
logger.error('Error getting document file as text');
return null;
}

return response.data;
}

export {
getAll,
getDocumentAsset,
uploadDocumentAssetToProject,
downloadDocumentAsset,
createNewDocumentFromGithubFile
createNewDocumentFromGithubFile,
getDocumentFileAsText
};
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import software.uncharted.terarium.hmiserver.proxies.dataservice.DocumentProxy;
import software.uncharted.terarium.hmiserver.proxies.jsdelivr.JsDelivrProxy;
import org.apache.http.entity.StringEntity;
import org.apache.commons.io.IOUtils;
import java.nio.charset.StandardCharsets;

import java.io.IOException;
import java.util.List;
Expand Down Expand Up @@ -149,4 +151,30 @@ public ResponseEntity<byte[]> downloadDocument(
return ResponseEntity.internalServerError().build();
}
}

@GetMapping("/{id}/download-document-as-text")
public ResponseEntity<String> getDocumentFileAsText(@PathVariable("id") String documentId, @RequestParam("filename") String filename) {

log.debug("Downloading document file {} for document {}", filename, documentId);

try (CloseableHttpClient httpclient = HttpClients.custom()
.disableRedirectHandling()
.build()) {

PresignedURL presignedURL = proxy.getDownloadUrl(documentId, filename).getBody();
final HttpGet httpGet = new HttpGet(presignedURL.getUrl());
final HttpResponse response = httpclient.execute(httpGet);
final String textFileAsString = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8);

return ResponseEntity.ok(textFileAsString);


} catch (Exception e) {
log.error("Unable to GET document data", e);
return ResponseEntity.internalServerError().build();
}

}


}

0 comments on commit bb724b9

Please sign in to comment.