-
Notifications
You must be signed in to change notification settings - Fork 1
/
songs_add.js
81 lines (65 loc) · 1.96 KB
/
songs_add.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var mongoose = require('mongoose');
var fs = require('fs');
var pa = require('path');
var id3 = require('id3js');
var Sync = require('sync');
var folder="Ajab Prem Ki Ghazab Kahani";
var path= "./public/music/"+folder;
var song_folder = fs.readdirSync(path);
function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
mongoose.connect('mongodb://localhost/music');
var Schema = mongoose.Schema;
// create a schema
var songScheme = new Schema({
title:String,
path:String,
artist:String,
album:String,
length:String,
release_date: Date,
album_art_small:String,
album_art:String,
rating:Number,
views:Number,
genre:String
},{ collection: 'songs' });
// the schema is useless so far
// we need to create a model using it
var Song = mongoose.model('Song',songScheme);
var name;
Sync(function(){
for(var i=0;i<song_folder.length;i++)
{
var file = song_folder[i];
if(endsWith(file, ".mp3"))
{
// Function.prototype.sync() interface is same as Function.prototype.call() - first argument is 'this' context
var tags = id3.sync(null,{ file: path+'/'+file, type: id3.OPEN_LOCAL });
// console.log(tags); // 5
var pat = '/music/'+folder+'/'+file;
var newSong = Song({
title:tags.title,
album:tags.album,
artist:tags.artist,
genre:tags.v2.genre,
path:pat,
album_art_small:'/music/'+folder+'/AlbumArtSmall.jpg',
album_art:'/music/'+folder+'/Folder.jpg',
rating:0,
views:0
});
// console.log(newSong.path);
newSong.save(function(err) {
if (err) throw err;
console.log('Song added');
});
}
}
})
var path,songs_all;
Song.find({}, function(err, songs) {
if (err) throw err;
songs_all = songs;
});