Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New API to allow uploaded files to pass through backend service #839

Merged
merged 8 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions NOTICE.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ From the following locations:

<https://github.com/remix-run/react-router>

### @azure/storage-blob

<https://github.com/Azure/azure-sdk-for-js>

### classnames

<https://github.com/JedWatson/classnames>
Expand Down
175 changes: 97 additions & 78 deletions app/backend/app.py

Large diffs are not rendered by default.

99 changes: 99 additions & 0 deletions app/backend/testsuite.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
from fastapi.testclient import TestClient
from dotenv import load_dotenv
import io

dir = current_working_directory = os.getcwd()
# We're running from MAKE file, so we need to change directory to app/backend
Expand Down Expand Up @@ -351,7 +352,105 @@ def test_delete_item():
assert response.status_code == 200
assert response.json() == True

def test_upload_file_one_tag():
with open("test_data/parts_inventory.csv", "rb") as file:
response = client.post(
"/file",
files={"file": ("parts_inventory.csv", file, "text/csv")},
data={"file_path": "parts_inventory.csv", "tags": "test"}
)
assert response.status_code == 200
assert response.json() == {"message": "File 'parts_inventory.csv' uploaded successfully"}


def test_uploadfilenotagsnofolder():
with open("test_data/parts_inventory.csv", "rb") as file:
response = client.post(
"/file",
files={"file": ("parts_inventory.csv", file, "text/csv")},
data={"file_path": "parts_inventory.csv", "tags": ""}
)
print(response.json())
assert response.status_code == 200
assert response.json() == {"message": "File 'parts_inventory.csv' uploaded successfully"}

def test_uploadfiletags():
with open("test_data/parts_inventory.csv", "rb") as file:
response = client.post(
"/file",
files={"file": ("parts_inventory.csv", file, "text/csv")},
data={"file_path": "parts_inventory.csv", "tags": "test,inventory"}
)
print(response.json())
assert response.status_code == 200
assert response.json() == {"message": "File 'parts_inventory.csv' uploaded successfully"}
def test_uploadfilespecificfolder():
with open("test_data/parts_inventory.csv", "rb") as file:
response = client.post(
"/file",
files={"file": ("parts_inventory.csv", file, "text/csv")},
data={"file_path": "Finance/parts_inventory.csv", "tags": "test"}
)
assert response.status_code == 200
assert response.json() == {"message": "File 'parts_inventory.csv' uploaded successfully"}
def test_uploadfilespecificfoldernested():
with open("test_data/parts_inventory.csv", "rb") as file:
response = client.post(
"/file",
files={"file": ("parts_inventory.csv", file, "text/csv")},
data={"file_path": "Finance/new/parts_inventory.csv", "tags": "test"}
)
assert response.status_code == 200
assert response.json() == {"message": "File 'parts_inventory.csv' uploaded successfully"}

def test_upload_file_no_file():
response = client.post(
"/file",
data={"file_path": "parts_inventory.csv", "tags": "test"}
)
assert response.status_code == 422 # Unprocessable Entity

def test_upload_file_large_file():
file_content = b"a" * (10 * 1024 * 1024) # 10 MB file
file = io.BytesIO(file_content)
file.name = "large_parts_inventory.csv"

response = client.post(
"/file",
files={"file": (file.name, file, "text/csv")},
data={"file_path": "large_parts_inventory.csv", "tags": "test"}
)
assert response.status_code == 200
assert response.json() == {"message": "File 'large_parts_inventory.csv' uploaded successfully"}

def test_upload_file_missing_file_path():
with open("test_data/parts_inventory.csv", "rb") as file:
response = client.post(
"/file",
files={"file": ("parts_inventory.csv", file, "text/csv")},
data={"tags": "test"}
)
assert response.status_code == 422 # Unprocessable Entity
def test_upload_file_special_characters_in_file_path():
with open("test_data/parts_inventory.csv", "rb") as file:
response = client.post(
"/file",
files={"file": ("parts_inventory.csv", file, "text/csv")},
data={"file_path": "Finance/@new/parts_inventory.csv", "tags": "test"}
)
assert response.status_code == 200
assert response.json() == {"message": "File 'parts_inventory.csv' uploaded successfully"}

