Browse Source

+ Added scheduled task to remove temporary attachments
! Checking only the size of the current directory when posting

emanuele 13 years ago
parent
commit
de836631d5

+ 1 - 1
Sources/ManageAttachments.php

@@ -620,7 +620,7 @@ function MaintainFiles()
 			if ($file == '.' || $file == '..')
 				continue;
 
-			if (preg_match('~^post_tmp_\d+_\d+$~', $file) != 0)
+			if (strpos($file, 'post_tmp_') !== false)
 			{
 				// Temp file is more than 5 hours old!
 				if (filemtime($attach_dir . '/' . $file) < time() - 18000)

+ 41 - 0
Sources/ScheduledTasks.php

@@ -1641,4 +1641,45 @@ function scheduled_paid_subscriptions()
 	return true;
 }
 
+/**
+ * Check for un-posted attachments is something we can do once in a while :P
+ * This function uses opendir cycling through all the attachments
+ */
+function scheduled_remove_old_temp_attachments()
+{
+	global $modSettings;
+
+	// We need to know where this thing is going.
+	if (!empty($modSettings['currentAttachmentUploadDir']))
+	{
+		if (!is_array($modSettings['attachmentUploadDir']))
+			$modSettings['attachmentUploadDir'] = unserialize($modSettings['attachmentUploadDir']);
+
+		// Just use the current path for temp files.
+		$attach_dirs = $modSettings['attachmentUploadDir'];
+	}
+	else
+	{
+		$attach_dirs = array($modSettings['attachmentUploadDir']);
+	}
+
+	foreach ($attach_dirs as $attach_dir)
+	{
+		$dir = @opendir($attach_dir) or fatal_lang_error('cant_access_upload_path', 'critical');
+		while ($file = readdir($dir))
+		{
+			if ($file == '.' || $file == '..')
+				continue;
+
+			if (strpos($file, 'post_tmp_') !== false)
+			{
+				// Temp file is more than 5 hours old!
+				if (filemtime($attach_dir . '/' . $file) < time() - 18000)
+					@unlink($attach_dir . '/' . $file);
+			}
+		}
+		closedir($dir);
+	}
+}
+
 ?>

+ 3 - 1
Sources/Subs-Post.php

