Skip to content

Commit

Permalink
copy photos from a specific date
Browse files Browse the repository at this point in the history
  • Loading branch information
paris-plated committed Apr 28, 2019
1 parent a7cc223 commit ead1f4d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 17 deletions.
66 changes: 54 additions & 12 deletions src/app.jsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,46 @@
import React, { Component } from 'react';
import { remote } from 'electron';
import storage from 'electron-json-storage';
import { copyPhotos } from './copy-photos';
import React, { Component } from "react";
import { remote } from "electron";
import storage from "electron-json-storage";
import { copyPhotos } from "./copy-photos";

class App extends Component {
constructor(props) {
super(props);
this.state = {};
this.state = {
libraryPath: "",
storagePath: "",
fromDate: "",
lastSyncDate: new Date(),
iPhoneOnly: true
};

this.selectPhotoLibraryPath = this.selectPhotoLibraryPath.bind(this);
this.selectStoragePath = this.selectStoragePath.bind(this);
this.copyPhotos = this.copyPhotos.bind(this);
this.setFromDate = this.setFromDate.bind(this);
}

componentDidMount() {
storage.getMany(['libraryPath', 'storagePath'], (error, data) => {
storage.getMany(["libraryPath", "storagePath", "fromDate", "lastSyncDate", "iPhoneOnly"], (error, data) => {
if (error) throw error;
this.setState({
libraryPath: data.libraryPath.value,
storagePath: data.storagePath.value
storagePath: data.storagePath.value,
fromDate: data.fromDate.value,
lastSyncDate: new Date(data.lastSyncDate.value),
iPhoneOnly: data.iPhoneOnly.value // this might not be bool but a string
});
});
}

selectPhotoLibraryPath() {
let options = {properties: ['openFile']};
let options = {properties: ["openFile"]};

remote.dialog.showOpenDialog(options, (filepaths) => {
if (filepaths && filepaths.length === 1) {
let filepath = filepaths[0];

storage.set('libraryPath', { value: filepath }, (error) => {
storage.set("libraryPath", { value: filepath }, (error) => {
if (error) throw error;
this.setState({libraryPath: filepath});
});
Expand All @@ -39,13 +49,13 @@ class App extends Component {
}

selectStoragePath() {
let options = {properties: ['openDirectory', 'createDirectory']};
let options = {properties: ["openDirectory", "createDirectory"]};

remote.dialog.showOpenDialog(options, (filepaths) => {
if (filepaths && filepaths.length === 1) {
let filepath = filepaths[0];

storage.set('storagePath', { value: filepath }, (error) => {
storage.set("storagePath", { value: filepath }, (error) => {
if (error) throw error;
this.setState({storagePath: filepath});
});
Expand All @@ -54,8 +64,28 @@ class App extends Component {

}

setFromDate(e) {
const fromDate = e.target.value;

storage.set("fromDate", { value: fromDate }, (error) => {
if (error) throw error;
this.setState({ fromDate });
});
}

copyPhotos() {
copyPhotos(this.state.libraryPath, this.state.storagePath);
copyPhotos(
this.state.libraryPath,
this.state.storagePath,
this.state.fromDate
);

const now = new Date();

storage.set("lastSyncDate", { value: now }, (error) => {
if (error) throw error;
this.setState({ lastSyncDate: now });
});
}

render() {
Expand Down Expand Up @@ -83,6 +113,18 @@ class App extends Component {
</button>
</div>
</div>
<div>
<h4>
<label htmlFor="from-date">From Date</label>
</h4>
<p>Last photo scyn date: {this.state.lastSyncDate.toLocaleDateString()}</p>
<input
id="from-date"
type="date"
value={this.state.fromDate}
onChange={this.setFromDate}
/>
</div>
<div>
<button onClick={this.copyPhotos}>
Copy Photos
Expand Down
20 changes: 15 additions & 5 deletions src/copy-photos.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { exec } from "child_process";

const { COPYFILE_EXCL } = fs.constants;

export const copyPhotos = (libraryPath, storagePath) => {
const photos = getPhotos(libraryPath);
export const copyPhotos = (libraryPath, storagePath, fromDate) => {
const photos = getPhotos(libraryPath, fromDate);
const totalPhotosCount = photos.length;

if (photos) {
Expand Down Expand Up @@ -35,8 +35,7 @@ export const copyPhotos = (libraryPath, storagePath) => {
}
};

const getPhotos = (libraryPath) => {
const allPhotos = [];
const getPhotos = (libraryPath, fromDate) => {
const masterPath = path.join(libraryPath, "/Masters")

try {
Expand All @@ -46,7 +45,8 @@ const getPhotos = (libraryPath) => {

return;
}


const allPhotos = [];
const years = fs.readdirSync(masterPath);

years.forEach((year) => {
Expand All @@ -58,6 +58,10 @@ const getPhotos = (libraryPath) => {
const days = fs.readdirSync(monthPath);

days.forEach((day) => {
if (fromDate && !dateInFromRange(year, month, day, fromDate)) {
return;
}

const dayPath = path.join(monthPath, day);
const times = fs.readdirSync(dayPath);

Expand All @@ -79,3 +83,9 @@ const getPhotos = (libraryPath) => {
return allPhotos;
};

const dateInFromRange = (y, m, d, fromDate) => {
const date = new Date(`${y}-${m}-${d}`);
const dateFrom = new Date(fromDate);

return (date >= dateFrom);
}

0 comments on commit ead1f4d

Please sign in to comment.