Skip to content

Commit

Permalink
Initial Project
Browse files Browse the repository at this point in the history
SupianIDz committed Feb 23, 2023
0 parents commit ffed32a
Showing 34 changed files with 8,592 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
root = true

[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false

[*.yml]
indent_style = space
indent_size = 2
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
* text=auto eol=lf
/.github export-ignore
.scrutinizer.yml export-ignore
BACKERS.md export-ignore
CONTRIBUTING.md export-ignore
CHANGELOG.md export-ignore
52 changes: 52 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: Build & Release

on:
release:
types: [ published ]

jobs:
release:
runs-on: ubuntu-latest

steps:
- name: Checkout Code
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.1
extensions: dom, libxml, mbstring, zip, pdo, sqlite, pdo_sqlite, curl, inotify, bcmath
tools: composer:v2
coverage: none

- name: Install Dependencies
run: |
composer install --no-dev --prefer-dist
- name: Set Variables
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV

- name: Build Binary
run: php gphoto app:build --build-version=${{ env.RELEASE_VERSION }}

- name: Automatic Releases
uses: actions/github-script@v3
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require("fs").promises;
const { repo: { owner, repo }, sha } = context;
const release = await github.repos.getReleaseByTag({
owner, repo,
tag: process.env.GITHUB_REF.replace("refs/tags/", ""),
});
console.log({ release });
await github.repos.uploadReleaseAsset({
owner, repo,
name: "gphoto",
data: await fs.readFile("./builds/gphoto"),
release_id: release.data.id,
});
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/vendor
/.idea
/.vscode
/.vagrant
.phpunit.result.cache
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Octopy ID & Supian M

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
148 changes: 148 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
# Google Photo CLI

GPhoto streamlines the photo uploading process and helps you keep your photo library organized. Say goodbye to manual photo uploads and hello to efficient photo management with
GPhoto.

## Features

- **No Duplicate Processing** - GPhoto uses a cache with keys based on hash files to ensure that the same files are not processed repeatedly, saving you time and storage.
- **No Duplicate Album Names** - GPhoto fetches album names from Google and compares them to ensure there are no duplicates, helping you keep your photo library organized and easy
to navigate.
- **Automatic Token Refresh** - GPhoto automatically refreshes the token if an error occurs during the photo upload process, ensuring a seamless and uninterrupted uploading
experience.
- **Secure Local Storage** - All authentication data and cache is securely stored locally to ensure your privacy and security. GPhoto does not collect any information from you.
## Installation

Please make sure you have the following requirements installed below:

- PHP 8.1 with curl, inotify, pcntl, and bcmatch extensions.

Download the latest release from the [releases page](https://github.com/OctopyID/GPhotoCLI/releases) or clone this repository.

## Usage

Before using this application, you need to create a Google Cloud Platform project and enable the Google Photos Library API and create
a [OAuth 2.0 Client ID](https://developers.google.com/photos/library/guides/overview#authorization).

The authentication file should look like this :

```json
{
"web": {
"client_id": "xxxxxxx.apps.googleusercontent.com",
"project_id": "xxxxxxx",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": "xxxxxxx",
"redirect_uris": [
"http://localhost:3000"
]
}
}
```

### 1. Authorization Token

```bash
php gphoto auth:create foo --auth=./path/to/auth.json --host=http://localhost:3000
```

The expired token can be renewed with the following command

```bash
php gphoto auth:reload foo
```

Make sure you register http://localhost:3000 on Authorised redirect URIs and no service uses this port.

### 2. Photos

#### 2.1 Upload Single File

```bash
php gphoto upload:photo ./path/to/photo.png --auth=foo
```

or you can use ` --album` to upload to a specific album.

```bash
php gphoto upload:photo ./path/to/MyPicture.png --auth=foo --album="My Album"
```

#### 2.2 Upload Multiple Files

To upload multiple files, you need to create a directory that contains all photos with the following structure:

```
. Pictures
├── MyPicture 1.jpg
├── MyPicture 2.jpg
├── MyPicture 3.jpg
└── MyPicture 4.jpg
```

```bash
php gphoto upload:photo ./path/to/Pictures --auth=foo
```

or you can use ` --album` to upload to a specific album.

```bash
php gphoto upload:photo ./path/to/Pictures --auth=foo --album="My Album"
```

### 3. Albums

#### 3.1 Upload Single Album

To upload a single album, you need to create a directory that contains all photos with the following structure:

```
. MyAlbum
├── MyPicture 1.jpg
├── MyPicture 2.jpg
├── MyPicture 3.jpg
├── MyPicture 4.jpg
└── MyPicture 5.jpg
```

```bash
php gphoto upload:album ./path/to/MyAlbum --auth=foo
```

This will upload all files in the `MyAlbum` directory to Google Photos and create an album with the `MyAlbum` name.

#### 3.2 Upload Multiple Albums

To upload multiple albums, you need to create a directory that contains all albums with the following structure:

```
. MyAlbumCollection
├── MyAlbum 1
│ ├── MyPicture 1.jpg
│ ├── MyPicture 2.jpg
│ ├── MyPicture 3.jpg
│ └── MyPicture 4.jpg
└── MyAlbum 2
├── MyPicture 1.jpg
├── MyPicture 2.jpg
├── MyPicture 3.jpg
└── MyPicture 4.jpg
```

```bash
php gphoto upload:album ./path/to/MyAlbumCollection --auth=foo --multiple
```

This will upload all files in the `MyAlbumCollection` directory to Google Photos and create an album with `MyAlbum 1` and `MyAlbum 2` names.

## Credits

- [Supian M](https://github.com/SupianIDz)
- [Octopy ID](https://github.com/OctopyID)

## License

The MIT License (MIT). Please see [License File](LICENSE) for more information.
79 changes: 79 additions & 0 deletions app/Auth/Credentials/Credential.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace App\Auth\Credentials;

use App\Exceptions\InvalidTokenException;
use App\GPhoto;
use Google\Auth\Credentials\UserRefreshCredentials;
use GuzzleHttp\Exception\ClientException;

class Credential
{
/**
* @param GPhoto $gphoto
*/
public function __construct(protected GPhoto $gphoto)
{
//
}

/**
* @return UserRefreshCredentials
* @throws InvalidTokenException
*/
public function getCredential() : UserRefreshCredentials
{
if (file_exists($this->gphoto->path('storage/state'))) {
return $this->getFromCache();
}

return $this->getFromGoogle();
}

/**
* @return bool
*/
public function deleteCredential() : bool
{
if (file_exists($this->gphoto->path('storage/state'))) {
return unlink($this->gphoto->path('storage/state'));
}

return true;
}

/**
* @return UserRefreshCredentials
* @throws InvalidTokenException
*/
private function getFromGoogle() : UserRefreshCredentials
{
try {
$credential = new UserRefreshCredentials($this->gphoto->scopes(), [
'client_id' => $this->gphoto->config('auth.client_id'),
'client_secret' => $this->gphoto->config('auth.client_secret'),
'refresh_token' => $this->gphoto->oauth()->fetchNewAccessToken(),
]);

if (file_put_contents($this->gphoto->path('storage/state'), serialize($credential))) {
return $credential;
}
} catch (ClientException $exception) {
if ($this->deleteCredential()) {
throw new InvalidTokenException;
}

throw $exception;
}
}

/**
* @return UserRefreshCredentials
*/
private function getFromCache() : UserRefreshCredentials
{
return unserialize(file_get_contents(
$this->gphoto->path('storage/state')
));
}
}
43 changes: 43 additions & 0 deletions app/Auth/OAuth2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Auth;

use App\GPhoto;
use Google\Auth\OAuth2 as GoogleOAuth2;
use Illuminate\Support\Arr;

class OAuth2 extends GoogleOAuth2
{
/**
* @var OAuth2
*/
protected OAuth2 $oauth;

/**
*
* @param GPhoto $gphoto
*/
public function __construct(protected GPhoto $gphoto)
{
parent::__construct([
'scope' => $this->gphoto->scopes(),
'redirectUri' => $this->gphoto->config('host'),
'clientId' => $this->gphoto->config('auth.client_id'),
'clientSecret' => $this->gphoto->config('auth.client_secret'),
'tokenCredentialUri' => 'https://www.googleapis.com/oauth2/v4/token',
'authorizationUri' => 'https://accounts.google.com/o/oauth2/v2/auth',
]);
}

/**
* @return string
*/
public function fetchNewAccessToken() : string
{
$this->setCode(file_get_contents($this->gphoto->path(
'storage/token'
)));

return Arr::get($this->fetchAuthToken(), 'access_token');
}
}
Loading

0 comments on commit ffed32a

Please sign in to comment.