hello.
i am trying to work with the ubercart file download module.
i have set the fille download directory to the same path where the CCK file fields are uploaded to.
so what i have is ubercart product node that i have added a cck file field to it.
once the user uploads the cck file field, it is still not available to be download once bought. since the ubercart file download should be told about what files are downloadable for this spesific download.
what i want to achieve, and haven't found the way yet, is auto referencing the cck file field as a downloadable file for that product ?
so once the user created the file field it will automatically created as a download ubercart file.
any help ?

Comments

ananghd’s picture

any solution for this?

abaddon’s picture

working on the same issue, i think i will need to code a custom hook on the node save to fill out that value from the filefield

jozan84’s picture

Any news for this problem? Or can someone help me with the hook? Thx in advance

abaddon’s picture

basically what i did was to go with filefield all the way and code everything i needed in views, so filefield handles the upload, i am protecting the upload folder with .htaccess redirecting every request from that folder through a custom hook_menu callback in my custom module
the function checks if the user has access (has bought the product or is owner/admin) and if it does, it calls the same functions that handle the private downloads in drupal, or returns an access denied page
the custom code i mentioned done in views, is just an user panel listing all available downloads via products bought
i think this was more flexible than ubercart private downloads (because of views integration

abaddon’s picture

Something like this (ive replaced certain tokens in ALL CAPS):

#
# .htaccess custom settings:
#

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteRule ^sites/default/files/MY-FILE-STORAGE-PATH/(.+)$ index.php?q=MY-FILE-STORAGE-PATH-download/$1 [L,QSA]
</IfModule>
/**
 * Implementation of hook_menu().
 *  http://api.drupal.org/api/function/hook_menu
 */
//* function MYMODULE_menu
function MYMODULE_menu() {
        $items = array() ;

        $items['MY-FILE-STORAGE-PATH-download/%node/%'] = array(
                'title' => 'File download'
                , 'description' => 'File download.'
                , 'page callback' => '_MYMODULE_MY-FILE-STORAGE-PATH_download'
                , 'page arguments' => array(1, 2)
                , 'access arguments' => array('access content')
                , 'file' => 'MYMODULE.MY-FILE-STORAGE-PATH-download.inc'
                , 'type' => MENU_CALLBACK
        ) ;

        return $items ;
} // function MYMODULE_menu */

//* function _MYMODULE_MY-FILE-STORAGE-PATH_download
function _MYMODULE_MY-FILE-STORAGE-PATH_download($node, $filename) {
        global $user ;
        if ( $node->type != 'PRODUCT-TYPE' || ! strlen($filename) ) {
                return drupal_not_found() ;
        }
        if ( $node->uid != $user->uid && ! user_access('download all PRODUCT-TYPE') ) {
                $order_count = db_result(db_query('SELECT COUNT(o.order_id) FROM {uc_orders} o, {uc_order_products} p WHERE o.order_status = \'completed\' AND o.order_id = p.order_id AND p.nid = %d AND o.uid = %d', $node->nid, $user->uid)) ;
                if ( $order_count < 1 ) {
                        drupal_set_message(t('Download access denied.'), 'error') ;
                        return drupal_goto('node/' . $node->nid) ;
                }
        }
        $filefields = array('field_TYPE_file_pdf', 'field_TYPE_file_zip' /* etc.. my product nodes can contain multiple cck filefields so im checking all */) ;
        foreach ( $filefields as $filefield ) {
                if ( ! property_exists($node, $filefield) ) continue ;
                if ( strcmp($filename, $node->{$filefield}[0]['filename']) ) continue ;
                $filepath = $node->{$filefield}[0]['filepath'] ;
                $filemime = $node->{$filefield}[0]['filemime'] ;
                $origname = $node->{$filefield}[0]['origname'] ;
                $headers = array(
                        'Content-Type: ' . $filemime
                        , 'Content-Disposition: attachment; filename="' . $origname . '"'
                        , 'Content-Length: ' . filesize($filepath)
                ) ;
                return file_transfer($filepath, $headers) ;
        }
        return drupal_not_found() ;
} // function _MYMODULE_MY-FILE-STORAGE-PATH_download */

with .htaccess control over file directories, you also need to be careful about blocking the drupal core private files serving mechanism, something like this:

/**
 * Implementation of hook_file_download().
 *  http://api.drupal.org/api/function/hook_file_download
 */
//* function MYMODULE_file_download
function MYMODULE_file_download($filepath) {
        if ( strpos($filepath, 'MY-FILE-STORAGE-PATH/') === 0 ) return -1 ;
} // function MYMODULE_file_download */

and i forgot to mention that filefield saved the files to a path like this in my files directory: "files/CONTENT-TYPE/NID/filename.ext".. this allowed me to get the nid from the path above.. im using the http://drupal.org/project/filefield_paths module for this

hope this helps..

P.S. please excuse the input filter messing up my api.drupal.org links.. ive left those on as pointers

jozan84’s picture

Thank you for the reply , I will try this solution.

krembo’s picture

What is this string doing:
, 'file' => 'MYMODULE.MY-FILE-STORAGE-PATH-download.inc' ?
It seems I'm missing something here...
Files saved as - "files/ product/nid/smthing.mp3" and stay at that place.
everyone can download file form that folder... When do you make this files downloadable, and closed for non-buyers?
And how do you create list of buyed files in views? I've tryed to do such list, but only thing i created was list of files from CCK field with path "files/ product/nid/smthing.mp3" . If you can explain a little how it's going on.