Skip to content

Commit

Permalink
Share GPX (#978)
Browse files Browse the repository at this point in the history
* remove unused annotations

* add share gpx

* fix shared token

* refactoring share gpx

* add share api

* add index

* fix saveAccessedUser

* refactoring

* create table share_files

* refactoring

* change share api

* open share file for owner

* add share details

* add share flag for list-files

* add changeShareType

* add share error file is not available

* add nickname field to PremiumUser

* add private share type

* rename column id

* refactoring

* change UUID

* refactoring

* refactoring
  • Loading branch information
alisa911 authored Jan 4, 2025
1 parent 87e703a commit a0730eb
Show file tree
Hide file tree
Showing 11 changed files with 805 additions and 107 deletions.
32 changes: 31 additions & 1 deletion db/sql_changeset_schema
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ CREATE INDEX supporters_device_sub_orderid_idx on supporters_device_sub(orderid)
ALTER TABLE supporters_device_sub add primary key (sku, orderid);

------ PREMIUM accounts ----
CREATE TABLE user_accounts(id serial primary key, email text not null, tokendevice text, orderid text, token text, tokentime timestamp, regtime timestamp);
CREATE TABLE user_accounts(id serial primary key, email text not null, nickname text, tokendevice text, orderid text, token text, tokentime timestamp, regtime timestamp);
CREATE TABLE user_account_devices(id serial primary key, userid integer, deviceid text, accesstoken text, lang text, brand text, model text, udpatetime timestamp);
CREATE TABLE user_files(id bigserial primary key, type text, name text, userid integer, deviceid integer, updatetime timestamp, clienttime timestamp, filesize bigint, zipfilesize bigint, storage text, gendetails jsonb, data bytea);
CREATE TABLE promo_campaigns(name text, starttime timestamp, endtime timestamp, subactivemonths integer, numberlimit integer, used integer, lastusers text);
Expand All @@ -39,6 +39,36 @@ CREATE INDEX user_account_devices_userid_idx on user_account_devices(userid);
CREATE INDEX user_files_userid_idx on user_files(userid);
CREATE INDEX user_files_deviceid_idx on user_files(deviceid);

-- share file
CREATE TABLE user_share_files (
id SERIAL PRIMARY KEY,
ownerid INTEGER NOT NULL,
uuid UUID UNIQUE,
filepath TEXT NOT NULL,
name TEXT NOT NULL,
type TEXT NOT NULL,
public_access BOOLEAN NOT NULL
);

CREATE TABLE user_share_files_access (
id SERIAL UNIQUE,
user_id INTEGER NOT NULL,
access TEXT NOT NULL,
date TIMESTAMP NOT NULL,
file_id INTEGER NOT NULL,
CONSTRAINT user_share_files_access_pk PRIMARY KEY (file_id, user_id),
CONSTRAINT fk_user_share_files_access_user FOREIGN KEY (user_id) REFERENCES user_accounts(id) ON DELETE CASCADE,
CONSTRAINT fk_user_share_files_access_file FOREIGN KEY (file_id) REFERENCES user_share_files(id) ON DELETE CASCADE
);

CREATE INDEX user_share_files_ownerid_idx ON user_share_files(ownerid);
CREATE INDEX user_share_files_uuid_idx ON user_share_files(uuid);
CREATE INDEX user_share_files_filepath_idx ON user_share_files(filepath);
CREATE UNIQUE INDEX user_share_files_ownerid_filepath_idx ON user_share_files(ownerid, filepath);

CREATE INDEX user_share_files_access_userid_idx ON user_share_files_access(user_id);
CREATE INDEX user_share_files_access_file_id_idx ON user_share_files_access(file_id);

-- mail group
CREATE TABLE email_free_users(aid text, email text, os text, updatetime timestamp);
CREATE TABLE email_blocked(email text, reason text, timestamp timestamp);
Expand Down
2 changes: 2 additions & 0 deletions java-tools/OsmAndServer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ dependencies {
implementation 'org.apache.httpcomponents.client5:httpclient5:5.3.1'
implementation 'org.lz4:lz4-java:1.8.0'

compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.28'
annotationProcessor group: 'org.projectlombok', name: 'lombok', version: '1.18.28'
// implementation 'com.clickhouse:clickhouse-http-client:0.6.0'

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.osmand.server.api.repo;


import java.io.IOException;
import java.io.Serial;
import java.io.Serializable;
import java.util.Date;
Expand All @@ -15,6 +16,7 @@
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import com.google.gson.Gson;
import org.hibernate.annotations.Type;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -37,7 +39,7 @@ public interface PremiumUserFilesRepository extends JpaRepository<UserFile, Long
UserFile findTopByUseridAndNameAndTypeAndUpdatetimeGreaterThanOrderByUpdatetimeDesc(int userid, String name, String type, Date updatetime);

List<UserFile> findAllByUseridAndNameAndTypeOrderByUpdatetimeDesc(int userid, String name, String type);

Iterable<UserFile> findAllByUserid(int userid);

@Query("SELECT uf FROM UserFile uf "
Expand All @@ -54,6 +56,8 @@ public interface PremiumUserFilesRepository extends JpaRepository<UserFile, Long
@Entity(name = "UserFile")
@Table(name = "user_files")
class UserFile implements Serializable {
private static final Gson gson = new Gson();

@Serial
private static final long serialVersionUID = 1L;

Expand Down Expand Up @@ -93,14 +97,24 @@ class UserFile implements Serializable {
@Column(name = "gendetails", columnDefinition = "jsonb")
@Type(type = "net.osmand.server.assist.data.JsonbType")
public JsonObject details;

// @Fetch(FetchMode.JOIN)
@Column(name = "data", columnDefinition="bytea")
public byte[] data;

// @Lob
// public Blob data;

@Column(name = "data", columnDefinition = "bytea")
public byte[] data;

@Serial
private void writeObject(java.io.ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeObject(details != null ? gson.toJson(details) : null);
}

@Serial
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
String json = (String) in.readObject();
if (json != null) {
this.details = gson.fromJson(json, JsonObject.class);
}
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ class PremiumUser implements Serializable {

@Column(name = "email")
public String email;

@Column(name = "nickname")
public String nickname;

@Column(name = "token")
public String token;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package net.osmand.server.api.repo;

import lombok.Getter;
import lombok.Setter;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.data.jpa.repository.Query;

import javax.persistence.*;
import java.io.Serial;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Repository
public interface ShareFileRepository extends JpaRepository<ShareFileRepository.ShareFile, Long> {

ShareFile findByUuid(UUID uuid);

ShareFile findByOwneridAndFilepath(int ownerid, String filepath);

List<ShareFile> findByOwnerid(int ownerid);

@Query("SELECT a FROM ShareFilesAccess a WHERE a.id = :id")
ShareFilesAccess findShareFilesAccessById(@Param("id") long id);

<S extends ShareFilesAccess> S saveAndFlush(S entity);

@Setter
@Getter
@Entity(name = "ShareFile")
@Table(name = "user_share_files")
class ShareFile implements Serializable {

@Serial
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false)
public long id;

@Column(nullable = false)
public int ownerid;

@Column(unique = true)
private UUID uuid;

@Column(nullable = false)
public String filepath;

@Column(nullable = false)
public String name;

@Column(nullable = false)
public String type;

@Column(nullable = false)
public boolean publicAccess;

@OneToMany(mappedBy = "file", cascade = CascadeType.ALL, orphanRemoval = true)
private List<ShareFilesAccess> accessRecords;

public void addAccessRecord(ShareFilesAccess access) {
accessRecords.add(access);
access.setFile(this);
}
}

@Setter
@Getter
@Entity(name = "ShareFilesAccess")
@Table(name = "user_share_files_access")
class ShareFilesAccess implements Serializable {

@Serial
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
private PremiumUsersRepository.PremiumUser user;

@Column(nullable = false)
private String access;

@Column(name = "date")
@Temporal(TemporalType.TIMESTAMP)
public Date requestDate;

@ManyToOne
@JoinColumn(name = "file_id", nullable = false)
private ShareFile file;
}

@Getter
@Setter
public class ShareFileDTO {

private long id;
private int ownerid;
private String uuid;
private String filepath;
private String name;
private String type;
private boolean publicAccess;
private List<ShareFilesAccessDTO> accessRecords;

public ShareFileDTO(ShareFile shareFile, boolean includeAccessRecords) {
this.id = shareFile.getId();
this.ownerid = shareFile.getOwnerid();
this.uuid = shareFile.getUuid() != null ? shareFile.getUuid().toString() : null;
this.filepath = shareFile.getFilepath();
this.name = shareFile.getName();
this.type = shareFile.getType();
this.publicAccess = shareFile.isPublicAccess();
if (includeAccessRecords && shareFile.getAccessRecords() != null) {
this.accessRecords = shareFile.getAccessRecords().stream()
.map((ShareFilesAccess access) -> new ShareFilesAccessDTO(access, false))
.collect(Collectors.toList());
}
}
}

@Getter
@Setter
class ShareFilesAccessDTO {

private long id;
private String name;
private String access;
private Date requestDate;

public ShareFilesAccessDTO(ShareFilesAccess access, boolean includeFile) {
this.id = access.getId();
this.name = access.getUser().nickname;
this.access = access.getAccess();
this.requestDate = access.getRequestDate();
if (includeFile) {
this.id = access.getFile().getId();
}
}
}
}
Loading

0 comments on commit a0730eb

Please sign in to comment.