Skip to content

Commit

Permalink
[sftp] Add implementations of get_(modified|accessed)_time (#1347)
Browse files Browse the repository at this point in the history
  • Loading branch information
tompap authored Jul 6, 2024
1 parent 6c494a0 commit ea25226
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@ Azure
- Add ``mode`` kwarg to ``.url()`` to support creation of signed URLs for upload (`#1414`_)
- Fix fetching user delegation key when custom domain is enabled (`#1418`_)

SFTP
----

- Add implementations of ``get_(modified|accessed)_time`` (`#1347`_)

.. _#1399: https://github.com/jschneier/django-storages/pull/1399
.. _#1381: https://github.com/jschneier/django-storages/pull/1381
.. _#1402: https://github.com/jschneier/django-storages/pull/1402
.. _#1403: https://github.com/jschneier/django-storages/pull/1403
.. _#1414: https://github.com/jschneier/django-storages/pull/1414
.. _#1417: https://github.com/jschneier/django-storages/pull/1417
.. _#1418: https://github.com/jschneier/django-storages/pull/1418
.. _#1347: https://github.com/jschneier/django-storages/pull/1347


1.14.3 (2024-05-04)
Expand Down
14 changes: 14 additions & 0 deletions storages/backends/sftpstorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import os
import posixpath
import stat
from datetime import datetime
from urllib.parse import urljoin

import paramiko
from django.core.files.base import File
from django.utils import timezone
from django.utils.deconstruct import deconstructible
from paramiko.util import ClosingContextManager

Expand Down Expand Up @@ -190,6 +192,18 @@ def size(self, name):
remote_path = self._remote_path(name)
return self.sftp.stat(remote_path).st_size

def get_accessed_time(self, name):
remote_path = self._remote_path(name)
utime = self.sftp.stat(remote_path).st_atime
ts = datetime.fromtimestamp(utime)
return timezone.make_aware(ts) if setting("USE_TZ") else ts

def get_modified_time(self, name):
remote_path = self._remote_path(name)
utime = self.sftp.stat(remote_path).st_mtime
ts = datetime.fromtimestamp(utime)
return timezone.make_aware(ts) if setting("USE_TZ") else ts

def url(self, name):
if self._base_url is None:
raise ValueError("This file is not accessible via a URL.")
Expand Down

0 comments on commit ea25226

Please sign in to comment.