Re: [Trac] Downloading all attachments

Jun Omae <[email protected]> Thu, 24 Oct 2024 03:38:04 +0900
Newsgroups gmane.comp.version-control.subversion.trac.general
Message-ID <[email protected]>
On 2024/10/23 23:22, 'Wätzold Plaum' via Trac Users wrote:
> Hi,
> I want to backup all Attachments in a way that they can be linked to the tickets. I can download the attachments-folder, but there we have some kind of md5-hash encryption. The attachments tabel contains the clear names. Any attempt to encode the file name of the attachment to md5 or sh1 failed. 
> 
> Any idea how to get all the attachments with clear names?
> 
> I'm running track-admin 1.2.6
> 
> Best Regards,
> Wätzold

See `Attachment._get_path()` at https://trac.edgewall.org/browser/tags/trac-1.2.6/trac/attachment.py#L727

[[[
import os, sys
from trac.env import Environment
from trac.attachment import Attachment

def main(args):
    if not args:
        sys.stderr.write('Usage: %s envpaths\n' % sys.argv[0])
        sys.exit(1)
    for arg in args:
        env = Environment(arg)
        list_attachments(env)
        env.shutdown()

if hasattr(Environment, 'attachments_dir'):
    get_attachments_dir = lambda env: env.attachments_dir
else:
    get_attachments_dir = lambda env: \
        os.path.join(os.path.normpath(env.path), 'files', 'attachments')

def list_attachments(env):
    out = sys.stdout
    if hasattr(out, 'buffer'):
        out = out.buffer
    attachments_dir = get_attachments_dir(env)
    with env.db_query as db:
        cursor = db.cursor()
        cursor.execute('SELECT type, id, filename FROM attachment '
                       'ORDER BY type, id')
        for type_, id_, filename in cursor:
            local_path = Attachment._get_path(attachments_dir, type_, id_,
                                              filename)
            line = u'\t'.join([type_, id_, filename, local_path]) + u'\n'
            out.write(line.encode('utf-8'))

if __name__ == '__main__':
    main(sys.argv[1:])
]]]

-- 
Jun Omae <[email protected]> (大前 潤)

-- 
You received this message because you are subscribed to the Google Groups "Trac Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to trac-users+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/[email protected]
To view this discussion visit https://groups.google.com/d/msgid/trac-users/59ae5d22-139e-4689-9c25-bfa47a86b704%40gmail.com.