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

Fix cmd line version #23

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
1 change: 1 addition & 0 deletions remotesync-api/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target/
persistence.xml
8 changes: 8 additions & 0 deletions remotesync-api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM eclipse-temurin:17-alpine

WORKDIR /opt/app
COPY target/lib /opt/app/lib
COPY target/classes /opt/app

EXPOSE 8080
ENTRYPOINT ["java","-cp","/opt/app:/opt/app/lib/*","org.piwigo.remotesync.api.Main"]
38 changes: 37 additions & 1 deletion remotesync-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
are made available under the terms of the GNU Public License v2.0
which accompanies this distribution, and is available at
http://www.gnu.org/licenses/old-licenses/gpl-2.0.html

Contributors:
Matthieu Helleboid - initial API and implementation
-->
Expand All @@ -13,7 +13,29 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>remotesync-api</artifactId>
<name>Piwigo Remote Sync API</name>

<properties>
<hibernate.version>5.6.14.Final</hibernate.version>
<mariadb.version>3.0.6</mariadb.version>
</properties>

<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core-jakarta</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>${mariadb.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>

<dependency>
<groupId>args4j</groupId>
<artifactId>args4j</artifactId>
Expand Down Expand Up @@ -138,6 +160,20 @@
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<parent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
* are made available under the terms of the GNU Public License v2.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
*
* Contributors:
* Matthieu Helleboid - initial API and implementation
******************************************************************************/
package org.piwigo.remotesync.api;

import org.piwigo.remotesync.api.AbstractMain;
import org.piwigo.remotesync.api.sync.LoginJob;
import org.piwigo.remotesync.api.sync.SyncJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -20,9 +22,12 @@ public class Main extends AbstractMain {
public static void main(String[] args) {
new Main().run(args);
}

protected void start() {
logger.debug("will start batch Remotesync");
LoginJob preJob = new LoginJob();
preJob.execute();
new SyncJob(preJob).execute();
}

// // TODO implement dry run
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* are made available under the terms of the GNU Public License v2.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
*
* Contributors:
* Matthieu Helleboid - initial API and implementation
******************************************************************************/
Expand All @@ -20,6 +20,7 @@
import org.piwigo.remotesync.api.Constants;
import org.piwigo.remotesync.api.ISyncConfiguration;
import org.piwigo.remotesync.api.cache.LegacyCache;
import org.piwigo.remotesync.menalto.Importer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -30,10 +31,12 @@ public abstract class SyncDirectoryWalker extends DirectoryWalker<File> {
protected ISyncConfiguration syncConfiguration;
protected File startDirectory;
protected Map<File, LegacyCache> legacyCaches = new HashMap<File, LegacyCache>();
private Importer importer;

protected SyncDirectoryWalker(ISyncConfiguration syncConfiguration) {
super(null, Constants.IMAGE_EXTENSIONS_FILTER, -1);
this.syncConfiguration = syncConfiguration;
importer = new Importer(syncConfiguration.getDirectory());
}

@Override
Expand All @@ -45,6 +48,9 @@ protected void handleDirectoryStart(File directory, int depth, Collection<File>
if (directory.equals(startDirectory))
return;

if( !importer.shouldImport(directory.getAbsolutePath()))
return;

if (legacyCache.getAlbumCacheElement() != null) {
logger.debug("Album is already in cache : " + directory);
} else {
Expand All @@ -55,13 +61,18 @@ protected void handleDirectoryStart(File directory, int depth, Collection<File>
// FIXME : ignore me
}
logger.info("Creating album for " + directory);
legacyCache.addAlbum(createAlbum(directory, parentAlbumId));
Integer albumId = createAlbum(directory, parentAlbumId);
legacyCache.addAlbum(albumId);
importer.addAlbum(directory.getAbsolutePath(), albumId);
}
}

protected void handleFile(File file, int depth, java.util.Collection<File> results) throws IOException {
LegacyCache legacyCache = legacyCaches.get(file.getParentFile());

if( !importer.shouldImport(file.getAbsolutePath()))
return;

if (legacyCache.containsImage(file)) {
logger.debug("Image already in cache : " + file);
} else {
Expand All @@ -72,7 +83,9 @@ protected void handleFile(File file, int depth, java.util.Collection<File> resul
// FIXME : ignore me
}
logger.info("Uploading " + file.getName() + " in album with ID " + albumId);
legacyCache.addImage(file, createImage(file, albumId));
Integer imageId = createImage(file, albumId);
legacyCache.addImage(file, imageId);
importer.addImage(file.getAbsolutePath(), albumId, imageId);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.piwigo.remotesync.menalto;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.NamedQueries;
import jakarta.persistence.NamedQuery;
import jakarta.persistence.Table;

@Entity
@Table(name = "import_item")
@NamedQueries({
@NamedQuery(name = "ImportItem.findAll", query = "select t from ImportItem t")
})
public class ImportItem
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Integer myId;

@Column(name = "path", nullable = false, length = 255)
private String myPath;

@Column(name = "url", nullable = false, length = 255)
private String myUrl;

@Column(name = "album_id", nullable = false)
private Integer myAlbumId;

@Column(name = "image_id", nullable = true)
private Integer myImageId;

public Integer getId()
{
return myId;
}

public void setId(Integer id)
{
myId = id;
}

public String getPath()
{
return myPath;
}

public ImportItem setPath(String path)
{
myPath = path;
return this;
}

public String getUrl()
{
return myUrl;
}

public ImportItem setUrl(String url)
{
myUrl = url;
return this;
}

public Integer getAlbumId()
{
return myAlbumId;
}

public ImportItem setAlbumId(Integer albumId)
{
myAlbumId = albumId;
return this;
}

public Integer getImageId()
{
return myImageId;
}

public ImportItem setImageId(Integer imageId)
{
myImageId = imageId;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.piwigo.remotesync.menalto;

import jakarta.persistence.EntityManager;
import org.piwigo.remotesync.api.sync.SyncDirectoryWalker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Importer
{
private static final Logger logger = LoggerFactory.getLogger(Importer.class);

private static final String PREFIX = "albums";
private final EntityManager myEntityManager;
private final String myStartDirectory;

public Importer(String directory)
{
PersistenceManager persistenceManager = new PersistenceManager();
myEntityManager = persistenceManager.getEntityManager();
myStartDirectory = directory + "/" + PREFIX;
}

public boolean shouldImport(String path)
{
return path.startsWith(myStartDirectory);
}

public void addAlbum(String path, Integer albumId)
{
addImportItem(path, albumId, null);
}

public void addImage(String path, Integer albumId, Integer imageId)
{
addImportItem(path, albumId, imageId);
}

private void addImportItem(String path, Integer albumId, Integer imageId)
{
String relativePath = getRelativePath(path);
Item item = forPath(relativePath);
ImportItem importItem = new ImportItem().setPath(relativePath).setAlbumId(albumId).setImageId(imageId).setUrl(item.getUrl());
addImportItem(importItem);
}

private String getRelativePath(String path)
{
String relative = path.replace(myStartDirectory, "");
return relative.startsWith("/") ? relative.substring(1) : relative;
}

private void addImportItem(ImportItem importItem)
{
myEntityManager.getTransaction().begin();
myEntityManager.persist(importItem);
myEntityManager.getTransaction().commit();
}

private Item forPath(String path)
{
try
{
return myEntityManager.createNamedQuery(Item.FIND_BY_PATH, Item.class)
.setParameter(Item.PATH, path)
.getSingleResult();
} catch (Exception e)
{
logger.info("Error fetch info for entity with path:" + path);
throw e;
}
}
}
Loading