简体   繁体   English

Node.js-尝试使用Exiftool编辑图像的元数据

[英]Nodejs - trying to edit images' metadata with Exiftool

I am currently working on a NodeJS (Express) project to edit images' metadata with Exiftool. 我目前正在使用NodeJS(Express)项目来使用Exiftool编辑图像的元数据。 To edit images' metadata with Exiftool, I've to create a JSON file containing all metadata to modify then execute the command : 要使用Exiftool编辑图像的元数据,我必须创建一个包含所有要修改的元数据的JSON文件,然后执行以下命令:

exiftool -j=metadata.json pathToTheImage/image.jpg

The json file must look like that : json文件必须如下所示:

[{"SourceFile":"pathToTheImage/image.jpg","XMP-dc:Title":"Image's title"}]

Here's my code to do that : 这是我的代码:

const {exec} = require('child_process');
let fs = require('fs');
let uploadPath = "uploads";
let uploadName = "image.jpg";

...

app.post('/metadata/editor', (req, res) => {
    let jsonToImport = [...];
    fs.writeFileSync("metadata.json", JSON.stringify(jsonToImport));
    exec('exiftool -j=metadata.json ' + uploadPath + '/' + uploadName, (error, stdout, stderr) => {
        if (error) {
            console.error(error);
            return;
        }
        res.redirect('/metadata/checker/' + uploadName);
    });
});

The problem is at the level of "writeFileSync/exec". 问题在“ writeFileSync / exec”级别。 Independently these two lines work well, that's to say that if I've just the first line, the JSON file is well created. 这两行独立运行良好,也就是说,如果我只有第一行,那么就可以很好地创建JSON文件。 And if I've just the second ligne, image's metadata are well updated. 如果我只是第二个ligne,那么图像的元数据将得到很好的更新。 But when I execute this two lines together, the JSON file is well created but the exec line do "nothing" (or something that I can't determine). 但是,当我一起执行这两行时,JSON文件创建良好,但是exec行“什么也没有”(或我无法确定的东西)。 This code uses synchronous functions, I've test it with asynchronous functions, this is the same behavior. 这段代码使用了同步功能,我已经用异步功能对其进行了测试,这是相同的行为。

Currently, to do what I need, I must execute the code above to create the JSON file, then I must comment the writeFileSync line and I must reexecute the code to update image's metadata correctly. 当前,要做我需要做的事情,我必须执行上面的代码来创建JSON文件,然后必须注释writeFileSync行,并且必须重新执行代码以正确更新图像的元数据。

It's really strange, I've try to read the JSON file content before the exec line but everything is ok. 真的很奇怪,我尝试在exec行之前读取JSON文件的内容,但是一切正常。 I've use asynchronous functions, with and without promise... there is nothing to do it doesn't work. 我已经使用了异步函数,无论有没有承诺,都没事做,这是行不通的。

Thank you for your help. 谢谢您的帮助。

I'll answer my own question: 我会回答我自己的问题:

The problem was that I use nodemon, however by default nodemon watches JSON files. 问题是我使用nodemon,但是默认情况下nodemon监视JSON文件。 But in my code I created a JSON file to use it right after. 但是在我的代码中,我创建了一个JSON文件以供以后使用。 So, I created the JSON file correctly, nodemon sees it, and restarts the node server; 因此,我正确创建了JSON文件,nodemon看到了该文件,然后重新启动了节点服务器。 the rest of the code does not run. 其余代码无法运行。

To fix this, I added an option to ignore the created files in my package.json: 为了解决这个问题,我添加了一个选项来忽略package.json中创建的文件:

"nodemonConfig": {
   "ignore": [
      "path/to/files/to/ingore/*"
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM