在CQ中,Attechment的size是没有限定的,类型是长整型的
但是过大的attachment在上传的过程中是很容易造成服务器挂掉的
可以在Base Action的Validation上写段脚本,读取当前记录的所有的Attachment,然后依次判断其size,如果某个文件的size超过了设定的值,则应阻止其进行提交
下面是一个限制Attachment的例子:
Restricting attachment size
developerWorks
This is an example of a policy enforcement hook for ClearQuest.
NOTE: This hook is presented only as an example of how to customize your use of IBM® Rational® ClearQuest®. It has not been formally tested, and is not supported by IBM.
Description
IBM® Rational® ClearQuest® Web supports uploading and viewing attachments just like native clients do, but is built in such a way that all other concurrent ClearQuest Web users on the same Web server will be frozen until the file upload/download is complete. One solution is to limit the size of the attachments before they are ever stored in the database. To accomplish this, we will depend on knowing a few facts about the inner workings of ClearQuest.
* Attachments can be added whether the record is in an editable state or not, and through the API, but in all cases adding an attachment performs an action on the record.
* The file upload is not done until that action is committed.
* Between the time that the file was attached and the action is committed, that attachment's size when accessed via the CQ API returns 0 (this is how we can tell which attachments have just been added).
By creating a Base Action, we can create a single Validation hook that executes during the context of all other actions. We will inspect the file size of the attachment(s), and prohibit the action from being committed if any is over a certain limit.
First we iterate through all attachments, checking their size with the attachment.FileSize method. If it returns 0, we know that the file has been added during the current action, and is still on the local drive, so we check the FileSystemObject's filesize. If this size is larger than the limit, we return an error string from the validation hook, that aborts the commit. Returning an empty string allows the commit to proceed.
VB Script
Base Action Validation hook:
Function Defect_Validation(actionname, actiontype)
' actionname As String
' actiontype As Long
' Defect_Validation As String
' action is AttachAuthorization
' record type name is ChangeRequest
REM Return a non-empty string explaining why the action
REM cannot commit with the current values.
REM Or, if it is valid, return an empty string value.
REM Example:
REM Dim value_info
REM Set value_info = GetFieldValue("some field")
REM If Len(value_info.GetValue()) 10 Then
REM Defect_Validation = "Must be at least 10 chars long"
REM End If
Dim fso, f, s
Defect_Validation = ""
set session = GetSession()
set attachFields = AttachmentFields
' Iterate over the attachment fields on an Entity.
For Each attachField In attachFields
set attaches = attachField.Attachments
' iterate over the attachment's field attachments
For Each myAttach In attaches
filename = myAttach.FileName
filesize = myAttach.FileSize
' INFO "filenmae is " & filename
' File size is always 0 before a first commit
If (filesize = 0) Then
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFile(filename)
filesize = f.size
End if
' INFO "filename is " & filename & " size is " & filesize
If (filesize 1500000) Then
Defect_Validation = "File " & filename & " is too big "
Exit Function
End If
Next
Next
End Function
Perl:
sub Defect_Validation {
my($actionname, $actiontype) = @_;
my $result;
# $actionname as string scalar
# $actiontype as long scalar
# $result as string scalar
# action is AttachmentSizeValidation
# record type name is ChangeRequest
# Return a non-empty string explaining why the action
cannot commit
# with its current values. If it is valid, return an
empty string.
# Example:
# my $value_info = $entity->GetFieldValue("some field");
# if (length($value_info->GetValue()) < 10) {
# $result = "Must be at least 10 chars long";
# }
my($maxFileSize) = 1024000 ; # 1 MByte
my($actionName, $actionType) = @_;
my($hookDesc) = "ChangeRequest: Base Action $actionName";
my($attachmentFields, $attachmentField) ;
my($attachments, $attachment) ;
my($numAttachFields) ;
my($numAttachments) ;
my($i, $j) ;
my ($result) = "";
$session->OutputDebugString("----- $hookDesc: Starting.\n");
# Get the list of the attachment fields in this record type
$attachmentFields = $entity->GetAttachmentFields();
$numAttachFields = $attachmentFields->Count() ;
# Iterate over the attachment fields; for each one,
validate the
# size of each attachment contained within each
attachment field
for ($i = 0; $i < $numAttachFields; $i++)
{
$attachmentField = $attachmentFields->Item($i) ;
# Obtain the attachments contained within this
attachment field
$attachments = $attachmentField->GetAttachments() ;
$numAttachments = $attachments->Count() ;
# Validate the size of each attachment
for ($j = 0; $j < $numAttachments; $j++)
{
$attachment = $attachments->Item($j) ;
$fileName = $attachment->GetFileName() ;
# Get the size of the file. CQ file size is zero
before the first
# commit, so if that is the case, get the
file size from the O/S
if ( 0 == ($fileSize = $attachment->GetFileSize()) )
{
$fileSize = (-s $fileName) ;
}
# If this file exceeds the maximum allowable
size, indicate
# that in the validation result
if ($fileSize > $maxFileSize)
{
$result = $result . "File '$fileName' in " .
$attachmentField->GetFieldName() .
" is larger than $maxFileSize bytes\n" ;
}
}
}
return $result;
}
Trackback:
http://www.ibm.com/developerworks/rational/library/3883.html
[
本帖最后由 yunshan 于 2008-1-11 12:07 编辑 ]