Skip to content

Commit e161fed

Browse files
committed
fixing some NPE and other errors
1 parent 3360ed6 commit e161fed

File tree

5 files changed

+92
-80
lines changed

5 files changed

+92
-80
lines changed

app/controllers/admin/Export.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
@BasicAuth
99
public class Export extends Controller {
10-
private final static Integer BATCH_SIZE = 500;
10+
private final static Integer BATCH_SIZE = 2500;
1111

1212
public static Result start() {
1313
ExportJob ej = new ExportJob(BATCH_SIZE);

app/jobs/ExportJob.java

+17-11
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,23 @@ public void run() {
2929
int count = 1;
3030
for(MediaFile mediaFile : files) {
3131
final File oldPath = new File(STORAGE_FOLDER + File.separator + mediaFile.getLocation());
32-
final String newFolder = EXPORT_FOLDER + mediaFile.getFolderName();
33-
checkFolder(newFolder);
34-
final File newPath = new File(newFolder + mediaFile.filename);
35-
final boolean flag = SystemHelper.move(oldPath, newPath);
36-
if(flag) {
37-
mediaFile.filepath = "exported";
38-
mediaFile.update();
39-
Logger.info("{} {} was successfully exported to {}",
40-
count,
41-
mediaFile.checksum,
42-
newPath.getAbsolutePath());
32+
if(!oldPath.exists()) {
33+
mediaFile.markExported();
34+
Logger.info("File {} already gone, assume it was already exported!", oldPath.getAbsolutePath());
35+
} else {
36+
final String newFolder = EXPORT_FOLDER + File.separator + mediaFile.getFolderName();
37+
checkFolder(newFolder);
38+
final File newPath = new File(newFolder + mediaFile.filename);
39+
final boolean flag = SystemHelper.move(oldPath, newPath);
40+
if(flag) {
41+
mediaFile.markExported();
42+
Logger.info("{} {} was successfully exported to {}",
43+
count,
44+
mediaFile.checksum,
45+
newPath.getAbsolutePath());
46+
} else {
47+
Logger.warn("There was an error exporting file {} to {}", mediaFile.checksum, newPath.getAbsolutePath());
48+
}
4349
}
4450
count++;
4551
}

app/models/MediaFile.java

+29-24
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,18 @@ public class MediaFile extends Model {
4141
public String filepath;
4242
public String checksum;
4343
public String filename;
44-
44+
4545
public Integer views = 0;
46-
46+
4747
public Long filesize;
4848
public String mimeType;
49-
49+
5050
public Date lastCheck;
5151
public Date created;
52-
52+
5353
@OneToOne
5454
public Thumbnail cover;
55-
55+
5656
@ManyToOne
5757
public MediaFolder folder = null;
5858

@@ -66,10 +66,10 @@ public class MediaFile extends Model {
6666
@OneToMany(mappedBy="mediaFile")
6767
@OrderBy("filename DESC")
6868
private List<Thumbnail> thumbnails;
69-
69+
7070
@Transient
7171
private Map<String, String> props = null;
72-
72+
7373
public static Finder<Long, MediaFile> Finder = new Finder<Long, MediaFile>(Long.class, MediaFile.class);
7474

7575
public void setTags(Set<Tag> tags) {
@@ -84,10 +84,10 @@ public void setTags(Set<Tag> tags) {
8484
public List<Tag> getTags() {
8585
return Tag.Finder.where().eq("mediaFiles.id", this.id).orderBy("name ASC").findList();
8686
}
87-
87+
8888
public List<Property> getProperties() {
8989
return Property.Finder.where().eq("mediaFile.id", this.id).orderBy("k ASC").findList();
90-
}
90+
}
9191

9292
public List<Property> getProperties(String k) {
9393
return Property.Finder.where().eq("mediaFile.id", this.id).eq("k", k.trim()).findList();
@@ -153,22 +153,27 @@ public String getFolderName() {
153153

154154
public JsonNode getStreams() {
155155
Property prop = Property.Finder.where().eq("mediaFile", this).eq("k", "streams").findUnique();
156-
JsonNode arrNode = null;
157-
try {
158-
if(prop != null) {
159-
arrNode = new ObjectMapper().readTree(prop.v);
160-
if(arrNode.isArray())
161-
for (final JsonNode objNode : arrNode) {
162-
Logger.debug("arrNode: "+objNode);
163-
}
156+
if(prop != null && prop.v != null) {
157+
JsonNode arrNode = null;
158+
try {
159+
arrNode = new ObjectMapper().readTree(prop.v);
160+
if(arrNode.isArray())
161+
for (final JsonNode objNode : arrNode) {
162+
Logger.debug("arrNode: "+objNode);
163+
}
164+
return arrNode != null && arrNode.isArray() ? arrNode : new ObjectMapper().readTree("[]");
165+
} catch (IOException ex) {
166+
Logger.warn(ex.getLocalizedMessage(), ex);
164167
}
165-
return arrNode != null && arrNode.isArray() ? arrNode : new ObjectMapper().readTree("[]");
166-
} catch (IOException ex) {
167-
Logger.warn(ex.getLocalizedMessage(), ex);
168-
return null;
169-
}
168+
}
169+
return null;
170170
}
171-
171+
172+
public void markExported() {
173+
this.filepath = "exported";
174+
update();
175+
}
176+
172177
public static MediaFile findUnique(String checksum) {
173178
List<MediaFile> mfs = MediaFile.Finder.where().eq("checksum", checksum).findList();
174179
if(mfs.size() > 1) {
@@ -220,7 +225,7 @@ public static Map<String, Long> getFileSizes() {
220225
List<SqlRow> rows = query.findList();
221226
for (SqlRow sqlRow : rows) {
222227
if(sqlRow.getString("mime_type") != null && sqlRow.getLong("size") != null) {
223-
sizes.put(sqlRow.getString("mime_type"), sqlRow.getLong("size"));
228+
sizes.put(sqlRow.getString("mime_type"), sqlRow.getLong("size"));
224229
}
225230
}
226231
return sizes;

app/models/MediaFolder.java

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ private final static String getFolder(String path) {
7777
}
7878

7979
public String toString() {
80+
if(path == null) return null;
8081
return path.replace(UPLOAD_DIR_HANDLE.getAbsolutePath(), "").trim();
8182
}
8283
}

app/models/Thumbnail.java

+44-44
Original file line numberDiff line numberDiff line change
@@ -15,48 +15,48 @@
1515

1616
@Entity
1717
public class Thumbnail extends Model {
18-
19-
@Id
20-
@GeneratedValue(strategy = GenerationType.IDENTITY)
21-
public Long id;
22-
23-
public String filepath;
24-
public String checksum;
25-
26-
@ManyToOne
27-
public MediaFile mediaFile;
28-
29-
public Thumbnail(String path) {
30-
this.filepath = path.trim();
31-
}
32-
33-
public static Finder<Long, Thumbnail> Finder = new Finder<Long, Thumbnail>(Long.class, Thumbnail.class);
34-
35-
public String getChecksum(File file) {
36-
if(checksum == null) {
37-
checksum = ThumbnailsHelper.getETag(file);
38-
update();
39-
}
40-
return checksum;
41-
}
42-
43-
public static Thumbnail getOrCreate(MediaFile mediaFile, String filepath, String checksum) {
44-
List<Thumbnail> thumbnails = Thumbnail.Finder.where().eq("mediaFile", mediaFile).eq("filepath", filepath.trim()).findList();
45-
Thumbnail t = thumbnails.size() > 1 ? thumbnails.get(0) : null;
46-
if(t == null) {
47-
t = new Thumbnail(filepath);
48-
t.mediaFile = mediaFile;
49-
t.checksum = checksum;
50-
t.save();
51-
}
52-
return t;
53-
}
54-
55-
public static Integer getSize() {
56-
return Finder.findRowCount();
57-
}
58-
59-
public static List<Thumbnail> getLast(int number) {
60-
return Finder.setMaxRows(number).orderBy("id DESC").findList();
61-
}
18+
19+
@Id
20+
@GeneratedValue(strategy = GenerationType.IDENTITY)
21+
public Long id;
22+
23+
public String filepath;
24+
public String checksum;
25+
26+
@ManyToOne
27+
public MediaFile mediaFile;
28+
29+
public Thumbnail(String path) {
30+
this.filepath = path.trim();
31+
}
32+
33+
public static Finder<Long, Thumbnail> Finder = new Finder<Long, Thumbnail>(Long.class, Thumbnail.class);
34+
35+
public String getChecksum(File file) {
36+
if(checksum == null) {
37+
checksum = ThumbnailsHelper.getETag(file);
38+
update();
39+
}
40+
return checksum;
41+
}
42+
43+
public static Thumbnail getOrCreate(MediaFile mediaFile, String filepath, String checksum) {
44+
List<Thumbnail> thumbnails = Thumbnail.Finder.where().eq("mediaFile", mediaFile).eq("filepath", filepath.trim()).findList();
45+
Thumbnail t = thumbnails.size() > 1 ? thumbnails.get(0) : null;
46+
if(t == null) {
47+
t = new Thumbnail(filepath);
48+
t.mediaFile = mediaFile;
49+
t.checksum = checksum;
50+
t.save();
51+
}
52+
return t;
53+
}
54+
55+
public static Integer getSize() {
56+
return Finder.findRowCount();
57+
}
58+
59+
public static List<Thumbnail> getLast(int number) {
60+
return Finder.setMaxRows(number).orderBy("id DESC").findList();
61+
}
6262
}

0 commit comments

Comments
 (0)