-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented common upload file method (#29)
* 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
Showing
4 changed files
with
309 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
}, | ||
}); | ||
}; |
Oops, something went wrong.