nodejs fs createWriteStream save multiple files with same name - node.js

How to store multiple files with same name and extension using fs.
fs.createWriteStream(`${dir}${file.filename}`)
What I want is:
upload image.jpg -> uploaded image.jpg
upload image.jpg -> uploaded image-2.jpg
upload image.jpg -> uploaded image-3.jpg
Currently I can not upload new file with same name

Related

Renaming the folder zip file extracts to

Might just be an edge case but I extracted a zip file to a directory using the zip file module. When extracting, zip file names the directory it extracts to.
If there is a way I get to specify the name of the folder Zip file creates to extract the files to? I am hitting an error because I am using the same folder zipped up to test zip file and it keeps using the old folder name which already exists so it throws an error. Here is my code:
orginalFolderName = jobFolder + name
with zipfile.ZipFile(directory,"r") as zip_ref:
zip_ref.extractall(jobFolder)
os.rename(orginalFolderName, newFoldername)
directory = newFoldername
with zipfile.ZipFile(filepath) as z:
z.extractall(dest_folder)
filepath - Complete path of zipfile
dest_folder - destination folder

How can I generate a PDF with custom fonts using AWS Lambda?

I have an AWS Lambda function that generates PDFs using the html-pdf library with custom fonts.
At first, I imported my fonts externally from Google Fonts, but then the PDF's size has enlarged by ten times.
So I tried to import my fonts locally src('file:///var/task/fonts/...ttf/woff2') but still no luck.
Lastly, I trie to create fonts folder in the main project and then I added all of my fonts, plus the file fonts.config:
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>/var/task/fonts/</dir>
<cachedir>/tmp/fonts-cache/</cachedir>
<config></config>
</fontconfig>
and set the following env:
FONTCONFIG_PATH = /var/task/fonts
but still no luck (I haven't installed fontconfig since I'm not sure how and if I need to).
My Runtime env is Node.js 8.1.0.
You can upload your fonts into an S3 bucket and then download them to the lambda's /tmp directory, during its execution. In case your lib creates .pkl files, you should first change your root directory to /tmp (lambda is not allowed to write in the default root directory).
The following Python code downloads your files from a /fonts directory in an S3 bucket to /tmp/fonts "local" directory.
import os
import boto3
os.chdir('/tmp')
os.mkdir(os.path.join('/tmp/', 'fonts'))
s3 = boto3.resource('s3')
s3_client = boto3.client('s3')
my_bucket = s3.Bucket("bucket_name")
for file in my_bucket.objects.filter(Prefix="fonts/"):
filename = file.key
short_filename = filename.replace('fonts/','')
if(len(short_filename) > 0):
s3_client.download_file(
bucket,
filename,
"/tmp/fonts/" + short_filename,
)

Downloading all files and folders from S3 bucket to local folder in node js

I need to download all files and folders from an s3 bucket to a local folder in sam hierarchy.
Suppose my s3 bucket basepath is: myBucket/user1/
in which and user1 may have dynamic values. Every folder for users contains 3 sub folders image, audio, video and files in them.
i just want to download all files in folder user1 to local file system in paths
/assets/s3files/user1/<FILES & FOLDERS HERE>
i have tried: skipper-better-s3, but it reads one the file we have passed.
adapter = require('skipper-better-s3')({
key: 'xxxxxxxxx',
secret: 'yyyyyyyyyyy',
bucket: 'myBucket'
});
adapter.read('/assets/s3files/user1/image/file1.jpg', (err, data) => {
// data is now a Buffer containing the avatar
});

Adm zip write zip buffer to ExpressJS response

Hi I'm trying to send a zip buffer made by Adm Zip npm module to my response for client download.
I manage to download the zip file but unable to expand it. OSX says "error 2 No such file or directory"...
The downlaoded zip file has got the right size I believe and is sent over this way:
var zip = new AdmZip();
// added files with zip.addFile(...)
var zipFile = zip.toBuffer();
res.contentType('zip');
res.write(zipFile);
res.end();
Any idea what could be wrong?
Thanks
Apparently it comes from the Adm-zip code base and hasn't been merged yet:
https://github.com/cthackers/adm-zip/compare/master...mygoare:unzipErr

how to unzip file and save into particaular folder

i am trying to unzip a zip file and save into target path
i tried these thing
var zip = new AdmZip(x);
zip.extractAllTo(/target path,false);
and then
fs.createReadStream(path/to/arch.zip).pipe(unzip.Extract({ path: "targetpath"}));
it is extracting the zip file and save that unzip file into target path.it is fine.
but if i upload the two zip file(it contain same name),it is overwrite that folder.
for example
first if i upload image.zip ,it will be extracted and it will be stored into images(target folder)
now images folder contain image folder.
again if i upload image.zip,it will be extracted and it will be overwrite image folder
so images folder contain again one image folder.
but if i upload image1.zip file , the images folder contain image and image1 folder.
so how to save even the folder contain the same name.

Resources