Получить дату из изображений - PullRequest
0 голосов
/ 02 мая 2018

Я создаю приложение для загрузки изображений, и у меня возникают проблемы, когда мне нужно узнать дату, когда было загружено изображение. Изображения будут загружены случайным образом, поэтому дата должна быть равна дате загрузки. Я работаю приложение с электронным и для загрузки изображений я использую Multer.

Вот мой код для галереи изображений, куда я загружаю изображения в галерею изображений. Если у кого-то есть идеи, как это должно быть, я был бы благодарен за ответ.

// Image gallery
    app.get('/gallery', (req, res) => {
    let images = getImagesFromDir(path.join(__dirname, 'public/uploads'));
    let time = new Date(images.lastModified); //here I'm trying to get date
    res.render('gallery', { title: 'Galerija slik', images: images, time: time })
});

// dirPath: target image directory
function getImagesFromDir(dirPath) {

    // All iamges holder, defalut value is empty
    let allImages = [];

    // Iterator over the directory
    let files = fs.readdirSync(dirPath);

    // Iterator over the files and push jpg and png images to allImages array.
    for (file of files) {
        let fileLocation = path.join(dirPath, file);
        var stat = fs.statSync(fileLocation);
        if (stat && stat.isDirectory()) {
            getImagesFromDir(fileLocation); // process sub directories
        } else if (stat && stat.isFile() && ['.jpg', '.png'].indexOf(path.extname(fileLocation)) != -1) {
            allImages.push('uploads/'+file); // push all .jpf and .png files to all images 
        }
    }

    // return all images in array formate
    return allImages;
}

А это мой HTML-код

      <div id="content">

        <h2 class="text-center"><%= title %></h2>

    <div id="filters">
        <a href="#" id="newer" class="selected">Newer</a>
        <a href="#" id="older">Older</a>
    </div>
    <div id="posts">

        <% for(let image of images) {%>
            <article class="post">
                <figure>
                    <a href="<%= image %>" data-fancybox="1"><img src="<%= image %>" /></a>
                </figure>
                <time><%= time %></time>
        </article>
          <% } %>

    </div>

1 Ответ

0 голосов
/ 04 мая 2018

Я предполагаю, что вы хотели получить дату последнего изменения каждого изображения.

// Image gallery
    app.get('/gallery', (req, res) => {
    let images = getImagesFromDir(path.join(__dirname, 'public/uploads'));
    res.render('gallery', { title: 'Galerija slik', images: images })
});

// dirPath: target image directory
function getImagesFromDir(dirPath) {

    // All iamges holder, defalut value is empty
    let allImages = [];

    // Iterator over the directory
    let files = fs.readdirSync(dirPath);

    // Iterator over the files and push jpg and png images to allImages array.
    for (file of files) {
        let fileLocation = path.join(dirPath, file);
        var stat = fs.statSync(fileLocation);
        if (stat && stat.isDirectory()) {
            getImagesFromDir(fileLocation); // process sub directories
        } else if (stat && stat.isFile() && ['.jpg', '.png'].indexOf(path.extname(fileLocation)) != -1) {
            allImages.push({path: 'uploads/'+file, lastModifiedDate: stat.mtime}); // push all .jpf and .png files to all images 
        }
    }

    // return all images in array formate
    return allImages;
}

И HTML должен быть обновлен, как показано ниже.

<div id="posts"> <% for(let image of images) {%> <article class="post"> <figure> <a href="<%= image.path %>" data-fancybox="1"><img src="<%= image.path %>" /></a> </figure> <time><%= image.lastModifiedDate %></time> </article> <% } %> </div>
...