Sonarqube Change this code to not construct the path from user-controlled data - java-8

User uploads file from UI and at springboot i check if the file exists and if yes, delete the file or copy the file to File server. I need to save the file with the file name what user has uploaded.
But Sonarqube is giving vulnerability error: "Change this code to not construct the path from user-controlled data."
private String processFileUpload(MultipartFile uploadedFile){
String filePathName = "/uploaded_files/"+uploadedFile.getOriginalFilename(); //Error at this line
File file = new File(uploadedFile.getOriginalFilename());
if (file.exists()) {
file.delete();
}
//file transfer code
}
I need the file to be stored with the same name user has uploaded. How to fix this sonarqube error?

Related

create a CSV file in ADLS from databricks

I am creating a CSV file in an ADLS folder.
For example: sample.txt is the file name
instead of a single file, I see sample.txt/..,part-000 files.
My question is is there a method to create sample.txt file instead of a directory in pyspark.
df.write() or df.save() both create folders and multiple files inside that directory.
Using Coalesce(1) I can combine multiple part-000 files into one file. but how to create a single csv file?
Unfortunately, Spark doesn’t support creating a data file without a folder
To workaround,
Firstly using coalesce or repartition, create a single part (partition) file.
df\
.coalesce(1)\
.write\
.format("csv")\
.mode("overwrite")\
.save("mydata")
The above example produces an mydata directory, a part-000* file, and hidden files
However, our data is contained in only one CSV file. The name of this file is not user-friendly. We can rename this file and extract it.
data_location = "/mydata.csv/"
files = dbutils.fs.ls(data_location)
csv_file = [x.path for x in files if x.path.endswith(".csv")][0]
dbutils.fs.mv(csv_file, data_location.rstrip('/') + ".csv")
dbutils.fs.rm(data_location, recurse = True)
set up account key and configure the storage account to access. then move file from databricks location to adls. To move file, we use dbutils.fs.mv
```python
storage_account_name = "Storage account name"
storage_account_access_key = "storage account acesss key"
spark.conf.set("fs.azure.account.key."+storage_account_name+".blob.core.windows.net",storage_account_access_key)
dbutils.fs.cp('/mydata.csv.csv','abfss://demo12#pratikstorage1.dfs.core.windows.net//mydata1.csv')
My Execution:
Output:

Flutter How to move file

I will move certain files. how to move the image file from directory to another directory,
example file img.jpg from /storage/emulated/0/Myfolder to /storage/emulated/0/Urfolder?
File.rename works only if source file and destination path are on the same file system, otherwise you will get a FileSystemException (OS Error: Cross-device link, errno = 18). Therefore it should be used for moving a file only if you are sure that the source file and the destination path are on the same file system.
For example when trying to move a file under the folder /storage/emulated/0/Android/data/ to a new path under the folder /data/user/0/com.my.app/cache/ will fail to FileSystemException.
Here is a small utility function for moving a file safely:
Future<File> moveFile(File sourceFile, String newPath) async {
try {
// prefer using rename as it is probably faster
return await sourceFile.rename(newPath);
} on FileSystemException catch (e) {
// if rename fails, copy the source file and then delete it
final newFile = await sourceFile.copy(newPath);
await sourceFile.delete();
return newFile;
}
}
await File('/storage/emulated/0/Myfolder').rename('/storage/emulated/0/Urfolder')
If the files are on different file systems you need to create a new destination file, read the source file and write the content into the destination file, then delete the source file.

Adding output files to an existing output directory in Mapreduce

I want to add output files of a map reduce program to the same directory every time I run the job by appending time stamp at the end of file name.
Currently i am able append the time stamp at the end of file output file, but I am unable to find out how to add files to the same output directory instead of overwriting it every time.
You can write output files in temporary folder and move them to target folder after the end of job. Example of a method that moves all files from one folder to another:
public static void moveFiles(Path from, Path to, Configuration conf) throws IOException {
FileSystem fs = from.getFileSystem(conf); // get file system
for (FileStatus status : fs.listStatus(from)) { // list all files in 'from' folder
Path file = status.getPath(); // get path to file in 'from' folder
Path dst = new Path(to, file.getName()); // create new file name
fs.rename(file, dst); // move file from 'from' folder to 'to' folder
}
}
The output can be controlled by using reduce method. I guess you can try out a logic in reducer.
Please note that number of reducers = number of output files.

Visual Studio Setup Project - How to Obtain the Directory Path from a File-Search Launch Condition

I am looking for a way to add File(s) to an existing directory that has a random name as part of a Visual Studio Setup Project and I hoped someone might be able to help me solve this puzzle please.
I have been attempting to obtain the discovered path property of the directory using a Launch Condition; Unfortunately this method returns the full file path including the filename, which cannot be used as a directory property.
The directory in question takes the form [AppDataFolder]Company\Product\aaaaaaaaaaaa\
where aaaaaaaaaaaa is a random installation string.
Within the Launch Condition Setup I check for the directory's existence by searching for a file that would appear inside it,
Search Target Machine
(Name): File marker
Filename: sample.txt
Folder: [AppDataFolder]Company\Product\
Property: DIRFILE
Launch Condition
(Name): File marker exists
Condition: DIRFILE
In the Setup Project I add the file I wish to insert, with the details
Condition: DIRFILE
Folder: 'Installation folder'
Then in File System Setup I add a new folder entry for the random directory aaaaaaaaaaaa
(Name): Installation folder
Condition: DIRFILE
DefaultLocation: [DIRFILE]\..\ *Incorrect*
Property [DIRLOCATION]
As you can see the installer detects the existence of the marker file but, instead of placing my file at the same location, when using [DIRFILE] the installer would incorrectly try and insert it INTO the file;
This is because the file path was returned
[AppDataFolder]Company\Product\aaaaaaaaaaaa\sample.txt
where I instead need the directory path
[AppDataFolder]Company\Product\aaaaaaaaaaaa
Therefore I was wondering if it was possible to return the directory the file was found in from Search Target Machine (as opposed to the file location of the file), if I could extract the directory path by performing a string replace of the filename on the file location DIRFILE within the DefaultLocation field in File System Setup, or if perhaps there is even another method I am missing?
I'm also very interested in a simple solution for this, inside the setup project.
The way I did solve it was to install the files to a temporary location and then copy them to the final location in an AfterInstall event handler. Not a very elegant solution! Since it no longer care about the user selected target path I removed that dialog. Also I needed to take special care when uninstalling.
public override void OnAfterInstall(IDictionary savedState)
{
base.OnAfterInstall(savedState);
// Get original file folder
string originDir = Context.Parameters["targetdir"];
// Get new file folder based on the dir of sample.txt
string newDir = Path.GetDirectoryName(Context.Parameters["dirfile"]);
// Application executable file name
// (or loop for all files on the path instead)
string filename = "ApplicationName.exe";
// Move or copy the file
File.Move(Path.Combine(originDir, filename), Path.Combine(newDir, filename)));
}

On downloading a file from a AJAX PAGE the file has same name as the ajax page downloading it

*Hi,
I am trying to write a Ajax page which decrypts an encrypted file on the server and downloads it. Now my code decrypts and downloads the file. But the problem is that the downloaded file has the same name as that of the Ajax Page.
Below is the code which downloads the file to the local machine. I have not included the decryption code in-order to avoid confusion.*
String fileName="/home/maclean/NetBeansProjects/SecureCloud/build/web/SecureCloud/"+Data.txt;
This line declares the files location on the server
response.setContentType("APPLICATION/DOWNLOAD");
response.setContentLength(in.available());
int length;
byte[] buffer = new byte[4096];
while ((length = in.read(buffer)) > 0){
data=new String(buffer);
buffer =decryptCaesar.Decrypt(data,shiftKey).getBytes();
os.write(buffer, 0, buffer.length);
System.out.println(buffer);
}
response.setHeader("Content-Disposition","attachment; filename="+FileName);
and this line downloads the file. But the downloaded file has the name AjaxRead.jsp which is the name of the Ajax page Where as the original name of the file is Data.txt
Do let me know what I am doing wrong and how to get the same file name.
Since you don't specify a file name for the download, the browser chooses one (based on the current URL). You can override the file name by setting the HTTP header Content-Disposition as:
response.setHeader("Content-Disposition","attachment; filename=Data.txt").

Resources