def test_upload_file_long_tags():
with open("test_data/parts_inventory.csv", "rb") as file:
long_tags = ",".join(["tag"] * 1000) # Very long tags string
response = client.post(
"/file",
files={"file": ("parts_inventory.csv", file, "text/csv")},
data={"file_path": "parts_inventory.csv", "tags": long_tags}
)
assert response.status_code == 200
assert response.json() == {"message": "File 'parts_inventory.csv' uploaded successfully"}
# This test requires some amount of data to be present and processed in IA
# It is commented out because processing the data takes time and the test will fail if the data is not processed
# Change the question to a valid question that will produce citations if you want to run this test
Expand Down
1 change: 0 additions & 1 deletion app/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"watch": "tsc && vite build --watch"
},
"dependencies": {
"@azure/storage-blob": "^12.24.0",
"@fluentui/react": "^8.110.7",
"@fluentui/react-icons": "^2.0.195",
"@react-spring/web": "^9.7.1",
Expand Down
17 changes: 0 additions & 17 deletions app/frontend/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import { ChatResponse,
ChatRequest,
BlobClientUrlResponse,
AllFilesUploadStatus,
GetUploadStatusRequest,
GetInfoResponse,
Expand Down Expand Up @@ -64,22 +63,6 @@ export function getCitationFilePath(citation: string): string {
return `${encodeURIComponent(citation)}`;
}

export async function getBlobClientUrl(): Promise<string> {
const response = await fetch("/getblobclienturl", {
method: "GET",
headers: {
"Content-Type": "application/json"
}
});

const parsedResponse: BlobClientUrlResponse = await response.json();
if (response.status > 299 || !response.ok) {
throw Error(parsedResponse.error || "Unknown error");
}

return parsedResponse.url;
}

export async function getAllUploadStatus(options: GetUploadStatusRequest): Promise<AllFilesUploadStatus> {
const response = await fetch("/getalluploadstatus", {
method: "POST",
Expand Down
26 changes: 6 additions & 20 deletions app/frontend/src/components/FolderPicker/FolderPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ import { ITextFieldStyleProps, ITextFieldStyles, TextField } from '@fluentui/rea
import { ILabelStyles, ILabelStyleProps } from '@fluentui/react/lib/Label';
import { IIconProps } from '@fluentui/react';
import { IButtonProps } from '@fluentui/react/lib/Button';
import { ContainerClient } from "@azure/storage-blob";

import { getBlobClientUrl } from "../../api";
import { getFolders } from "../../api";
import styles from "./FolderPicker.module.css";

var allowNewFolders = false;
Expand Down Expand Up @@ -85,26 +84,13 @@ export const FolderPicker = ({allowFolderCreation, onSelectedKeyChange, preSelec

async function fetchBlobFolderData() {
try {
const blobClientUrl = await getBlobClientUrl();
var containerClient = new ContainerClient(blobClientUrl);
const delimiter = "/";
const prefix = "";
var newOptions: IComboBoxOption[] = allowNewFolders ? [] : [
const newOptions: IComboBoxOption[] = allowNewFolders ? [] : [
{ key: 'selectAll', text: 'Select All', itemType: SelectableOptionMenuItemType.SelectAll },
{ key: 'FolderHeader', text: 'Folders', itemType: SelectableOptionMenuItemType.Header }];
for await (const item of containerClient.listBlobsByHierarchy(delimiter, {
prefix,
})) {
// Check if the item is a folder
if (item.kind === "prefix") {
// Get the folder name and add to the dropdown list
var folderName = item.name.slice(0,-1);

newOptions.push({key: folderName, text: folderName});
setOptions(newOptions);
}
}
if (!allowNewFolders) {
const folders = await getFolders();
newOptions.push(...folders.map((folder: string) => ({ key: folder, text: folder })));
setOptions(newOptions);
if (!allowNewFolders) {
var filteredOptions = newOptions.filter(
option =>
(option.itemType === SelectableOptionMenuItemType.Normal || option.itemType === undefined) && !option.disabled,
Expand Down
Loading
Loading