Skip to content

Commit

Permalink
Implemented common upload file method (#29)
Browse files Browse the repository at this point in the history
* added generic upload file method

* remove comments

* fix lint errors

* made file input singular file path, added form-data to package json

* fix lint

* remove comment'

* returning progress as object and no longer rounding

* adding symbolFile as const

* created fileUpload object

* rename to camel case

* added unit tests using default node:test framework for http utils

* resolve merge conflict?

* remove extra file

* address comments

* checking for general enoent comment

* checking for general enoent comment

* fix lint

* updated enoent tests

* fix lint
  • Loading branch information
tonzhan2 authored Nov 8, 2024
1 parent b9ea3fc commit 6aa6c1d
Show file tree
Hide file tree
Showing 4 changed files with 309 additions and 4 deletions.
105 changes: 101 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
"author": "",
"license": "Apache-2.0",
"dependencies": {
"axios": "^1.7.7",
"chalk": "^4.1.2",
"commander": "^12.1.0",
"form-data": "^4.0.1",
"ora": "^5.4.1",
"xml2js": "^0.6.2"
},
Expand Down
68 changes: 68 additions & 0 deletions src/utils/httpUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright Splunk Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import axios from 'axios';
import fs from 'fs';
import FormData from 'form-data';

interface FileUpload {
filePath: string;
fieldName: string;
}

interface UploadOptions {
url: string;
file: FileUpload;
parameters: { [key: string]: string | number };
onProgress?: (progressInfo: { progress: number; loaded: number; total: number }) => void;
}

export interface ProgressInfo {
progress: number;
loaded: number;
total: number;
}

// This uploadFile method will be used by all the different commands that want to upload various types of
// symbolication files to o11y cloud. The url, file, and additional parameters are to be prepared by the
// calling method. Various errors, Error, axiosErrors and all should be handled by the caller of this method.
// Since the API contracts with the backend are not yet determined. This is subject to change

export const uploadFile = async ({ url, file, parameters, onProgress }: UploadOptions): Promise<void> => {
const formData = new FormData();

formData.append(file.fieldName, fs.createReadStream(file.filePath));

for (const [ key, value ] of Object.entries(parameters)) {
formData.append(key, value);
}

const fileSizeInBytes = fs.statSync(file.filePath).size;

await axios.post(url, formData, {
headers: {
...formData.getHeaders(),
},
onUploadProgress: (progressEvent) => {
const loaded = progressEvent.loaded;
const total = progressEvent.total || fileSizeInBytes;
const progress = (loaded / total) * 100;
if (onProgress) {
onProgress({ progress, loaded, total });
}
},
});
};
Loading

0 comments on commit 6aa6c1d

Please sign in to comment.