Skip to content

Commit

Permalink
add update method #21
Browse files Browse the repository at this point in the history
  • Loading branch information
Zazama committed Oct 10, 2017
1 parent 8387a33 commit f4da14a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 4 deletions.
19 changes: 15 additions & 4 deletions example/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const fs = require('fs')


//tags.image is the path to the image (only png/jpeg files allowed)
const tags = {
/*const tags = {
title: "Tomorrow",
artist: "Kevin Penkin",
album: "TVアニメ「メイドインアビス」オリジナルサウンドトラック",
Expand All @@ -14,12 +14,12 @@ const tags = {
text: "some text"
},
TRCK: "27"
}
}*/

//let success = nodeID3.write(tags, "./example/Kevin Penkin - Tomorrow.mp3");
//console.log(success);

console.log(nodeID3.read("./example/Kevin Penkin - Tomorrow.mp3"))
//console.log(nodeID3.read("./example/Kevin Penkin - Tomorrow.mp3"))

//async

Expand All @@ -46,4 +46,15 @@ nodeID3.read("./example/Kevin Penkin - Tomorrow.mp3", function(err, tags) {
})
})
*/
*/

console.log(nodeID3.update({
title: "TomorrowUP",
TRCK: "28",
image: "./example/mia_cover.jpg",
COMM: {
language: "eng",
text: "some text2"
},
genre: "testUP"
}, "./example/Kevin Penkin - Tomorrow.mp3"));
52 changes: 52 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,58 @@ NodeID3.prototype.read = function(filebuffer, options, fn) {
}
}

/*
** Update ID3-Tags from passed buffer/filepath
** filebuffer => Buffer || String
** tags => Object
** fn => function (for asynchronous usage)
*/
NodeID3.prototype.update = function(tags, filebuffer, fn) {
let rawTags = {}
Object.keys(tags).map(function(tagKey) {
// if js name passed (TF)
if(TFrames[tagKey]) {
rawTags[TFrames[tagKey]] = tags[tagKey]

// if js name passed (SF)
} else if(SFrames[tagKey]) {
rawTags[SFrames[tagKey].name] = tags[tagKey]

// if raw name passed (TF)
} else if(Object.keys(TFrames).map(i => TFrames[i]).indexOf(tagKey) !== -1) {
rawTags[tagKey] = tags[tagKey]

// if raw name passed (SF)
} else if(Object.keys(SFrames).map(i => SFrames[i]).map(x => x.name).indexOf(tagKey) !== -1) {
rawTags[tagKey] = tags[tagKey]
}
})
if(!fn || typeof fn !== 'function') {
let currentTags = this.read(filebuffer)
currentTags = currentTags.raw
// update current tags with new or keep them
Object.keys(rawTags).map(function(tag) {
currentTags[tag] = rawTags[tag]
})
return this.write(currentTags, filebuffer)
} else {
this.read(filebuffer, function(err, currentTags) {
if(err) {
fn(err)
return
}
currentTags = currentTags.raw
// update current tags with new or keep them
Object.keys(currentTags).map(function(tag) {
if(rawTags[tag]) {
currentTags[tag] = rawTags[tag]
}
})
this.write(currentTags, filebuffer, fn)
}.bind(this))
}
}

/*
** Read ID3-Tags from passed buffer
** filebuffer => Buffer
Expand Down

0 comments on commit f4da14a

Please sign in to comment.