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

Share GPX #978

Merged
merged 31 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0b9d50c
remove unused annotations
alisa911 Nov 20, 2024
e017c8f
add share gpx
alisa911 Nov 20, 2024
96fedd7
fix shared token
alisa911 Nov 20, 2024
14d564e
refactoring share gpx
alisa911 Nov 21, 2024
033af30
merge master
alisa911 Nov 28, 2024
e493f00
add share api
alisa911 Nov 28, 2024
d91e40d
add index
alisa911 Nov 28, 2024
b2b40b0
fix saveAccessedUser
alisa911 Nov 28, 2024
138137f
refactoring
alisa911 Nov 29, 2024
c635cbd
create table share_files
alisa911 Dec 2, 2024
bcf7abe
refactoring
alisa911 Dec 2, 2024
267b58e
merge master
alisa911 Dec 6, 2024
20d7d02
change share api
alisa911 Dec 11, 2024
a14d5da
open share file for owner
alisa911 Dec 11, 2024
9989a01
Merge branch 'master' into T2527
alisa911 Dec 13, 2024
593764b
add share details
alisa911 Dec 13, 2024
2878b47
add share flag for list-files
alisa911 Dec 13, 2024
de77bde
add changeShareType
alisa911 Dec 13, 2024
88ff43f
add share error file is not available
alisa911 Dec 16, 2024
579e4eb
add nickname field to PremiumUser
alisa911 Dec 17, 2024
b128058
add private share type
alisa911 Dec 18, 2024
161a9e0
Merge branch 'master' into T2527
alisa911 Dec 18, 2024
41e92f4
rename column id
alisa911 Dec 18, 2024
e71e43c
refactoring
alisa911 Dec 18, 2024
4450b40
change UUID
alisa911 Dec 18, 2024
a313d4d
refactoring
alisa911 Dec 18, 2024
36d2757
refactoring
alisa911 Dec 18, 2024
5a54608
Merge branch 'master' into T2527
alisa911 Dec 20, 2024
965f2f5
Merge branch 'master' into T2527
alisa911 Dec 26, 2024
36e6920
Merge branch 'master' into T2527
alisa911 Dec 27, 2024
4c76626
Merge branch 'master' into T2527
alisa911 Dec 31, 2024
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
6 changes: 4 additions & 2 deletions db/sql_changeset_schema
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,28 @@ CREATE INDEX user_files_deviceid_idx on user_files(deviceid);
CREATE TABLE user_share_files (
id SERIAL PRIMARY KEY,
alisa911 marked this conversation as resolved.
Show resolved Hide resolved
ownerid INTEGER NOT NULL,
uuid TEXT UNIQUE,
uuid UUID UNIQUE,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to use VARCHAR not UUID. Some day we might decide to use some shorter code such as set of [a-zA-Z0-9]. VARCHAR would be OK for both UUID and String.

filepath TEXT NOT NULL,
name TEXT NOT NULL,
alisa911 marked this conversation as resolved.
Show resolved Hide resolved
type TEXT NOT NULL,
public_access BOOLEAN NOT NULL
);

CREATE TABLE user_share_files_access (
id SERIAL PRIMARY KEY,
id SERIAL UNIQUE,
user_id INTEGER NOT NULL,
access TEXT NOT NULL,
date TIMESTAMP NOT NULL,
file_id INTEGER NOT NULL,
alisa911 marked this conversation as resolved.
Show resolved Hide resolved
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 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);
alisa911 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
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(String uuid);
ShareFile findByUuid(UUID uuid);

ShareFile findByOwneridAndFilepath(int ownerid, String filepath);
alisa911 marked this conversation as resolved.
Show resolved Hide resolved

