diff --git a/app/collections/playlists.js b/app/collections/playlists.js index 0070b35..eebcd12 100644 --- a/app/collections/playlists.js +++ b/app/collections/playlists.js @@ -44,8 +44,7 @@ const Playlists = Backbone.Collection.extend({ } }, - parse(resp, options) { - let playlists = JSON.parse('' + resp); + parse(playlists, options) { let result = []; for (let i = 0; i < playlists.length; i++) { result.push(playlists[i].value); diff --git a/app/collections/tracks.js b/app/collections/tracks.js index cb99df1..22a5b39 100644 --- a/app/collections/tracks.js +++ b/app/collections/tracks.js @@ -80,9 +80,8 @@ const Tracks = Backbone.Collection.extend({ } }, - parse(resp, options) { + parse(tracks, options) { let result = []; - let tracks = JSON.parse('' + resp); for (let i = 0; i < tracks.length; i++) { result.push(tracks[i].value); } @@ -99,12 +98,21 @@ cozysdk.defineRequest('File', 'music', (doc) => { }); cozysdk.defineRequest('Track', 'all', (doc) => { + if (!doc._attachments) { emit(doc._id, doc); + } + }, (error, response) => { +}); + +cozysdk.defineRequest('Track', 'oldDoctype', (doc) => { + if (doc.title) { + emit(doc._id, doc); + } }, (error, response) => { }); cozysdk.defineRequest('Track', 'playable', (doc) => { - if (!doc.hidden) { + if (!doc.hidden && !doc._attachments) { emit(doc._id, doc); } }, (error, response) => { diff --git a/app/libs/file.js b/app/libs/file.js index 0e4e61b..b4ede61 100644 --- a/app/libs/file.js +++ b/app/libs/file.js @@ -1,25 +1,115 @@ import Track from '../models/track'; import application from '../application'; import cozysdk from 'cozysdk-client'; - +import async from 'async'; export function syncFiles() { - cozysdk.run('File', 'music', {}, (err, res) => { - if (res) { - let files = JSON.parse('' + res); + cozysdk.run('Track', 'oldDoctype', {}, (err, tracks) => { + if (tracks.length > 0) { + createCozicFolder(tracks); + } else { + fileSynchronisation(); + } + }); +} + +function fileSynchronisation() { + cozysdk.run('File', 'music', {}, (err, files) => { + if (files) { getAllTracksFileId(files); } }); } +// Create Cozic Folder +function createCozicFolder(tracks) { + cozysdk.defineRequest('Folder', 'Cozic', (doc) => { + if (doc.name == 'Cozic') { + emit(doc._id, doc); + } + }, (error, response) => { + cozysdk.run('Folder', 'Cozic', {}, (err, folder) => { + if (folder.length == 0) { + let folder = { + "path": "", + "name": "Cozic", + "docType": "folder", + "creationDate": Date.now, + "lastModification": Date.now, + "tags": [] + } + cozysdk.create('Folder', folder, (err, res) => { + convertToBinaries(tracks); + }); + } else { + convertToBinaries(tracks); + } + }); + }); +} + +// Convert attachment into a binary +function convertToBinaries(tracks) { + async.eachSeries(tracks, convertOneTrack, (err) => { + fileSynchronisation(); + }); +} + +function convertOneTrack(track, callback) { + track = track.value; + cozysdk.convertToBinaries(track._id, 'file', (err, resp) => { + if (resp) { + getTrack(track, callback); + } + }); +} + +// Get track info +function getTrack(track, callback) { + cozysdk.find('Track', track._id, (err, newTrack) => { + getBinary(newTrack, callback); + }); +} + +// Get binary info +function getBinary(track, callback) { + let binaryId = track.binary.file.id; + cozysdk.find('Binary', binaryId, (err, binary) => { + migrateTrack(track, binary, callback); + }); +} + +// Delete the old track and create a File doctype +function migrateTrack(track, binary, callback) { + let file = { + 'path': '/Cozic', + 'name': track.slug, + 'docType': 'file', + 'mime': 'audio/mpeg', + 'creationDate': Date.now, + 'lastModification': Date.now, + 'class': 'music', + 'size': binary._attachments.file.length, + 'tags': [], + 'uploading': false, + 'binary': track.binary, + 'checksum': '' + } + cozysdk.create('File', file, (err, res) => { + cozysdk.destroy('Track', track._id, (err, res) => { + callback(); + }); + }); +} + + // Get all needed variable function getAllTracksFileId(musicFiles) { - cozysdk.run('Track', 'file', {}, (err, res) => { + cozysdk.run('Track', 'file', {}, (err, tracks) => { let tracksFileId = []; let allTracksFiles = []; let musicFilesFileId = []; - if (res) { - let tracks = JSON.parse('' + res); + if (tracks) { for (let i = 0; i < tracks.length; i++) { tracksFileId.push(tracks[i].value.ressource.fileID); allTracksFiles.push(new Track(tracks[i].value)); diff --git a/package.json b/package.json index d66f1c2..f98a108 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "url": "https://github.com/flyingrub/cozy-music.git" }, "dependencies": { + "async": "^2.0.0-rc.2", "backbone": "^1.2.3", "backbone.marionette": "^2.4.4", "backbone.radio": "^1.0.2",