Skip to content

Commit 3e482e9

Browse files
committed
Update HomeForm so now it shows movie images;
Implement image retrieval method
1 parent 80b16f0 commit 3e482e9

File tree

8 files changed

+229
-14
lines changed

8 files changed

+229
-14
lines changed

data/images/README.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Temporal data, such as images, will be stored here.

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@
126126
<artifactId>neo4j-java-driver</artifactId>
127127
<version>1.1.2</version>
128128
</dependency>
129+
<!-- https://mvnrepository.com/artifact/org.imgscalr/imgscalr-lib -->
130+
<dependency>
131+
<groupId>org.imgscalr</groupId>
132+
<artifactId>imgscalr-lib</artifactId>
133+
<version>4.2</version>
134+
</dependency>
129135
</dependencies>
130136

131137
</project>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package ud.binmonkey.prog3_proyecto_client.common;
2+
3+
public class MovieName {
4+
5+
/**
6+
* Returns a string with $name($year) format
7+
* @param name name of movie
8+
* @param year year of release
9+
* @return String with $name($year) format
10+
*/
11+
public static String formatMovie(String name, String year) {
12+
return name + "(" + year + ")";
13+
}
14+
15+
/**
16+
* Returns a string with $name($year) format
17+
* @param name name of movie
18+
* @param year year of release
19+
* @return String with $name($year) format
20+
*/
21+
public static String formatMovie(String name, int year) {
22+
return formatMovie(name, new Integer(year).toString());
23+
}
24+
25+
/**
26+
* Returns only the name given a $name($year) formatted string
27+
* @param movieFileName $name($year) formatted string
28+
* @return name of movie
29+
*/
30+
public static String getName(String movieFileName) {
31+
String fileName = "";
32+
String[] components = movieFileName.split("\\(");
33+
34+
for (int i = 0; i < components.length - 1; i++) {
35+
fileName += components[i];
36+
if (i != components.length - 2) {
37+
fileName += ")";
38+
}
39+
}
40+
41+
return fileName;
42+
}
43+
44+
/**
45+
* Returns only the year given a $name($year) formatted string
46+
* @param movieFileName $name($year) formatted string
47+
* @return year of release
48+
*/
49+
public static String getYear(String movieFileName) {
50+
String[] components = movieFileName.split("\\(");
51+
try {
52+
return components[components.length - 1].split("\\)")[0];
53+
} catch (IndexOutOfBoundsException e) {
54+
return null;
55+
}
56+
}
57+
58+
/**
59+
* Removes extension from filename
60+
* @param movieFileName name of file
61+
* @return name without extension
62+
*/
63+
public static String removeExtension(String movieFileName) {
64+
String fileName = "";
65+
String[] components = movieFileName.split("\\.");
66+
67+
for (int i = 0; i < components.length - 1; i++) {
68+
fileName += components[i];
69+
if (i != components.length - 2) {
70+
fileName += ".";
71+
}
72+
}
73+
74+
return fileName;
75+
}
76+
77+
/**
78+
* Checks if filename matches $name($year).$extension format
79+
* @param fileName filename to check
80+
* @return true if match, false if no match
81+
*/
82+
public static boolean matchesMovie(String fileName) {
83+
return fileName.matches("[A-Za-z0-9]+\\(\\d{4}\\)\\.[A-Za-z0-9]+");
84+
}
85+
86+
public static void main(String[] args) {
87+
String movie = "Victoria(2015).mp4";
88+
System.out.println(getName(movie));
89+
System.out.println(getYear(movie));
90+
}
91+
}

src/main/java/ud/binmonkey/prog3_proyecto_client/ftp/FTPlib.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
import org.apache.commons.net.ftp.FTP;
44
import org.apache.commons.net.ftp.FTPClient;
5+
import ud.binmonkey.prog3_proyecto_client.common.MovieName;
56
import ud.binmonkey.prog3_proyecto_client.common.network.URI;
67

7-
import java.io.File;
8-
import java.io.FileInputStream;
9-
import java.io.IOException;
10-
import java.io.InputStream;
8+
import java.io.*;
119

1210
public class FTPlib {
1311

@@ -120,9 +118,30 @@ public static void mkdir(String username, String password, String dirname) throw
120118
client.makeDirectory(dirname);
121119
}
122120

