Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #22543 from jimporter/ringtones-mimetype
Browse files Browse the repository at this point in the history
Bug 1045186 - [B2G][Ringtones] The user can not share ringtone through MMS
  • Loading branch information
Jim Porter authored and rvandermeulen committed Aug 20, 2014
1 parent 72e8f07 commit 806d37a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
2 changes: 1 addition & 1 deletion apps/ringtones/js/actions_menu.js
Expand Up @@ -84,7 +84,7 @@ ActionsMenu.prototype = {
__bug1015513_hide_from_self__: true,
number: 1,
blobs: [blob],
filenames: [self._tone.name],
filenames: [self._tone.filename],
metadata: [{
title: self._tone.name
}]
Expand Down
50 changes: 43 additions & 7 deletions apps/ringtones/js/built_in_ringtones.js
Expand Up @@ -25,6 +25,30 @@
* An array of the valid tone types ('ringtone' and 'alerttone').
*/
window.builtInRingtones = (function() {
var mimeTypeMap = {
'.mp3': 'audio/mp3',
'.mp4': 'audio/mp4',
'.ogg': 'audio/ogg',
'.opus': 'audio/ogg'
};

/**
* Try to guess the MIME type of a file based on its extension. It's a shame
* XHRs don't do this for us automatically!
*
* @param {String} filename The filename of the ringtone.
* @return {String} The MIME type.
*/
function inferMimeType(filename) {
var dot = filename.lastIndexOf('.');
if (dot === -1) {
console.warn('Couldn\'t infer mimetype for ' + filename);
return 'application/octet-stream';
}
var ext = filename.substr(dot);
return mimeTypeMap[ext] || 'application/octet-stream';
}

/**
* Create a new built-in ringtone object.
*
Expand All @@ -34,11 +58,18 @@ window.builtInRingtones = (function() {
function BuiltInRingtone(filename, baseURL) {
// Strip the file extension for the ID and l10n ID to make it easier to
// change the file extensions in the future.
this._rootName = filename.replace(/\.\w+$/, '');
this._url = baseURL + filename;
this._filename = filename;
this._baseURL = baseURL;
}

BuiltInRingtone.prototype = {
/**
* @return {String} The filename without the extension.
*/
get _rootName() {
return this._filename.replace(/\.\w+$/, '');
},

/**
* @return {String} The localized name of the tone. Assumes that mozL10n has
* been initialized.
Expand All @@ -47,6 +78,13 @@ window.builtInRingtones = (function() {
return navigator.mozL10n.get(this.l10nID);
},

/**
* @return {String} The filename of the ringtone.
*/
get filename() {
return this._filename;
},

/**
* @return {String} The l10n ID for the tone's name.
*/
Expand All @@ -65,7 +103,7 @@ window.builtInRingtones = (function() {
* @return {String} A URL pointing to the tone's audio data.
*/
get url() {
return this._url;
return this._baseURL + this._filename;
},

/**
Expand All @@ -92,13 +130,11 @@ window.builtInRingtones = (function() {
return new Promise(function(resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.overrideMimeType(inferMimeType(url));
xhr.responseType = 'blob';
xhr.send();
xhr.onload = function() {
// Use slice() to strip the "application/xml" MIME type from the Blob,
// since it's not XML! (We'll just let consumers infer what the type
// really is.)
resolve(xhr.response.slice());
resolve(xhr.response);
};
xhr.onerror = function() {
var err = new Error('Could not read sound file: ' + url +
Expand Down
9 changes: 9 additions & 0 deletions apps/ringtones/js/custom_ringtones.js
Expand Up @@ -72,6 +72,15 @@ window.customRingtones = (function() {
return this._name;
},

/**
* @return {String} The filename of the ringtone.
*/
get filename() {
// XXX: If we ever start supporting Blobs that aren't really files, we'll
// need to figure out a way to make a fallback filename!
return this._blob.name;
},

/**
* @return {String} The subtitle of the ringtone (e.g. the artist of the
* song), or null if there is no subtitle.
Expand Down
7 changes: 7 additions & 0 deletions apps/ringtones/js/null_ringtone.js
Expand Up @@ -30,6 +30,13 @@ NullRingtone.prototype = {
return navigator.mozL10n.get(this.l10nID);
},

/**
* @return {String} The filename of the ringtone (null in this case).
*/
get filename() {
return null;
},

/**
* @return {String} The URL of the ringtone (null in this case).
*/
Expand Down

0 comments on commit 806d37a

Please sign in to comment.