Loop through all files/images in a folder with Angular - javascript

I have an app with some products and each product has a gallery with a different amount of images. Each of the images has a name that is completely random / no correlation with the other image names.
Each of the product images are in /src/assets/images/products/:id/.
I need to add the paths to a gallery component but I can't loop through them because the names are random. Is there any way to just loop through each file from a folder using only Angular? If not can I do it on the back-end without renaming the files? I'm also running the app on a Node.js back-end if that matters.

You can't do that with frontend.
What you need to is using your back-end and return file in it.
You are using NodeJs as back-end so can use the fs.readdir or fs.readdirSync methods.
fs.readdir
const testFolder = './images/';
const fs = require('fs');
fs.readdir(testFolder, (err, files) => {
files.forEach(file => {
console.log(file); // use those file and return it as a REST API
});
})
fs.readdirSync
const testFolder = './images/';
const fs = require('fs');
fs.readdirSync(testFolder).forEach(file => {
console.log(file);
})
Read the full documenation, it may help you to how you can proceed.

Related

Google Cloud Storage pushing the files into bucket very slowly

I'm using the below code to recursively get all the paths in the folder and push them one by one into the google cloud storage bucket. The problem is, it's extremely slow. I have around 30-40K files that need to be pushed every day and each one is taking like 0.25 to 0.5 second to push. Is there any way I could push them all together? In bulk? Or another way that makes it way faster?
const {Storage} = require('#google-cloud/storage');
const fs = require('fs');
const path = require('path');
function getAllFilesInDirectoryRecursively(dir){
const files = fs.readdirSync(dir, {withFileTypes: true});
for (const file of files) {
if (file.isDirectory()) {
yield* getAllFilesInDirectoryRecursively(path.join(dir, file.name));
} else {
yield path.join(dir, file.name);
}
}
}
const storage = new Storage();
(async function(){
for (let filePath of getAllFilesInDirectoryRecursively('./main/')) {
await storage.bucket('bucket.domain.com').upload('./' + filePath, {
destination: filePath.replace('main', ''),
});
}
})()
You can use gsutil to upload contents of a directory using:
gsutil cp -r ./main gs://bucket-name
To run this command periodically, you can use a cron job or run the command from NodeJS script after the files have been generated.
For a solution without gsutil, it might be better to use Promise.all() instead of running all upload promises individually.

I want to check if a file having .mp4 extension exist in my folder using node js

So the thing is that, I'm storing files of other guys on my server that are mp4 but I need to make sure to delete that later on but sometimes the files remain.
So I want to do something that will detect if any .mp4 file is found in the folder if yes then I can remove it using unlinkSync.
I don't know the filename, that's why I want the system to figure it out
Hope you guys can help?
You can use readDir to read all filenames in a folder. Afterwards you can check the file ending:
const testFolder = './tests/';
const fs = require('fs');
fs.readdir(testFolder, (err, files) => {
files.forEach(file => {
console.log(file.includes(".mp4"));
});
});

Read files from a directory with a given path in JS

Is it possible to return the contents of a static path to a directory instead of using an .
I want to write a script that reads the contents of a directory on the file system to a given time daily. This is integrated in a webapp I can't edit.
Short answer: you can't.
You need to do this server-side. Here is an answer from a similar question, using node.js.
You can use the fs.readdir or fs.readdirSync methods.
fs.readdir
const testFolder = './tests/';
const fs = require('fs');
fs.readdir(testFolder, (err, files) => {
files.forEach(file => {
console.log(file);
});
});
fs.readdirSync
const testFolder = './tests/';
const fs = require('fs');
fs.readdirSync(testFolder).forEach(file => {
console.log(file);
});
The difference between the two methods, is that the first one is asynchronous, so you have to provide a callback function that will be executed when the read process ends.
The second is synchronous, it will return the file name array, but it will stop any further execution of your code until the read process ends.

Get List of all server files in a directory in Node.js

Any one here can help me out to rite a code to fetch List of all server files in a directory using Node.js
Just like it is done locally for system file
const testFolder = './models/';
const fs = require('fs');
fs.readdir(testFolder, (err, files) => {
files.forEach(file => {
console.log(file);
});
});
You can try this code to read all files present inside a directory.
fs.readdirSync(`foldername`).forEach(function (file) {
console.log(file);
});

How to get files contained in a path

In the following path:
app/test/vehicle/
there are several .js file. Is there any way in Javascript where I can get the number of .js file contains in the above-mentioned path?
I am using webStorm environment
Yes, using node:
const fs = require('fs');
const dir = './directory';
fs.readdir(dir, (err, files) => {
console.log(files.length);
});
https://stackoverflow.com/a/43747896/3650835
Since javascript doesn't have local filesystem access when run in the browser, there is no way to access local files from javascript. If you are asking about accessing all files hosted on a cdn or something similar in that directory, there is still no way to tell which requests will return a 20x.
If you are using nodejs, use the fs.readdir function:
const fs = require('fs');
fs.readdir('app/test/vehicle/', (err, files) => {
files.forEach(file => { console.log(file);} );
})
There is no way to access the file system from a web page.
If you are using nodejs, then you can use fs.readdir or fs.readdirSync:
const fs = require('fs');
const dir = './directory';
fs.readdir(dir, (err, files) => {
console.log(files.filter(file => file.endsWith(".js")).length);
});
Where are you trying to get list of the files? On the client side it is impossible. JavaScript does not have access to the client file system. If you are trying to do it in the server side using for example node.js then you can do something like:
var filesys = require('fs');
var files = filesys.readdirSync('/app/test/vehicle/');

Categories