Closed GoogleCodeExporter closed 8 years ago
FOUND THE PROBLEM ! TO ALL DEVS : Do not assume that
MediaFileSource.GetContentTypeForFileName(path) returns the mime type
always. You have to install windows media player on the server, so the
video file extensions can be properly inserted in the machine's
registries. Actually this method should be removed from the API, its
causing troubles. User this one instead (video files only) :
public string GetMIMETypeByFileName(string filename)
{
string extension =
filename.Substring(filename.LastIndexOf('.') + 1).ToLower();
switch (extension)
{
case "wmv" : return "video/x-ms-wmv";
case "mp4" : return "video/mp4";
case "mov" : return "video/quicktime";
case "flv" : return "video/x-flv";
case "mpg" :
case "mpeg" : return "video/mpeg";
case "3gp" : return "video/3gpp";
case "mkv": return "video/x-matroska";
case "avi":
default: return "video/x-msvideo";
}
}
Original comment by mathcore...@gmail.com
on 29 Sep 2011 at 3:53
Thanks for the updates, Nikolay.
Would you like to submit a patch for your solution?
Original comment by ccherub...@google.com
on 29 Sep 2011 at 4:04
Since I'm very new to the API and this issue tracker, how I can submit a patch ?
I would like to share with the community this piece of code here :
public string GetContentType(string fileName)
{
string contentType = "application/octetstream";
string ext = Path.GetExtension(fileName).ToLower();
if (string.IsNullOrEmpty(ext))
{
return contentType;
}
RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(ext);
if (registryKey != null && registryKey.GetValue("Content Type") != null)
{
contentType = registryKey.GetValue("Content Type").ToString();
return contentType;
}
RegistryKey typeKey = Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type");
foreach (string keyname in typeKey.GetSubKeyNames())
{
RegistryKey curKey = typeKey.OpenSubKey(keyname);
if (curKey != null)
{
object extension = curKey.GetValue("Extension");
if (extension != null)
{
if (extension.ToString().ToLower() == ext)
{
return keyname;
}
}
}
}
return contentType;
}
This is a good method for getting the MIME type for a given file extension, but
its not working perfectly (again some applications has to be installed and do
associations with their file extensions). So I guess the 'hardcoded' way of
returning the MIME type is the best solution. The cases above cover only the
video file types supported by YouTube (Have I missed any?), so we should add
the audio file types as well.
Original comment by mathcore...@gmail.com
on 29 Sep 2011 at 8:13
To create a patch, you have to start from the latest version of the code from
the repository (http://code.google.com/p/google-gdata/source/checkout) and then
run "svn diff".
BTW, your latest code uses the Windows Registry and I don't think that's a good
idea. First, the values you'll find in the registry won't be consistent in
different Windows installations and second I think this excludes non-Windows
machine.
Original comment by ccherub...@google.com
on 29 Sep 2011 at 9:03
I just posted the last code as example, with no intention that it is going to
do any good to determine correctly the mime type by file extension. As I've
said its not going to work in all cases. Will extend the function with the
hardcoded values for all types supported with youtube and will commit it next
week.
Original comment by mathcore...@gmail.com
on 30 Sep 2011 at 7:32
Marking this as obsolete as there was no activity for more than 6 months.
Feel free to reopen if there are updates
Original comment by ccherub...@google.com
on 11 Apr 2012 at 11:09
Original issue reported on code.google.com by
mathcore...@gmail.com
on 29 Sep 2011 at 10:01