@@ -2298,8 +2298,10 @@ function attachmentChecks($attachID)
 		{
 			$request = $smcFunc['db_query']('', '
 				SELECT SUM(size)
-				FROM {db_prefix}attachments',
+				FROM {db_prefix}attachments
+				WHERE id_folder = {int:folder_id}',
 				array(
+					'folder_id' => empty($modSettings['currentAttachmentUploadDir']) ? 1 : $modSettings['currentAttachmentUploadDir'],
 				)
 			);
 			list ($context['dir_size']) = $smcFunc['db_fetch_row']($request);

+ 1 - 0
other/install_2-1_mysql.sql

@@ -1614,6 +1614,7 @@ VALUES
 	(8, 0, 0, 1, 'd', 1, 'birthdayemails'),
 	(9, 0, 0, 1, 'w', 0, 'weekly_maintenance'),
 	(10, 0, 120, 1, 'd', 1, 'paid_subscriptions');
+	(11, 0, 120, 1, 'd', 1, 'remove_old_temp_attachments');
 
 # --------------------------------------------------------
 

+ 1 - 0
other/install_2-1_postgresql.sql

@@ -2105,6 +2105,7 @@ INSERT INTO {$db_prefix}scheduled_tasks	(id_task, next_time, time_offset, time_r
 INSERT INTO {$db_prefix}scheduled_tasks	(id_task, next_time, time_offset, time_regularity, time_unit, disabled, task) VALUES (8, 0, 0, 1, 'd', 1, 'birthdayemails');
 INSERT INTO {$db_prefix}scheduled_tasks	(id_task, next_time, time_offset, time_regularity, time_unit, disabled, task) VALUES (9, 0, 0, 1, 'w', 0, 'weekly_maintenance');
 INSERT INTO {$db_prefix}scheduled_tasks	(id_task, next_time, time_offset, time_regularity, time_unit, disabled, task) VALUES (10, 0, 120, 1, 'd', 1, 'paid_subscriptions');
+INSERT INTO {$db_prefix}scheduled_tasks	(id_task, next_time, time_offset, time_regularity, time_unit, disabled, task) VALUES (10, 0, 120, 1, 'd', 1, 'remove_old_temp_attachments');
 
 # --------------------------------------------------------
 

+ 1 - 0
other/install_2-1_sqlite.sql

@@ -1756,6 +1756,7 @@ INSERT INTO {$db_prefix}scheduled_tasks	(id_task, next_time, time_offset, time_r
 INSERT INTO {$db_prefix}scheduled_tasks	(id_task, next_time, time_offset, time_regularity, time_unit, disabled, task) VALUES (8, 0, 0, 1, 'd', 1, 'birthdayemails');
 INSERT INTO {$db_prefix}scheduled_tasks	(id_task, next_time, time_offset, time_regularity, time_unit, disabled, task) VALUES (9, 0, 0, 1, 'w', 0, 'weekly_maintenance');
 INSERT INTO {$db_prefix}scheduled_tasks	(id_task, next_time, time_offset, time_regularity, time_unit, disabled, task) VALUES (10, 0, 120, 1, 'd', 1, 'paid_subscriptions');
+INSERT INTO {$db_prefix}scheduled_tasks	(id_task, next_time, time_offset, time_regularity, time_unit, disabled, task) VALUES (10, 0, 120, 1, 'd', 1, 'remove_old_temp_attachments');
 COMMIT;
 
 # --------------------------------------------------------

+ 11 - 1
other/upgrade_2-1_mysql.sql

@@ -128,4 +128,14 @@ CHANGE ip_high4 ip_high4 tinyint(3) unsigned NOT NULL DEFAULT '0';
 ---# Adding new columns to log_packages ..
 ALTER TABLE {$db_prefix}log_packages
 ADD COLUMN credits varchar(255) NOT NULL DEFAULT '';
----#
+---#
+
+/******************************************************************************/
+--- Adding new scheduled tasts
+/******************************************************************************/
+---# Adding new Scheduled Task...
+INSERT INTO {$db_prefix}scheduled_tasks
+	(next_time, time_offset, time_regularity, time_unit, disabled, task)
+VALUES
+	(0, 120, 1, 'd', 0, 'remove_old_temp_attachments');
+---#

+ 11 - 1
other/upgrade_2-1_postgresql.sql

@@ -132,4 +132,14 @@ CHANGE ip_high4 ip_high4 tinyint(3) unsigned NOT NULL DEFAULT '0';
 ---# Adding new columns to log_packages ..
 ALTER TABLE {$db_prefix}log_packages
 ADD COLUMN credits varchar(255) NOT NULL DEFAULT '';
----#
+---#
+
+/******************************************************************************/
+--- Adding new scheduled tasts
+/******************************************************************************/
+---# Adding new Scheduled Task...
+INSERT INTO {$db_prefix}scheduled_tasks
+	(next_time, time_offset, time_regularity, time_unit, disabled, task)
+VALUES
+	(0, 120, 1, 'd', 0, 'remove_old_temp_attachments');
+---#

+ 11 - 1
other/upgrade_2-1_sqlite.sql

@@ -128,4 +128,14 @@ CHANGE ip_high4 ip_high4 tinyint(3) unsigned NOT NULL DEFAULT '0';
 ---# Adding new columns to log_packages ..
 ALTER TABLE {$db_prefix}log_packages
 ADD COLUMN credits varchar(255) NOT NULL DEFAULT '';
----#
+---#
+
+/******************************************************************************/
+--- Adding new scheduled tasts
+/******************************************************************************/
+---# Adding new Scheduled Task...
+INSERT INTO {$db_prefix}scheduled_tasks
+	(next_time, time_offset, time_regularity, time_unit, disabled, task)
+VALUES
+	(0, 120, 1, 'd', 0, 'remove_old_temp_attachments');
+---#