human
can you write a minimal code snippet using python that demonstrates how to upload a large 1GB file onto google drive?
gpt
Sure. The followings steps should help with that. 

1. First ensure that your python environment is properly configured with the Google Drive API. Information on setup can be found here: [https://developers.google.com/drive/api/guides/about-sdk](https://developers.google.com/drive/api/guides/about-sdk).

2. Once setup is complete modify the following snippet to suit your specific use case.

```python
from __future__ import print_function

import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from googleapiclient.http import MediaFileUpload


def upload():
    """Upload file
    Returns: ID of the file uploaded

    Load pre-authorized user credentials from the environment.
    TODO(developer) - See https://developers.google.com/identity
    for guides on implementing OAuth2 for the application.
    """
    creds, _ = google.auth.default()

    try:
        # create drive api client
        service = build('drive', 'v3', credentials=creds)

        file_metadata = {
            'name': 'My File',
            'mimeType': 'application/octet-stream'
        }
        media = MediaFileUpload('file.bin', mimetype= 'application/octet-stream',
                                resumable=True)
        # pylint: disable=maybe-no-member
        file = service.files().create(body=file_metadata, media_body=media,
                                      fields='id').execute()
        print(F'File with ID: "{file.get("id")}" has been uploaded.')

    except HttpError as error:
        print(F'An error occurred: {error}')
        file = None

    return file.get('id')
```
human
What if I wanted to detect the file type on upload and only allow certain file types to be uploaded?
gpt
To detect the file type on upload and only allow certain file types to be uploaded, you can modify the code I provided earlier to include file type checking before uploading the file. Here's an updated code snippet that demonstrates how to do this using the python-magic library:

```python
import os
import magic
import requests
from google.oauth2.credentials import Credentials

# Set up the credentials for the Google Drive API
creds = Credentials.from_authorized_user_file(os.path.expanduser('path/to/credentials.json'))

# Set the file path of the file you want to upload
file_path = 'path/to/your/large_file'

# Define the allowed file types
allowed_types = ['image/png', 'image/jpeg', 'image/gif', 'application/pdf']

# Check the file type using magic
mime = magic.Magic(mime=True)
file_type = mime.from_file(file_path)

# If the file type is not allowed, raise an exception
if file_type not in allowed_types:
    raise Exception('File type not allowed')

# Set the upload URL and metadata for the file
url = 'https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable'
headers = {'Authorization': 'Bearer ' + creds.token, 'Content-Type': 'application/json'}
params = {'name': 'your_file_name'}

# Send the initial request to start the upload and get the session URL
response = requests.post(url, headers=headers, json=params)
session_url = response.headers['Location']

# Open the file in binary mode and stream it to the session URL in chunks
with open(file_path, 'rb') as f:
    chunk_size = 1024 * 1024 * 5 # 5MB chunks
    while True:
        chunk = f.read(chunk_size)
        if not chunk:
            break
        headers = {'Content-Length': str(len(chunk)), 'Content-Range': 'bytes {}-{}/{}'.format(start, start+len(chunk)-1, total_size)}
        response = requests.put(session_url, headers=headers, data=chunk)
        start += len(chunk)
        
print('Upload complete!')
```

In this updated code snippet, we first define a list of allowed file types. We then use the python-magic library to detect the file type of the file being uploaded, and raise an exception if the file type is not allowed. If the file type is allowed, we proceed with the upload as before.

Note that in order to use python-magic, you will need to have it installed on your system. You can install it using pip by running pip install python-magic.