예제 #1
0
/*----------------------------------------------------------------------
|   PLT_FileMediaServer::BuildResourceUri
+---------------------------------------------------------------------*/
NPT_String
PLT_FileMediaServer::BuildResourceUri(const NPT_HttpUrl& base_uri, 
                                      const char*        host, 
                                      const char*        file_path)
{
    NPT_HttpUrl uri = base_uri;
    NPT_HttpUrlQuery query(uri.GetQuery());
    NPT_String result;

    query.AddField("path", file_path);
    if (host) uri.SetHost(host);
    uri.SetQuery(query.ToString());
    
    // 360 hack: force inclusion of port
    result = uri.ToStringWithDefaultPort(0);

    // 360 hack: it removes the query, so we make it look like a path
    // and we replace + with urlencoded value of space
    result.Replace('?', "%3F");
    result.Replace('+', "%20");
    return result;
}
예제 #2
0
/*----------------------------------------------------------------------
|   PLT_FileMediaServer::BuildFromFilePath
+---------------------------------------------------------------------*/
PLT_MediaObject*
PLT_FileMediaServer::BuildFromFilePath(const NPT_String& filepath, 
                                       bool              with_count /* = true */,
                                       NPT_SocketInfo*   info /* = NULL */,
                                       bool              keep_extension_in_title /* = false */)
{
    NPT_String            delimiter = m_DirDelimiter;
    NPT_String            root = m_Path;
    PLT_MediaItemResource resource;
    PLT_MediaObject*      object = NULL;
    int                   dir_delim_index;

    /* make sure this is a valid entry */
    /* and retrieve the entry type (directory or file) */
    NPT_DirectoryEntryInfo entry_info; 
    if (!ProceedWithEntry(filepath, entry_info)) goto failure;

    /* find the last directory delimiter */
    dir_delim_index = filepath.ReverseFind(delimiter);
    if (dir_delim_index < 0) goto failure;

    if (entry_info.type == NPT_FILE_TYPE) {
        object = new PLT_MediaItem();

        /* we need a valid extension to retrieve the mimetype for the protocol info */
        int ext_index = filepath.ReverseFind('.');
        if (ext_index <= 0 || ext_index < dir_delim_index) {
            ext_index = filepath.GetLength();
        }

        /* Set the title using the filename for now */
        object->m_Title = filepath.SubString(dir_delim_index+1, 
            keep_extension_in_title?filepath.GetLength():ext_index - dir_delim_index -1);
        if (object->m_Title.GetLength() == 0) goto failure;

        /* Set the protocol Info from the extension */
        const char* ext = ((const char*)filepath) + ext_index;
        resource.m_ProtocolInfo = PLT_MediaItem::GetProtInfoFromExt(ext);
        if (resource.m_ProtocolInfo.GetLength() == 0)  goto failure;

        /* Set the resource file size */
        resource.m_Size = entry_info.size;
 
        /* format the resource URI */
        NPT_String url = filepath.SubString(root.GetLength());

        // get list of ip addresses
        NPT_List<NPT_String> ips;
        NPT_CHECK_LABEL_SEVERE(PLT_UPnPMessageHelper::GetIPAddresses(ips), failure);

        // if we're passed an interface where we received the request from
        // move the ip to the top
        if (info && info->local_address.GetIpAddress().ToString() != "0.0.0.0") {
            ips.Remove(info->local_address.GetIpAddress().ToString());
            ips.Insert(ips.GetFirstItem(), info->local_address.GetIpAddress().ToString());
        }

        // iterate through list and build list of resources
        NPT_List<NPT_String>::Iterator ip = ips.GetFirstItem();
        while (ip) {
            NPT_HttpUrl uri = m_FileBaseUri;
            NPT_HttpUrlQuery query;
            query.AddField("path", url);
            uri.SetHost(*ip);
            uri.SetQuery(query.ToString());
            //uri.SetPath(uri.GetPath() + url);

            /* prepend the base URI and url encode it */ 
            //resource.m_Uri = NPT_Uri::Encode(uri.ToString(), NPT_Uri::UnsafeCharsToEncode);
            resource.m_Uri = uri.ToString();

            /* Look to see if a metadatahandler exists for this extension */
            PLT_MetadataHandler* handler = NULL;
            NPT_Result res = NPT_ContainerFind(m_MetadataHandlers, PLT_MetadataHandlerFinder(ext), handler);
            if (NPT_SUCCEEDED(res) && handler) {
                /* if it failed loading data, reset the metadatahandler so we don't use it */
                if (NPT_SUCCEEDED(handler->LoadFile(filepath))) {
                    /* replace the title with the one from the Metadata */
                    NPT_String newTitle;
                    if (handler->GetTitle(newTitle) != NULL) {
                        object->m_Title = newTitle;
                    }

                    /* assign description */
                    handler->GetDescription(object->m_Description.long_description);

                    /* assign album art uri if we haven't yet */
                    /* prepend the album art base URI and url encode it */ 
                    if (object->m_ExtraInfo.album_art_uri.GetLength() == 0) {
                        NPT_HttpUrl uri = m_AlbumArtBaseUri;
                        NPT_HttpUrlQuery query;
                        query.AddField("path", url);
                        uri.SetHost(*ip);
                        uri.SetQuery(query.ToString());
                        //uri.SetPath(uri.GetPath() + url);

                        object->m_ExtraInfo.album_art_uri = NPT_Uri::PercentEncode(uri.ToString(), NPT_Uri::UnsafeCharsToEncode);
                    }

                    /* duration */
                    handler->GetDuration((NPT_UInt32&)resource.m_Duration);

                    /* protection */
                    handler->GetProtection(resource.m_Protection);
                }
            }

            object->m_ObjectClass.type = PLT_MediaItem::GetUPnPClassFromExt(ext);
            object->m_Resources.Add(resource);

            ++ip;
        }
    } else {
        object = new PLT_MediaContainer;

        /* Assign a title for this container */
        if (filepath.Compare(root, true) == 0) {
            object->m_Title = "Root";
        } else {
            object->m_Title = filepath.SubString(dir_delim_index+1, filepath.GetLength() - dir_delim_index -1);
            if (object->m_Title.GetLength() == 0) goto failure;
        }

        /* Get the number of children for this container */
        if (with_count) {
            NPT_Cardinal count = 0;
            NPT_CHECK_LABEL_SEVERE(GetEntryCount(filepath, count), failure);
            ((PLT_MediaContainer*)object)->m_ChildrenCount = count;
        }

        object->m_ObjectClass.type = "object.container";
    }

    /* is it the root? */
    if (filepath.Compare(root, true) == 0) {
        object->m_ParentID = "-1";
        object->m_ObjectID = "0";
    } else {
        /* is the parent path the root? */
        if (dir_delim_index == (int)root.GetLength() - 1) {
            object->m_ParentID = "0";
        } else {
            object->m_ParentID = "0" + delimiter + filepath.SubString(root.GetLength(), dir_delim_index - root.GetLength());
        }
        object->m_ObjectID = "0" + delimiter + filepath.SubString(root.GetLength());
    }

    return object;

failure:
    delete object;
    return NULL;
}