2016-11-26 03:25:35 +00:00
|
|
|
import { each } from 'lodash'
|
2016-06-05 11:29:49 +00:00
|
|
|
|
2016-11-26 03:25:35 +00:00
|
|
|
import { secondsToHis } from '../../utils'
|
|
|
|
import { http } from '..'
|
2016-06-05 11:29:49 +00:00
|
|
|
|
2016-06-25 10:15:57 +00:00
|
|
|
export const albumInfo = {
|
2016-06-25 16:05:24 +00:00
|
|
|
/**
|
|
|
|
* Get extra album info (from Last.fm).
|
|
|
|
*
|
|
|
|
* @param {Object} album
|
|
|
|
*/
|
2016-11-26 03:25:35 +00:00
|
|
|
fetch (album) {
|
2016-06-27 06:11:35 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
if (album.info) {
|
2016-11-26 03:25:35 +00:00
|
|
|
resolve(album)
|
|
|
|
return
|
2016-06-25 16:05:24 +00:00
|
|
|
}
|
|
|
|
|
2016-07-09 04:06:14 +00:00
|
|
|
http.get(`album/${album.id}/info`, data => {
|
2016-11-26 03:25:35 +00:00
|
|
|
data && this.merge(album, data)
|
|
|
|
resolve(album)
|
|
|
|
}, r => reject(r))
|
|
|
|
})
|
2016-06-25 16:05:24 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Merge the (fetched) info into an album.
|
|
|
|
*
|
|
|
|
* @param {Object} album
|
|
|
|
* @param {Object} info
|
|
|
|
*/
|
2016-11-26 03:25:35 +00:00
|
|
|
merge (album, info) {
|
2016-06-25 16:05:24 +00:00
|
|
|
// Convert the duration into i:s
|
2016-11-26 03:25:35 +00:00
|
|
|
info.tracks && each(info.tracks, track => {
|
|
|
|
track.fmtLength = secondsToHis(track.length)
|
|
|
|
})
|
2016-06-25 16:05:24 +00:00
|
|
|
|
|
|
|
// If the album cover is not in a nice form, discard.
|
|
|
|
if (typeof info.image !== 'string') {
|
2016-11-26 03:25:35 +00:00
|
|
|
info.image = null
|
2016-06-25 16:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the album cover on the client side to the retrieved image from server.
|
|
|
|
if (info.cover) {
|
2016-11-26 03:25:35 +00:00
|
|
|
album.cover = info.cover
|
2016-06-25 16:05:24 +00:00
|
|
|
}
|
|
|
|
|
2016-11-26 03:25:35 +00:00
|
|
|
album.info = info
|
|
|
|
}
|
|
|
|
}
|