redmine_custom_workflows icon indicating copy to clipboard operation
redmine_custom_workflows copied to clipboard

Delete specific attachments before save issue or comment

Open DolezalDavid opened this issue 4 years ago • 8 comments

Hi, i try long time to find solution, how delete specific attachmets before save issues or comments.

Reason is simple. I use email as the one of the ways how create or comment issues. And emails usually have some logo (like corporation logo, etc.) and I have database full of logos. :-)

I think it will be quite easy to manage it, but I don't see light in the end of tunnel now. I know size of file on disk or size in px...

Someone to help? :-)

DolezalDavid avatar Nov 05 '20 11:11 DolezalDavid

What do you think about digest? Can I use it as indentificator for specific attachments? It looks it can work...

DolezalDavid avatar Nov 05 '20 14:11 DolezalDavid

I'm ruby beginner, but how said Jan Amos Komensky:"Learn, learn, learn." I try some script in console and this is my start point: Attachment.where(digest: '<hexadecimal_code>').ids It turn me back aray of ids of pictures/logos

May be I can start like this? if subject.present? if self.has_attachment? ...

Any ideas? @picman

DolezalDavid avatar Nov 06 '20 07:11 DolezalDavid

It has said Lenin.

picman avatar Nov 06 '20 08:11 picman

Hi David,

Set the observable object to "Issue Attachments":

image

Use the below code in AFTER SAVE. You can modify the file size limit by changing it in this section: "< 200.to_i.kilobytes".


if @attachment.filesize < 200.to_i.kilobytes
  if @attachment.container
    @attachment.container.attachments.delete(@attachment)
  else
    @attachment.destroy
  end
end

I hope its help!

AirTibu avatar Nov 06 '20 11:11 AirTibu

@picman Sorry. I made mistake... You have right.

@AirTibu It works nice. Thank you!

DolezalDavid avatar Nov 06 '20 14:11 DolezalDavid

if @attachment.filesize < 200.to_i.kilobytes if @attachment.container @attachment.container.attachments.delete(@attachment) else @attachment.destroy end end

Hi, I turn back to this problem. I applied your solution and on the first test looks good, but. If I send with company logo another attachment this script delete it also.

So I think about solution and here is my idea.

  1. find how many attachments are in container
  2. if there are more then one, check filesize one by one (for each?)
  3. if filesize = xyz kilobytes, then delete, else do nothing

The point of this is delete after save company logo which is always the same, ale slowly take some disc space...

What do you think about it?

Thanks.

David

DolezalDavid avatar Dec 15 '21 15:12 DolezalDavid

I cannot imagine how a logo < 200kB, although saved many time, can noticeably consume the disk space. I'd enlarge the disk space rather than implement such a hack.

picman avatar Sep 20 '22 10:09 picman

I cannot imagine how a logo < 200kB, although saved many time, can noticeably consume the disk space. I'd enlarge the disk space rather than implement such a hack. or user can run script on host machine periodicaly, that will finge full file duples and change each duplicate to symlink )

And as not to make many issuses, it will be nice to have more examples for users like me, that not good in Ruby or so ) Thanks!

qawsed55 avatar Mar 12 '23 15:03 qawsed55