Skip to content

Commit cdf383b

Browse files
jchooverdregad
authored andcommittedJun 27, 2012
Fix #11687: Bugs with attachments that are moved will lose attachments
Backporting 08c027a to 1.2.x branch. When a bug is logged and assigned to a project which has a project path assigned, and later a mantis "administrator" moves the bug from the initial project to a new project, the attachments are lost. Signed-off-by: Damien Regad <damien.regad@merckgroup.com>
1 parent ef24c0f commit cdf383b

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed
 

‎core/bug_api.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -1075,13 +1075,16 @@ function bug_copy( $p_bug_id, $p_target_project_id = null, $p_copy_custom_fields
10751075

10761076
/**
10771077
* Moves an issue from a project to another.
1078+
*
10781079
* @todo Validate with sub-project / category inheritance scenarios.
1079-
* @todo Fix #11687: Bugs with attachments that are moved will lose attachments.
10801080
* @param int p_bug_id The bug to be moved.
10811081
* @param int p_target_project_id The target project to move the bug to.
10821082
* @access public
10831083
*/
10841084
function bug_move( $p_bug_id, $p_target_project_id ) {
1085+
// Attempt to move disk based attachments to new project file directory.
1086+
file_move_bug_attachments( $p_bug_id, $p_target_project_id );
1087+
10851088
// Move the issue to the new project.
10861089
bug_set_field( $p_bug_id, 'project_id', $p_target_project_id );
10871090

‎core/file_api.php

+69
Original file line numberDiff line numberDiff line change
@@ -909,3 +909,72 @@ function file_get_content_type_override( $p_filename ) {
909909

910910
return $g_file_download_content_type_overrides[$t_extension];
911911
}
912+
913+
/**
914+
* Move any attachments as needed when a bug is moved from project to project.
915+
*
916+
* @param int $p_bug_id ID of bug containing attachments to be moved
917+
* @param int $p_project_id_to destination project ID for the bug
918+
* @return null
919+
*/
920+
function file_move_bug_attachments( $p_bug_id, $p_project_id_to ) {
921+
$t_project_id_from = bug_get_field( $p_bug_id, 'project_id' );
922+
if ( $t_project_id_from == $p_project_id_to ) {
923+
return;
924+
}
925+
926+
$t_method = config_get( 'file_upload_method' );
927+
if ( $t_method != DISK ) {
928+
return;
929+
}
930+
931+
if ( !file_bug_has_attachments( $p_bug_id ) ) {
932+
return;
933+
}
934+
935+
$t_path_from = project_get_field( $t_project_id_from, 'file_path' );
936+
if ( is_blank( $t_path_from ) ) {
937+
$t_path_from = config_get( 'absolute_path_default_upload_folder', null, null, $t_project_id_from );
938+
}
939+
file_ensure_valid_upload_path( $t_path_from );
940+
$t_path_to = project_get_field( $p_project_id_to, 'file_path' );
941+
if ( is_blank( $t_path_to ) ) {
942+
$t_path_to = config_get( 'absolute_path_default_upload_folder', null, null, $p_project_id_to );
943+
}
944+
file_ensure_valid_upload_path( $t_path_to );
945+
if ( $t_path_from == $t_path_to ) {
946+
return;
947+
}
948+
949+
# Initialize the update query to update a single row
950+
$t_bug_file_table = db_get_table( 'mantis_bug_file_table' );
951+
$c_bug_id = db_prepare_int( $p_bug_id );
952+
$query_disk_attachment_update = "UPDATE $t_bug_file_table
953+
SET folder=" . db_param() . "
954+
WHERE bug_id=" . db_param() . "
955+
AND id =" . db_param();
956+
957+
$t_attachment_rows = bug_get_attachments( $p_bug_id );
958+
$t_attachments_count = count( $t_attachment_rows );
959+
for ( $i = 0; $i < $t_attachments_count; $i++ ) {
960+
$t_row = $t_attachment_rows[$i];
961+
$t_basename = basename( $t_row['diskfile'] );
962+
963+
$t_disk_file_name_from = file_path_combine( $t_path_from, $t_basename );
964+
$t_disk_file_name_to = file_path_combine( $t_path_to, $t_basename );
965+
966+
if ( !file_exists( $t_disk_file_name_to ) ) {
967+
chmod( $t_disk_file_name_from, 0775 );
968+
if ( !rename( $t_disk_file_name_from, $t_disk_file_name_to ) ) {
969+
if ( !copy( $t_disk_file_name_from, $t_disk_file_name_to ) ) {
970+
trigger_error( FILE_MOVE_FAILED, ERROR );
971+
}
972+
file_delete_local( $t_disk_file_name_from );
973+
}
974+
chmod( $t_disk_file_name_to, config_get( 'attachments_file_permissions' ) );
975+
db_query_bound( $query_disk_attachment_update, Array( db_prepare_string( $t_path_to ), $c_bug_id, db_prepare_int( $t_row['id'] ) ) );
976+
} else {
977+
trigger_error( ERROR_FILE_DUPLICATE, ERROR );
978+
}
979+
}
980+
}

0 commit comments

Comments
 (0)