Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround to upload files >2GB in size #20

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
76 changes: 38 additions & 38 deletions doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@

```python

import seafileapi
import python_seafile

client = seafileapi.connect('http://127.0.0.1:8000', '[email protected]', 'password')
client = python_seafile.connect('http://127.0.0.1:8000', '[email protected]', 'password')
```

**Return Type**
Expand All @@ -69,9 +69,9 @@ A Client Object

```python

import seafileapi
import python_seafile

client = seafileapi.connect('http://127.0.0.1:8000', '[email protected]', 'password')
client = python_seafile.connect('http://127.0.0.1:8000', '[email protected]', 'password')
repo = client.repos.get_repo('09c16e2a-ff1a-4207-99f3-1351c3f1e507')
```

Expand All @@ -93,9 +93,9 @@ None

```python

import seafileapi
import python_seafile

client = seafileapi.connect('http://127.0.0.1:8000', '[email protected]', 'password')
client = python_seafile.connect('http://127.0.0.1:8000', '[email protected]', 'password')
repo = client.repos.get_repo('09c16e2a-ff1a-4207-99f3-1351c3f1e507')
is_readonly = repo.is_readonly()
```
Expand All @@ -114,18 +114,18 @@ None

```python

import seafileapi
import python_seafile

client = seafileapi.connect('http://127.0.0.1:8000', '[email protected]', 'password')
client = python_seafile.connect('http://127.0.0.1:8000', '[email protected]', 'password')
repo_list = client.repos.list_repos()

print repo_list
Out >>> [<seafileapi.repo.Repo at 0x7f1bb0769750>,
<seafileapi.repo.Repo at 0x7f1bb07693d0>,
<seafileapi.repo.Repo at 0x7f1bb0769a50>,
<seafileapi.repo.Repo at 0x7f1bb077cc10>,
<seafileapi.repo.Repo at 0x7f1bb077cfd0>,
<seafileapi.repo.Repo at 0x7f1bb077ca10>]
Out >>> [<python_seafile.repo.Repo at 0x7f1bb0769750>,
<python_seafile.repo.Repo at 0x7f1bb07693d0>,
<python_seafile.repo.Repo at 0x7f1bb0769a50>,
<python_seafile.repo.Repo at 0x7f1bb077cc10>,
<python_seafile.repo.Repo at 0x7f1bb077cfd0>,
<python_seafile.repo.Repo at 0x7f1bb077ca10>]

print [repo.name for repo in repo_list]
Out >>> ['alphabox',
Expand All @@ -151,9 +151,9 @@ A list of Libraries Object

```python

import seafileapi
import python_seafile

client = seafileapi.connect('http://127.0.0.1:8000', '[email protected]', 'password')
client = python_seafile.connect('http://127.0.0.1:8000', '[email protected]', 'password')
repo = client.repos.create_repo('test_repo')
```

Expand All @@ -172,9 +172,9 @@ None

```python

import seafileapi
import python_seafile

client = seafileapi.connect('http://127.0.0.1:8000', '[email protected]', 'password')
client = python_seafile.connect('http://127.0.0.1:8000', '[email protected]', 'password')
repo = client.repos.get_repo('09c16e2a-ff1a-4207-99f3-1351c3f1e507')
repo.delete()
```
Expand All @@ -194,17 +194,17 @@ None

```python

import seafileapi
import python_seafile

