Mass rename uploaded files Mass rename uploaded files
 

News:

cpg1.5.48 Security release - upgrade mandatory!
The Coppermine development team is releasing a security update for Coppermine in order to counter a recently discovered vulnerability. It is important that all users who run version cpg1.5.46 or older update to this latest version as soon as possible.
[more]

Main Menu

Mass rename uploaded files

Started by netb, March 21, 2014, 09:22:40 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

netb

Hello all, my first post here, although have been using the forums for quite some time.  :)

If someone could help me, because I'm not experienced enough and could make only simple mods in the code.

Please tell me what needs to be done to transfer the "rename a file" code from edit_one_pic.php in editpics.php, so that it would be more easier and faster to rename multiple files which are already uploaded.

I have found the pieces of code in the two files but am afraid to experiment not knowing how the whole thing works.


    // rename a file
    if ($superCage->post->keyExists('filename')) {
        $post_filename = $superCage->post->getEscaped('filename');
    }

    if ($post_filename != $pic['filename']) {

        if ($CONFIG['make_intermediate'] && cpg_picture_dimension_exceeds_intermediate_limit($pic['pwidth'], $pic['pheight'])) {
            $prefixes = array('fullsize', 'normal', 'thumb');
        } else {
            $prefixes = array('fullsize', 'thumb');
        }

        if ($CONFIG['enable_watermark'] == '1' && ($CONFIG['which_files_to_watermark'] == 'both' || $CONFIG['which_files_to_watermark'] == 'original')) {
            $prefixes[] = 'orig';
        }

        if (!is_image($pic['filename'])) {
            $prefixes = array('fullsize');

            // Check for custom thumbnails
            $mime_content_old = cpg_get_type($pic['filename']);
            $mime_content_new = cpg_get_type(replace_forbidden($post_filename));

            $file_base_name_old = str_replace('.' . $mime_content_old['extension'], '', basename($pic['filename']));

            foreach (array('.gif','.png','.jpg') as $thumb_extension) {
                if (file_exists($CONFIG['fullpath'] . $pic['filepath'] . $CONFIG['thumb_pfx'] . $file_base_name_old . $thumb_extension)) {
                    // Thumbnail found, check if it's the only file using that thumbnail
                    $count = mysql_result(cpg_db_query("SELECT COUNT(*) FROM {$CONFIG['TABLE_PICTURES']} WHERE filepath = '{$pic['filepath']}' AND filename LIKE '{$file_base_name_old}.%'"), 0);
                    if ($count == 1) {
                        $prefixes[] = 'thumb';
                        $custom_thumb = TRUE;
                        break;
                    }
                }
            }
        }

        $pic_prefix = array(
            'thumb'    => $CONFIG['thumb_pfx'],
            'normal'   => $CONFIG['normal_pfx'],
            'orig'     => $CONFIG['orig_pfx'],
            'fullsize' => '',
        );

        $files_to_rename = array();

        foreach ($prefixes as $prefix) {

            $oldname = urldecode($CONFIG['fullpath'] . $pic['filepath'] . $pic_prefix[$prefix] . $pic['filename']);
            $filename = replace_forbidden($post_filename);
            $newname = str_replace($pic['filename'], $filename, $oldname);

            if ($custom_thumb == TRUE && $prefix == 'thumb') {
                $oldname = str_replace('.' . $mime_content_old['extension'], $thumb_extension, $oldname);
                $newname = str_replace('.' . $mime_content_new['extension'], $thumb_extension, $newname);
            }

            $old_mime = cpg_get_type($oldname);
            $new_mime = cpg_get_type($newname);

            if (($old_mime['mime'] != $new_mime['mime']) && isset($new_mime['mime'])) {
                cpg_die(CRITICAL_ERROR, sprintf($lang_editpics_php['mime_conv'], $old_mime['mime'], $new_mime['mime']), __FILE__, __LINE__);
            }

            if (!is_known_filetype($newname)) {
                cpg_die(CRITICAL_ERROR, $lang_editpics_php['forb_ext'], __FILE__, __LINE__);
            }

            if (file_exists($newname)) {
                cpg_die(CRITICAL_ERROR, sprintf($lang_editpics_php['file_exists'], $newname), __FILE__, __LINE__);
            }

            if (!file_exists($oldname)) {
                cpg_die(CRITICAL_ERROR, sprintf($lang_editpics_php['src_file_missing'], $oldname), __FILE__, __LINE__);
            }

            // Check if there will be no conflicts before doing anything
            $files_to_rename[] = array(
                'oldname'   => $oldname,
                'filename'  => $filename,
                'newname'   => $newname
            );
        }

        if (count($files_to_rename) > 0) {
            foreach ($files_to_rename as $file) {
                if (rename($file['oldname'], $file['newname'])) {
                    cpg_db_query("UPDATE {$CONFIG['TABLE_PICTURES']} SET filename = '{$file['filename']}' WHERE pid = '$pid' LIMIT 1");
                } else {
                    cpg_die(CRITICAL_ERROR, sprintf($lang_editpics_php['rename_failed'], $oldname, $newname), __FILE__, __LINE__);
                }
            }
        }
    }
} // end function process_post_data


{$icon_array['file_name']}{$lang_common['filename']}: <tt>{$CONFIG['site_url']}{$CONFIG['fullpath']}{$filepath}{$filename}</tt>

netb

Couldn't edit the first post. P.S. Forgot to leave a link to my gallery - greatmusclebodies.com

netb

I was going to start a new topic but found my old one in the search. :) So could this mod be made into a plugin? I really need a fast way to bulk edit filenames already inside the gallery from its admin interface at editpics.php for example. Doing it one by one from the edit_one_pic.php link isn't very efficient.

Αndré

I'll have a look as soon as possible. Unfortunately I've currently just little spare time. There are (at least) 2 things to regard:
1. If 2 or more files have the same name, add our common "~123" suffix
2. If an error occurs, queue the error message, proceed renaming the other files and display the errors at the end.

Please reply to this thread, so I get a reminder in my inbox.

netb

Bump for Αndré, to get a reminder. :)

netb


Αndré

Thanks, I'll have a look as soon as possible (I know, I already said that :P).