Expand Down Expand Up @@ -46,7 +47,7 @@ class ShareFile implements Serializable {
public int ownerid;

@Column(unique = true)
private String uuid;
private UUID uuid;

@Column(nullable = false)
public String filepath;
Expand Down Expand Up @@ -114,7 +115,7 @@ public class ShareFileDTO {
public ShareFileDTO(ShareFile shareFile, boolean includeAccessRecords) {
this.id = shareFile.getId();
this.ownerid = shareFile.getOwnerid();
this.uuid = shareFile.getUuid();
this.uuid = shareFile.getUuid() != null ? shareFile.getUuid().toString() : null;
this.filepath = shareFile.getFilepath();
this.name = shareFile.getName();
this.type = shareFile.getType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public enum PermissionType {
public static final String PUBLIC_SHARE_TYPE = "public";

@Transactional
public String generateSharedCode(PremiumUserFilesRepository.UserFile userFile, boolean publicAccess) {
String uniqueCode = generateUniqueCode();
public UUID generateSharedCode(PremiumUserFilesRepository.UserFile userFile, boolean publicAccess) {
UUID uniqueCode = generateUniqueCode();
// Update existing file with new code
ShareFileRepository.ShareFile existingFile = getFileByOwnerAndFilepath(userFile.userid, userFile.name);
if (existingFile != null) {
Expand All @@ -65,16 +65,20 @@ public String generateSharedCode(PremiumUserFilesRepository.UserFile userFile, b
return uniqueCode;
}

private String generateUniqueCode() {
String uniqueCode;
private UUID generateUniqueCode() {
UUID uniqueCode;
do {
uniqueCode = UUID.randomUUID().toString();
uniqueCode = UUID.randomUUID();
} while (shareFileRepository.findByUuid(uniqueCode) != null);
return uniqueCode;
}

@Transactional
public ShareFileRepository.ShareFile createShareFile(PremiumUserFilesRepository.UserFile userFile, boolean publicAccess, String uniqueCode) {
public ShareFileRepository.ShareFile createShareFile(PremiumUserFilesRepository.UserFile userFile, boolean publicAccess, UUID uniqueCode) {
ShareFileRepository.ShareFile existingFile = getFileByOwnerAndFilepath(userFile.userid, userFile.name);
if (existingFile != null) {
return existingFile;
}
alisa911 marked this conversation as resolved.
Show resolved Hide resolved
ShareFileRepository.ShareFile shareFile = new ShareFileRepository.ShareFile();

String name = userFile.name.substring(userFile.name.lastIndexOf("/") + 1);
Expand Down Expand Up @@ -126,7 +130,7 @@ public boolean changeFileShareType(ShareFileRepository.ShareFile shareFile, Stri
} else {
shareFile.setPublicAccess(shareType.equals(PUBLIC_SHARE_TYPE));
if (shareFile.isPublicAccess() && shareFile.getUuid() == null) {
String uuid = generateUniqueCode();
UUID uuid = generateUniqueCode();
shareFile.setUuid(uuid);
}
shareFileRepository.saveAndFlush(shareFile);
Expand Down Expand Up @@ -178,7 +182,7 @@ public ShareFileRepository.ShareFile getShareFileByUuid(String uuid) {
if (uuid == null) {
return null;
}
return shareFileRepository.findByUuid(uuid);
return shareFileRepository.findByUuid(UUID.fromString(uuid));
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.UUID;

import static net.osmand.server.api.services.ShareFileService.PRIVATE_SHARE_TYPE;
import static net.osmand.server.api.services.UserdataService.FILE_NOT_FOUND;
Expand Down Expand Up @@ -63,11 +64,11 @@ public ResponseEntity<String> generateLink(@RequestParam String fileName,
if (userFile == null) {
return ResponseEntity.badRequest().body(FILE_NOT_FOUND);
}
String uuid = shareFileService.generateSharedCode(userFile, publicAccess);
UUID uuid = shareFileService.generateSharedCode(userFile, publicAccess);
if (uuid == null) {
return ResponseEntity.badRequest().body("Error generating link");
}
return ResponseEntity.ok(gson.toJson(Map.of("uuid", uuid)));
return ResponseEntity.ok(gson.toJson(Map.of("uuid", uuid.toString())));
}

@GetMapping(path = {"/get/{uuid}"}, produces = "application/json")
Expand Down Expand Up @@ -167,7 +168,7 @@ public ResponseEntity<String> getFileInfo(@RequestParam String fileName,
if (user == null) {
return ResponseEntity.badRequest().body("Error getting user info");
}
String ownerName = user.nickname;
String ownerName = user.nickname != null ? user.nickname : user.email;
ShareFileRepository.ShareFileDTO shareFileDto = new ShareFileRepository.ShareFileDTO(shareFile, true);
return ResponseEntity.ok(gson.toJson(Map.of("owner", ownerName, "file", shareFileDto)));
}
Expand Down