-
Notifications
You must be signed in to change notification settings - Fork 3
/
PhotoService.java
205 lines (167 loc) · 7.29 KB
/
PhotoService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package com.mgu.photoalbum.service;
import com.google.inject.Inject;
import com.mgu.photoalbum.adapter.fileio.InputStreamAdapter;
import com.mgu.photoalbum.adapter.fileio.PathAdapter;
import com.mgu.photoalbum.domain.Photo;
import com.mgu.photoalbum.identity.IdGenerator;
import com.mgu.photoalbum.service.scaler.ImageScaler;
import com.mgu.photoalbum.storage.PhotoRepository;
import org.apache.commons.lang3.StringUtils;
import org.ektorp.UpdateConflictException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;
public class PhotoService implements PhotoCommandService, PhotoQueryService {
private static final Logger LOGGER = LoggerFactory.getLogger(PhotoService.class);
private final PhotoRepository repository;
private final AlbumCommandService albumCommandService;
private final PathScheme pathScheme;
private final PathAdapter pathAdapter;
private final InputStreamAdapter inputStreamAdapter;
private final ImageScaler scaler;
private final Supplier<String> photoIdGenerator;
@Inject
public PhotoService(
final AlbumCommandService albumCommandService,
final PhotoRepository repository,
final PathScheme pathScheme,
final PathAdapter pathAdapter,
final InputStreamAdapter inputStreamAdapter,
final IdGenerator idGenerator,
final ImageScaler scaler) {
this.repository = repository;
this.albumCommandService = albumCommandService;
this.pathAdapter = pathAdapter;
this.inputStreamAdapter = inputStreamAdapter;
this.pathScheme = pathScheme;
this.scaler = scaler;
this.photoIdGenerator = () -> idGenerator.generateId(14);
}
@Override
public String uploadPhoto(
final String ownerId,
final String albumId,
final String originalFilename,
final InputStream fileInputStream) {
final String photoId = generateUniquePhotoId();
Path photoFolderPath = pathScheme.constructPathToPhotoFolder(ownerId, albumId, photoId);
Path originalPath = pathScheme.constructPathToOriginal(ownerId, albumId, photoId);
Path thumbnailPath = pathScheme.constructPathToThumbnail(ownerId, albumId, photoId);
pathAdapter.createDirectory(photoFolderPath);
pathAdapter.copy(fileInputStream, originalPath);
pathAdapter.copyImage(generateThumbnail(originalPath), thumbnailPath);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Generated thumbnail for photo with ID " + photoId + " at " + thumbnailPath.toString());
}
final Photo photo = Photo
.create()
.belongsTo(albumId)
.createdBy(ownerId)
.id(photoId)
.originalFilename(originalFilename)
.build();
repository.add(photo);
LOGGER.info("Successfully uploaded photo with ID " + photoId + " to repository.");
return photoId;
}
private String generateUniquePhotoId() {
String photoId = photoIdGenerator.get();
while (repository.contains(photoId)) {
photoId = photoIdGenerator.get();
}
return photoId;
}
private BufferedImage generateThumbnail(final Path pathToUnscaledImage) {
final byte[] unscaledImage = pathAdapter.readBytes(pathToUnscaledImage);
return scaler.scale(unscaledImage, new Dimension(550, 370));
}
@Override
public void deletePhoto(final String photoId) {
if (!repository.contains(photoId)) {
throw new PhotoDoesNotExistException(photoId);
}
final Photo photo = repository.get(photoId);
deletePhotoFiles(photo.getOwnerId(), photo.getAlbumId(), photoId);
repository.remove(photo);
LOGGER.info("Removed photo with ID " + photoId + " from photo album with ID " + photo.getAlbumId() + ".");
// TODO (mgu): Should not be necessary and we need to get rid of that dependency!
albumCommandService.removePhotoFromAlbum(photo.getAlbumId(), photoId);
}
private void deletePhotoFiles(final String ownerId, final String albumId, final String photoId) {
final File original = pathScheme.constructPathToOriginal(ownerId, albumId, photoId).toFile();
final File thumbnail = pathScheme.constructPathToThumbnail(ownerId, albumId, photoId).toFile();
if (pathAdapter.delete(original)) {
LOGGER.info("Deleted original photo at " + original.toString());
}
if (pathAdapter.delete(thumbnail)) {
LOGGER.info("Deleted thumbnail photo at " + thumbnail.toString());
}
}
@Override
public void updateMetadata(final String photoId, final String description, final List<String> tags) {
if (!repository.contains(photoId)) {
throw new PhotoDoesNotExistException(photoId);
}
final Photo photo = repository.get(photoId);
photo.describe(description);
photo.untag();
photo.tag(tags);
try {
repository.update(photo);
if (LOGGER.isInfoEnabled()) {
LOGGER.info("Updated metadata for photo with ID " + photoId + " to: description='" + description + "', " +
"tags='" + StringUtils.join(tags, ",") + "'.");
}
} catch (UpdateConflictException e) {
throw new UnableToUpdateMetadataException(photoId, description, tags);
}
}
@Override
public byte[] originalById(final String photoId) {
if (!repository.contains(photoId)) {
throw new PhotoDoesNotExistException(photoId);
}
final Photo photo = repository.get(photoId);
final Path pathToOriginal = pathScheme.constructPathToOriginal(photo.getOwnerId(), photo.getAlbumId(), photoId);
if (!pathAdapter.exists(pathToOriginal)) {
throw new ImageDoesNotExistException(photoId);
}
return pathAdapter.readBytes(pathToOriginal);
}
@Override
public byte[] thumbnailById(final String photoId) {
if (!repository.contains(photoId)) {
throw new PhotoDoesNotExistException(photoId);
}
final Photo photo = repository.get(photoId);
final Path pathToThumbnail = pathScheme.constructPathToThumbnail(photo.getOwnerId(), photo.getAlbumId(), photoId);
if (!pathAdapter.exists(pathToThumbnail)) {
throw new ImageDoesNotExistException(photoId);
}
return pathAdapter.readBytes(pathToThumbnail);
}
@Override
public Photo photoById(final String photoId) {
if (!repository.contains(photoId)) {
throw new PhotoDoesNotExistException(photoId);
}
return repository.get(photoId);
}
@Override
public List<Photo> search(final String albumId, final List<String> tags, final int offset, final int pageSize) {
return repository
.getAllByAlbum(albumId)
.stream()
.filter(photo -> photo.anyTagMatches(tags))
.skip(offset)
.limit(pageSize)
.collect(Collectors.toList());
}
}