Skip to content

Commit

Permalink
[ADD] Convert old doctype (rdubigny#15)
Browse files Browse the repository at this point in the history
 * [fix] JSON.parse (need unpublished cozysdk commits for now)
* [feature] add old format patch converter
  • Loading branch information
Ronan authored and m4dz committed Apr 13, 2016
1 parent 3741dd1 commit 7012f42
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 12 deletions.
3 changes: 1 addition & 2 deletions app/collections/playlists.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 11 additions & 3 deletions app/collections/tracks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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) => {
Expand Down
104 changes: 97 additions & 7 deletions app/libs/file.js
Original file line number Diff line number Diff line change
@@ -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));
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 7012f42

Please sign in to comment.