Other Notes
Notes of a misc. nature.
How Relative Storage Path Works:
To store a file in a new path, you need to pass the storage path in the file's metadata. This is acomplished in two parts, the client side and server side.
On the client you need to do the following:
- Create a meta object for the file object if it doesn't already exist.
- Teradata's File object does not carry any metadata
- Add a field to this meta object called "storagePath" or whatever you like with the string you want to pass to the server.
- Pass this metadata object through the "insert()" config
- Start Upload
<1,2>
if(this.currentFile.meta && !this.currentFile.meta.storagePath && this.storagePath){
this.currentFile.meta.storagePath = this.storagePath;
} else if(!this.currentFile.meta){
this.currentFile.meta = {storagePath:this.storagePath};
}
...
this.currentUpload = this.currentCollection.insert({
file:this.currentFile,
...
<3> meta: this.currentFile.meta
});
...
<4> this.currentUpload.start();
Then, on the server side, you need to config your storagePath funciton from the FilesCollection cofig to take this metadata and return a valid path. You can do whatever you want at this point, but you have to keep the user informed about what you want to do. Here, we take the storage path and assume it to be a relative path, so we append it to the rootpath if the metadata exists.
storagePath: function(fileObj):string{
let toSavePath:string = homeFolder;
if(fileObj && fileObj.meta && fileObj.meta.storagePath){
toSavePath += fileObj.meta.storagePath;
}
return toSavePath;
}
A few notes:
- First, this method will be called at initalization, so be aware you have to deal with the case that fileObj is null, as well as the expected case of fileObj.meta being null.
- This method is called every time a single chunk is sent to the server, meaning large files will call this method hundreds of times. As a result, keep work to a minimum in this function.
- If you return a path that DNE, ostrio files will go through the whole process of uploading the file and then hang, as it throws an error on a seperate fibber.
- To Fix this, you can use fs-extra 's mkdirp() method to check and make a path.
- Consider using node's path.normalize() if you do not want to worry about trailing and leading slashes.
- You don't even need to use meta for this method to work. Since you are given a fileObj, you can use any parameter as an input. Examples include id, name, and userid.