Skip to content
This repository has been archived by the owner on Jun 5, 2023. It is now read-only.

Commit

Permalink
Release 0.5.1 (#16)
Browse files Browse the repository at this point in the history
* Enable upload sessions
  • Loading branch information
mikevansighem authored Dec 20, 2021
1 parent 39ae4f0 commit bf5d6e1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
8 changes: 7 additions & 1 deletion dropbox_backup/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

<!-- markdown-link-check-disable -->

## [0.5.0](https://github.com/mikevansighem/dropbox-backup/releases/tag/0.5.0) - 2021-12-20
## [0.5.1](https://github.com/mikevansighem/dropbox-backup/releases/tag/0.5.1) - 2021-12-20

<!-- markdown-link-check-enable -->

### Changed

- Use upload session for large files.

## [0.5.0](https://github.com/mikevansighem/dropbox-backup/releases/tag/0.5.0) - 2021-12-20

### Changed

- Added a logo.
- Use Dropbox Python SDK directly.
- Make `output` a optional setting.
Expand Down
2 changes: 1 addition & 1 deletion dropbox_backup/config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
name: Dropbox backup
version: "0.5.0"
version: "0.5.1"
slug: dropbox-backup
description: "Upload your Home Assistant backups to Dropbox"
url: https://github.com/mikevansighem/dropbox-backup
Expand Down
49 changes: 45 additions & 4 deletions dropbox_backup/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,55 @@
import dropbox
from dropbox.files import WriteMode
from dropbox.exceptions import ApiError, AuthError
from tqdm import tqdm

TIMEOUT = 900
CHUNK_SIZE = 4 * 1024 * 1024


# Uploads a file to Dropbox
def upload_file(dbx, file, target):

# Open the file
with open(file, 'rb') as f:
print("[INFO] Uploading " + file + " to Dropbox as " + target + "...")

try:
dbx.files_upload(f.read(), target, mode=WriteMode('add'))
print("[INFO] Uploading " + file + " to Dropbox as " + target + "...")

# Get file size
file_size = os.path.getsize(file)

# Use normal upload method if file is small.
if file_size <= CHUNK_SIZE:

print("[DEBUG] Using simple uploader.")

# Use normal upload method if file is small.
dbx.files_upload(f.read(), target, mode=WriteMode('add'))

else:

print("[DEBUG] Using upload session.")

# Use upload session for large files.
with tqdm(total=file_size, desc="Uploaded") as pbar:

upload_session_start_result = dbx.files_upload_session_start(f.read(CHUNK_SIZE))
pbar.update(CHUNK_SIZE)
cursor = dropbox.files.UploadSessionCursor(session_id=upload_session_start_result.session_id, offset=f.tell())
commit = dropbox.files.CommitInfo(path=target)

while f.tell() < file_size:

if (file_size - f.tell()) <= CHUNK_SIZE:
print(dbx.files_upload_session_finish(f.read(CHUNK_SIZE), cursor, commit))

else:
dbx.files_upload_session_append(f.read(CHUNK_SIZE), cursor.session_id, cursor.offset)
cursor.offset = f.tell()

pbar.update(CHUNK_SIZE)

except ApiError as err:
# This checks for the specific error where a user doesn't have
# enough Dropbox space quota to upload this file
Expand Down Expand Up @@ -65,8 +106,8 @@ def main(token, output_dir):
print("[INFO] Found", len(file_list), "file(s) to upload.")

# Create an instance of a Dropbox class, which can make requests to the API.
print("[INFO] Creating a Dropbox object...")
with dropbox.Dropbox(token) as dbx:
print("[DEBUG] Creating a Dropbox object.")
with dropbox.Dropbox(token, timeout=TIMEOUT) as dbx:

# Check that the access token is valid.
try:
Expand Down

0 comments on commit bf5d6e1

Please sign in to comment.