Is there an established way to retrieve the NID of a node based solely on the name of a file stored in an RDF repository?

i.e. sites/all/files/bitcache/file/1234567890abcdefghij => nid: 245

My goal: If a direct request is made for a file from the repository, I wish to rewrite the request to grab the appropriate node and display it, but I am having great difficulty figuring out how I can associate a NID with the file being requested, given no other information. I plan on using an .htaccess file to redirect the request to a PHP file which will do the lookup and spit out the correct node.

Much more info is available ... Thanks for your help.

Comments

Stupidscript’s picture

For example:

Request: http://example.com/files/1234567890abcdefghij

Lookup: SELECT nid FROM {???} WHERE ???file??? = '1234567890abcdefghij'

Redirect: http://example.com/node/ . $nid

I'd like to note that I have been trying to work with the bitcache functions to resolve this, however they seem less than useful, in that they always either return an expanded string that uses the input directly, or nothing.

i.e.
$uri = "bitcache://a0f1d4b7b8471897cfe8ee368a21dbcf9617b7f8";
// uploaded file in bitcache/file/

$url = bitcache_resolve_uri($uri);
// produces: http://example.com/bitcache/a0f1d4b7b8471897cfe8ee368a21dbcf9617b7f8
// I expected a path to the file referenced by the fingerprint data, but that URI = 404

$url2 = bitcache_uri_to_id($url);
// produces nothing
// I expected to receive a NID or something similar

$data = file_get_contents($url);
// produces nothing, although the file actually contains significant content
// I expected binary output, as this sample is a PDF file

I know this is the RDF issues section, and my reasoning for placing this support request here is because the ONLY references in the database I can find that correlate to the actual files stored in the repository are contained in the {rdf_resources} table.

I can find no other similar data anywhere else.
Forgive me if this is not the correct place to ask.

Stupidscript’s picture

Here's how I did it:

.htaccess file in the 'file' directory grabs all requests and redirects to a PHP file containing this script segment:

$file_hash = substr($file_name,0,-4);
$file_sql = "
        SELECT s.uri, fn.nid, ua.title, ur.dst, d.data FROM {rdf_data_file} d
        INNER JOIN {rdf_resources} s ON d.sid = s.rid
        LEFT JOIN {file_nodes} fn ON d.data = fn.uri
        LEFT JOIN {node} ua ON fn.nid = ua.nid
        LEFT JOIN {url_alias} ur ON ur.src = concat('node/', fn.nid)
        WHERE s.uri REGEXP '%s' && d.data REGEXP 'bitcache'
        LIMIT 1
";
$file_getNID = db_fetch_object(db_query($file_sql, $file_hash));

Which gives me the two things I need for my links: the title and the url_alias:

$file_getNID->title
$file_getNID->dst

The query relationships that get me there are:

rdf_data_file.SID = rdf_resources.RID
rdf_data_file.DATA = file_nodes.URI
file_nodes.NID = node.NID
url_alias.SRC = 'node/'.NID

Tweaks welcome.

smustgrave’s picture

Issue summary: View changes
Status: Active » Closed (outdated)