121+
public static void downloadFilmImage(String name, int year) throws IOException {
122+
downloadFilmImage(name, new Integer(year).toString());
123+
}
124+
125+
public static void downloadFilmImage(String name, String year) throws IOException {
126+
FTPClient client = logIn("common", "common");
127+
client.enterLocalPassiveMode();
128+
client.setFileType(FTP.BINARY_FILE_TYPE);
129+
130+
String imagePath = "data/images/" + MovieName.formatMovie(name, year) + ".jpg";
131+
132+
File file = new File(imagePath);
133+
file.delete();
134+
135+
OutputStream os = new BufferedOutputStream(new FileOutputStream(imagePath));
136+
137+
client.retrieveFile(imagePath, os);
138+
os.close();
139+
}
140+
123141
public static void main(String[] args) {
124142
try {
125-
uploadFile("test", "test", "src/test/resources/ftpd/.ignore", "hi/there");
143+
// uploadFile("test", "test", "src/test/resources/ftpd/.ignore", "hi/there");
144+
downloadFilmImage("Victoria", 2015);
126145
} catch (IOException e) {
127146
e.printStackTrace();
128147
}

src/main/java/ud/binmonkey/prog3_proyecto_client/gui/HomeForm.form

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,20 @@
22
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="ud.binmonkey.prog3_proyecto_client.gui.HomeForm">
33
<grid id="27dc6" binding="mainHomePanel" layout-manager="BorderLayout" hgap="0" vgap="0">
44
<constraints>
5-
<xy x="20" y="20" width="500" height="400"/>
5+
<xy x="20" y="20" width="519" height="400"/>
66
</constraints>
77
<properties/>
88
<border type="none"/>
99
<children>
10-
<grid id="1965e" binding="activitiesPanel" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
10+
<grid id="1965e" binding="activitiesPanel" layout-manager="GridLayoutManager" row-count="4" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
1111
<margin top="0" left="0" bottom="0" right="0"/>
1212
<constraints border-constraint="Center"/>
1313
<properties/>
1414
<border type="none"/>
1515
<children>
16-
<hspacer id="c1965">
17-
<constraints>
18-
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
19-
</constraints>
20-
</hspacer>
2116
<scrollpane id="6cc6d" binding="userFileSysScrollPane">
2217
<constraints>
23-
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
18+
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
2419
</constraints>
2520
<properties/>
2621
<border type="none"/>
@@ -79,7 +74,7 @@
7974
</grid>
8075
<grid id="daf97" binding="uploadProgressPanel" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1">
8176
<constraints>
82-
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
77+
<grid row="3" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
8378
</constraints>
8479
<properties/>
8580
<border type="none"/>
@@ -100,6 +95,53 @@
10095
</component>
10196
</children>
10297
</grid>
98+
<component id="4763f" class="javax.swing.JLabel" binding="whoseFilesLabel">
99+
<constraints>
100+
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="0" indent="0" use-parent-layout="false"/>
101+
</constraints>
102+
<properties>
103+
<text value="My Files"/>
104+
</properties>
105+
</component>
106+
<grid id="ac9f0" binding="moviePanel" layout-manager="BorderLayout" hgap="0" vgap="0">
107+
<constraints>
108+
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
109+
</constraints>
110+
<properties/>
111+
<border type="none"/>
112+
<children>
113+
<component id="4995" class="javax.swing.JLabel" binding="movieImageLabel">
114+
<constraints border-constraint="Center"/>
115+
<properties>
116+
<horizontalAlignment value="0"/>
117+
<text value=""/>
118+
</properties>
119+
</component>
120+
<grid id="a5972" binding="movieInfoLayout" layout-manager="FlowLayout" hgap="5" vgap="5" flow-align="1">
121+
<constraints border-constraint="South"/>
122+
<properties/>
123+
<border type="none"/>
124+
<children>
125+
<component id="1f459" class="javax.swing.JLabel" binding="movieNameLabel">
126+
<constraints/>
127+
<properties>
128+
<text value=""/>
129+
</properties>
130+
</component>
131+
<hspacer id="ce904">
132+
<constraints/>
133+
</hspacer>
134+
<component id="55af4" class="javax.swing.JLabel" binding="movieYearLabel">
135+
<constraints/>
136+
<properties>
137+
<foreground color="-12434878"/>
138+
<text value=""/>
139+
</properties>
140+
</component>
141+
</children>
142+
</grid>
143+
</children>
144+
</grid>
103145
</children>
104146
</grid>
105147
</children>

src/main/java/ud/binmonkey/prog3_proyecto_client/gui/HomeForm.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@
33
import com.intellij.uiDesigner.core.GridConstraints;
44
import com.intellij.uiDesigner.core.GridLayoutManager;
55
import com.intellij.uiDesigner.core.Spacer;
6+
import org.imgscalr.Scalr;
67
import org.json.JSONArray;
78
import org.json.JSONObject;
9+
import ud.binmonkey.prog3_proyecto_client.common.MovieName;
10+
import ud.binmonkey.prog3_proyecto_client.ftp.FTPlib;
811
import ud.binmonkey.prog3_proyecto_client.gui.listeners.homeForm.*;
912
import ud.binmonkey.prog3_proyecto_client.https.HTTPSClient;
1013

14+
import javax.imageio.ImageIO;
1115
import javax.swing.*;
1216
import javax.swing.tree.DefaultMutableTreeNode;
1317
import javax.swing.tree.TreePath;
1418
import javax.swing.tree.TreeSelectionModel;
1519
import java.awt.*;
20+
import java.awt.image.BufferedImage;
21+
import java.io.File;
22+
import java.io.IOException;
1623
import java.util.Enumeration;
1724
import java.util.HashMap;
1825

@@ -31,6 +38,12 @@ public class HomeForm {
3138
public JProgressBar uploadProgressBar;
3239
public JLabel uploadProgressLabel;
3340
public JButton reloadButton;
41+
private JLabel whoseFilesLabel;
42+
private JPanel moviePanel;
43+
private JLabel movieImageLabel;
44+
private JPanel movieInfoLayout;
45+
private JLabel movieNameLabel;
46+
private JLabel movieYearLabel;
3447

3548
/**
3649
* Default form shown when user logs in
@@ -183,8 +196,50 @@ public void loadFileSysTree() {
183196
userFileSysTree.setEditable(false);
184197
userFileSysTree.setToolTipText("File system of " + MainWindow.INSTANCE.getFrame().getUser());
185198
userFileSysTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
199+
200+
/*
201+
* TREE LISTENER
202+
*/
186203
userFileSysTree.addTreeSelectionListener(treeSelectionEvent -> {
204+
187205
userFileSysTree.setSelectionPath(treeSelectionEvent.getNewLeadSelectionPath());
206+
207+
/*TODO: check if file is movie */
208+
String fileName = userFileSysTree.getSelectionPath().getLastPathComponent().toString();
209+
System.out.println(MovieName.matchesMovie(fileName));
210+
if (MovieName.matchesMovie(fileName)) {
211+
212+
String name = MovieName.getName(fileName);
213+
String year = MovieName.getYear(fileName);
214+
String fullName = MovieName.removeExtension(fileName);
215+
216+
movieNameLabel.setText("Name: " + name);
217+
movieYearLabel.setText("Year: " + year);
218+
219+
File imageFile = new File("data/images/" + fullName + ".jpg");
220+
if (!imageFile.exists()) {
221+
try {
222+
FTPlib.downloadFilmImage(name, year);
223+
224+
} catch (IOException e) {
225+
movieImageLabel.setText("404: No image found.");
226+
return;
227+
}
228+
}
229+
try {
230+
BufferedImage image = ImageIO.read(imageFile);
231+
image = Scalr.resize(image, 300, 425);
232+
ImageIcon pic = new ImageIcon(image);
233+
movieImageLabel.setIcon(pic);
234+
} catch (IOException e) {
235+
e.printStackTrace();
236+
}
237+
} else {
238+
movieNameLabel.setText("");
239+
movieYearLabel.setText("");
240+
movieImageLabel.setIcon(null);
241+
}
242+
188243
});
189244
if (userFileSysScrollPane != null) {
190245
userFileSysScrollPane.setViewportView(userFileSysTree);

src/test/resources/movies/README.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
All files with names of copyrighted films are actually Popeye For President with another name.
25.7 MB
Binary file not shown.

0 commit comments

Comments
 (0)