client = seafileapi.connect('http://127.0.0.1:8000', '[email protected]', 'password')
client = python_seafile.connect('http://127.0.0.1:8000', '[email protected]', 'password')
repo = client.repos.get_repo('09c16e2a-ff1a-4207-99f3-1351c3f1e507')
seafdir = repo.get_dir('/root')
print seafdir.__dict__
Out >>> {'client': SeafileApiClient[server=http://127.0.0.1:8000, [email protected]],
'entries': [],
'id': 'c3742dd86004d51c358845fa3178c87e4ab3aa60',
'path': '/root',
'repo': <seafileapi.repo.Repo at 0x7f2af56b1490>,
'repo': <python_seafile.repo.Repo at 0x7f2af56b1490>,
'size': 0}
```

Expand All @@ -225,9 +225,9 @@ A Directory Object

```python

import seafileapi
import python_seafile

client = seafileapi.connect('http://127.0.0.1:8000', '[email protected]', 'password')
client = python_seafile.connect('http://127.0.0.1:8000', '[email protected]', 'password')
repo = client.repos.get_repo('09c16e2a-ff1a-4207-99f3-1351c3f1e507')
seafdir = repo.get_dir('/root')

Expand Down Expand Up @@ -265,9 +265,9 @@ List of Directory and File

```python

import seafileapi
import python_seafile

client = seafileapi.connect('http://127.0.0.1:8000', '[email protected]', 'password')
client = python_seafile.connect('http://127.0.0.1:8000', '[email protected]', 'password')
repo = client.repos.get_repo('09c16e2a-ff1a-4207-99f3-1351c3f1e507')
seafdir = repo.get_dir('/root')

Expand All @@ -287,9 +287,9 @@ None

```python

import seafileapi
import python_seafile

client = seafileapi.connect('http://127.0.0.1:8000', '[email protected]', 'password')
client = python_seafile.connect('http://127.0.0.1:8000', '[email protected]', 'password')
repo = client.repos.get_repo('09c16e2a-ff1a-4207-99f3-1351c3f1e507')
seafdir = repo.get_dir('/root')

Expand All @@ -312,17 +312,17 @@ A Response Instance

```python

import seafileapi
import python_seafile

client = seafileapi.connect('http://127.0.0.1:8000', '[email protected]', 'password')
client = python_seafile.connect('http://127.0.0.1:8000', '[email protected]', 'password')
repo = client.repos.get_repo('09c16e2a-ff1a-4207-99f3-1351c3f1e507')
seaffile = repo.get_file('/root/test.md')

print seafile.__dict__
Out >>> {'client': SeafileApiClient[server=http://127.0.0.1:8000, [email protected]],
'id': '0000000000000000000000000000000000000000',
'path': '/root/test.md',
'repo': <seafileapi.repo.Repo at 0x7f2af56b1490>,
'repo': <python_seafile.repo.Repo at 0x7f2af56b1490>,
'size': 0}
```

Expand All @@ -344,9 +344,9 @@ None

```python

import seafileapi
import python_seafile

client = seafileapi.connect('http://127.0.0.1:8000', '[email protected]', 'password')
client = python_seafile.connect('http://127.0.0.1:8000', '[email protected]', 'password')
repo = client.repos.get_repo('09c16e2a-ff1a-4207-99f3-1351c3f1e507')
seaffile = repo.get_file('/root/test.md')

Expand All @@ -366,9 +366,9 @@ File Content

```python

import seafileapi
import python_seafile

client = seafileapi.connect('http://127.0.0.1:8000', '[email protected]', 'password')
client = python_seafile.connect('http://127.0.0.1:8000', '[email protected]', 'password')
repo = client.repos.get_repo('09c16e2a-ff1a-4207-99f3-1351c3f1e507')
seafdir = repo.get_dir('/root')

Expand All @@ -390,9 +390,9 @@ A File Object of new empty file

```python

import seafileapi
import python_seafile

client = seafileapi.connect('http://127.0.0.1:8000', '[email protected]', 'password')
client = python_seafile.connect('http://127.0.0.1:8000', '[email protected]', 'password')
repo = client.repos.get_repo('09c16e2a-ff1a-4207-99f3-1351c3f1e507')
seafdir = repo.get_dir('/root')

Expand All @@ -417,9 +417,9 @@ None

```python

import seafileapi
import python_seafile

client = seafileapi.connect('http://127.0.0.1:8000', '[email protected]', 'password')
client = python_seafile.connect('http://127.0.0.1:8000', '[email protected]', 'password')
repo = client.repos.get_repo('09c16e2a-ff1a-4207-99f3-1351c3f1e507')
seaffile = repo.get_file('/root/test.md')

Expand Down
2 changes: 1 addition & 1 deletion pytest.ini
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[pytest]
addopts = -s -v --doctest-modules --ignore=build --ignore=dist --ignore=seafileapi.egg-info --ignore=setup.py
addopts = -s -v --doctest-modules --ignore=build --ignore=dist --ignore=python_seafile.egg-info --ignore=setup.py
3 changes: 2 additions & 1 deletion seafileapi/__init__.py → python_seafile/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from seafileapi.client import SeafileApiClient
from python_seafile.client import SeafileApiClient
from python_seafile._version import __version__

def connect(server, username, password):
client = SeafileApiClient(server, username, password)
Expand Down
1 change: 1 addition & 0 deletions python_seafile/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.1.2"
File renamed without changes.
20 changes: 17 additions & 3 deletions seafileapi/client.py → python_seafile/client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import requests
from seafileapi.utils import urljoin
from seafileapi.exceptions import ClientHttpError
from seafileapi.repos import Repos
from python_seafile.utils import urljoin
from python_seafile.exceptions import ClientHttpError
from python_seafile.repos import Repos
from os.path import getsize

from requests_toolbelt import MultipartEncoder

MAX_SIZE = 2147483647 #https://github.com/psf/requests/issues/2717

class SeafileApiClient(object):
"""Wraps seafile web api"""
Expand Down Expand Up @@ -60,6 +65,15 @@ def _send_request(self, method, url, *args, **kwargs):
expected = kwargs.pop('expected', 200)
if not hasattr(expected, '__iter__'):
expected = (expected, )
if 'files' in kwargs and hasattr(kwargs['files']['file'][1], 'name') and getsize(kwargs['files']['file'][1].name) > MAX_SIZE:
#see https://github.com/psf/requests/issues/2717#issuecomment-724725392
m = MultipartEncoder(
fields={'file': (kwargs['files']['file'][1].name, open(kwargs['files']['file'][1].name, 'rb'), 'text/plain'),
'parent_dir': kwargs['files']['parent_dir']}
)
del kwargs['files']
kwargs['data'] = m
kwargs['headers']['Content-Type'] = m.content_type
resp = requests.request(method, url, *args, **kwargs)
if resp.status_code not in expected:
msg = 'Expected %s, but get %s' % \
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions seafileapi/files.py → python_seafile/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import posixpath
import re
from seafileapi.utils import querystr
from python_seafile.utils import querystr

ZERO_OBJ_ID = '0000000000000000000000000000000000000000'

Expand Down Expand Up @@ -185,7 +185,7 @@ def upload_local_file(self, filepath, name=None):
Return a :class:`SeafFile` object of the newly uploaded file.
"""
name = name or os.path.basename(filepath)
with open(filepath, 'r') as fp:
with open(filepath, 'rb') as fp:
return self.upload(fp, name)

def _get_upload_link(self):
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions seafileapi/repo.py → python_seafile/repo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from urllib.parse import urlencode
from seafileapi.files import SeafDir, SeafFile
from seafileapi.utils import raise_does_not_exist
from python_seafile.files import SeafDir, SeafFile
from python_seafile.utils import raise_does_not_exist

class Repo(object):
"""
Expand Down
13 changes: 11 additions & 2 deletions seafileapi/repos.py → python_seafile/repos.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from seafileapi.repo import Repo
from seafileapi.utils import raise_does_not_exist
from python_seafile.repo import Repo
from python_seafile.utils import raise_does_not_exist

class Repos(object):
def __init__(self, client):
Expand All @@ -21,6 +21,15 @@ def get_repo(self, repo_id):
repo_json = self.client.get('/api2/repos/' + repo_id).json()
return Repo.from_json(self.client, repo_json)

@raise_does_not_exist('The requested library does not exist')
def get_default_repo(self):
"""Get the default repo.

Raises :exc:`DoesNotExist` if no default repo exists.
"""
repo_json = self.client.get('/api2/default-repo').json()
return self.get_repo(repo_json['repo_id'])

def list_repos(self):
repos_json = self.client.get('/api2/repos/').json()
return [Repo.from_json(self.client, j) for j in repos_json]
2 changes: 1 addition & 1 deletion seafileapi/utils.py → python_seafile/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import random
from functools import wraps
from urllib.parse import urlencode
from seafileapi.exceptions import ClientHttpError, DoesNotExist
from python_seafile.exceptions import ClientHttpError, DoesNotExist

def randstring(length=0):
if length == 0:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
requests
requests-toolbelt
23 changes: 17 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,31 @@
from setuptools import setup, find_packages
import re

__version__ = '0.1.1'

def get_version():
#https://stackoverflow.com/a/7071358/5122790
VERSIONFILE="python_seafile/_version.py"
verstrline = open(VERSIONFILE, "rt").read()
VSRE = r"^__version__ = ['\"]([^'\"]*)['\"]"
mo = re.search(VSRE, verstrline, re.M)
if mo:
return mo.group(1)
else:
raise RuntimeError("Unable to find version string in %s." % (VERSIONFILE,))

setup(name='seafileapi',
version=__version__,

setup(name='python_seafile',
version=get_version(),
license='BSD',
description='Client interface for Seafile Web API',
author='Shuai Lin',
author_email='[email protected]',
author_email='[email protected], [email protected]',
url='http://seafile.com',
platforms=['Any'],
packages=find_packages(),
install_requires=['requests'],
install_requires=['requests', 'requests-toolbelt'],
classifiers=['Development Status :: 4 - Beta',
'License :: OSI Approved :: BSD License',
'Operating System :: OS Independent',
'Programming Language :: Python'],
'Programming Language :: Python 3'],
)
4 changes: 2 additions & 2 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os
import pytest

import seafileapi
import python_seafile
from tests.utils import randstring

SERVER = os.environ.get('SEAFILE_TEST_SERVER_ADDRESS', 'http://127.0.0.1:8000')
Expand All @@ -14,7 +14,7 @@

@pytest.fixture(scope='session')
def client():
return seafileapi.connect(SERVER, USER, PASSWORD)
return python_seafile.connect(SERVER, USER, PASSWORD)

@pytest.yield_fixture(scope='function')
def repo(client):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_repos.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from seafileapi.exceptions import DoesNotExist
from python_seafile.exceptions import DoesNotExist
from tests.utils import randstring

def test_create_delete_repo(client):
Expand Down