Skip to content

Commit

Permalink
upload file
Browse files Browse the repository at this point in the history
  • Loading branch information
Better-Boy committed Nov 12, 2024
1 parent baf0a22 commit 2b6c2de
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export default class Constants {
/** MindsDB ML Engines endpoint. */
public static readonly BASE_MLENGINES_URI = '/api/handlers/byom';

public static readonly FILES_URI = '/api/files';

public static readonly BASE_CALLBACK_URI = '/cloud/callback/model_status';


Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const Projects = new ProjectsModule.ProjectsRestApiClient(
defaultAxiosInstance,
httpAuthenticator
);
const Tables = new TablesModule.TablesRestApiClient(SQL);
const Tables = new TablesModule.TablesRestApiClient(SQL, defaultAxiosInstance, httpAuthenticator);
const Views = new ViewsModule.ViewsRestApiClient(SQL);
const Jobs = new JobsModule.JobsRestApiClient(SQL);
const MLEngines = new MLEnginesModule.MLEnginesRestApiClient(
Expand Down
14 changes: 14 additions & 0 deletions src/tables/tablesApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,18 @@ export default abstract class TablesApiClient {
* @throws {MindsDbError} - Something went wrong deleting the file.
*/
abstract deleteFile(name: string): Promise<void>;

/**
* Uploads a file to a remote server or storage service.
*
* @param filePath - The local path to the file that needs to be uploaded.
* @param fileName - The name that the file should have on the remote server after the upload.
*
* @returns A promise that resolves when the file has been successfully uploaded.
* The promise does not return any value upon success.
*
* @throws {Error} - If there is an error during the file upload process, the promise is rejected with an error message.
*/
abstract uploadFile(filePath: string, fileName: string, original_file_name ?: string): Promise<void>;

}
67 changes: 66 additions & 1 deletion src/tables/tablesRestApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,38 @@ import Table from './table';
import TablesApiClient from './tablesApiClient';
import mysql from 'mysql';
import { MindsDbError } from '../errors';
import HttpAuthenticator from '../httpAuthenticator';
import { Axios } from 'axios';
import FormData from 'form-data';
import * as fs from 'fs';
import Constants from '../constants';
import { getBaseRequestConfig } from '../util/http';
import path from 'path';

/** Implementation of TablesApiClient that goes through the REST API */
export default class TablesRestApiClient extends TablesApiClient {
/** SQL API client to send all SQL query requests. */
sqlClient: SqlApiClient;

/** Axios instance to send all requests. */
client: Axios;

/** Authenticator to use for reauthenticating if needed. */
authenticator: HttpAuthenticator;

/**
*
* @param {SqlApiClient} sqlClient - SQL API client to send all SQL query requests.
*/
constructor(sqlClient: SqlApiClient) {
constructor(
sqlClient: SqlApiClient,
client: Axios,
authenticator: HttpAuthenticator
) {
super();
this.sqlClient = sqlClient;
this.client = client;
this.authenticator = authenticator;
}

/**
Expand Down Expand Up @@ -101,4 +120,50 @@ export default class TablesRestApiClient extends TablesApiClient {
throw new MindsDbError(sqlQueryResult.error_message);
}
}

private getFilesUrl(): string {
const baseUrl =
this.client.defaults.baseURL || Constants.BASE_CLOUD_API_ENDPOINT;
const filesUrl = new URL(Constants.FILES_URI, baseUrl);
return filesUrl.toString();
}

override async uploadFile(filePath: string, fileName: string, original_file_name ?: string): Promise<void> {
const formData = new FormData();

if(original_file_name)
formData.append('original_file_name', original_file_name);

if (fs.existsSync(filePath)) {
formData.append('file', fs.createReadStream(filePath), {
filename: path.basename(filePath),
contentType: 'multipart/form-data',
});
} else {
console.error('File does not exist:', filePath);
}

// Axios request configuration
const { authenticator, client } = this;

const config = getBaseRequestConfig(authenticator);
const filesUrl = this.getFilesUrl();
config.method = 'PUT';
config.url = `${filesUrl}/${fileName}`;
(config.headers = {
...config.headers,
...formData.getHeaders(),
}),
(config.data = formData);

try {
const uploadFileResponse = await client.request(config);
console.log(JSON.stringify(uploadFileResponse, null, 2));
} catch (error) {
console.error(error);
throw MindsDbError.fromHttpError(error, filesUrl);
}

}

}

0 comments on commit 2b6c2de

Please sign in to comment.