Skip to content

Commit

Permalink
added demo code
Browse files Browse the repository at this point in the history
  • Loading branch information
danialfarid committed Aug 13, 2013
1 parent 5168ebb commit c215133
Show file tree
Hide file tree
Showing 29 changed files with 695 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.settings
.metadata
.backup
*.war
.DS_Store
.DS_Store?
ehthumbs.db
Thumbs.db
.Spotlight-V100
.Trashes
/target/*
classes/
._*
Servers
.idea
.project
.classpath
16 changes: 16 additions & 0 deletions src/META-INF/jdoconfig.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<jdoconfig xmlns="http://java.sun.com/xml/ns/jdo/jdoconfig"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://java.sun.com/xml/ns/jdo/jdoconfig">

<persistence-manager-factory name="transactions-optional">
<property name="javax.jdo.PersistenceManagerFactoryClass"
value="org.datanucleus.api.jdo.JDOPersistenceManagerFactory"/>
<property name="javax.jdo.option.ConnectionURL" value="appengine"/>
<property name="javax.jdo.option.NontransactionalRead" value="true"/>
<property name="javax.jdo.option.NontransactionalWrite" value="true"/>
<property name="javax.jdo.option.RetainValues" value="true"/>
<property name="datanucleus.appengine.autoCreateDatastoreTxns" value="true"/>
<property name="datanucleus.appengine.singletonPMFForName" value="true"/>
</persistence-manager-factory>
</jdoconfig>
15 changes: 15 additions & 0 deletions src/META-INF/persistence.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">

<persistence-unit name="transactions-optional">
<provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
<properties>
<property name="datanucleus.NontransactionalRead" value="true"/>
<property name="datanucleus.NontransactionalWrite" value="true"/>
<property name="datanucleus.ConnectionURL" value="appengine"/>
</properties>
</persistence-unit>
</persistence>
95 changes: 95 additions & 0 deletions src/com/df/angularfileupload/FileUpload.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.df.angularfileupload;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class FileUpload extends HttpServlet {
private static final long serialVersionUID = -8244073279641189889L;

@Override
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
try {
ServletFileUpload upload = new ServletFileUpload();

FileItemIterator iterator = upload.getItemIterator(req);

StringBuilder sb = new StringBuilder();

while (iterator.hasNext()) {
FileItemStream item = iterator.next();
sb.append(item.getFieldName());
if (item.getName() != null) {
sb.append(" - ").append(item.getName());

}
sb.append(" : ");
if (item.getName() != null) {
sb.append("file size on the server: ").append(
fileSize(item.openStream()));
} else {
sb.append(read(item.openStream()));
}
sb.append("<br/>");
}
res.setContentType("text/plain");
PrintWriter printWriter = new PrintWriter(res.getOutputStream());
try {
printWriter.print(sb.toString());
printWriter.flush();
} finally {
printWriter.close();
}
} catch (Exception ex) {
throw new ServletException(ex);
}
}

protected int fileSize(InputStream stream) {
int length = 0;
try {
byte[] buffer = new byte[2048];
int size;
while ((size = stream.read(buffer)) != -1) {
length += size;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return length;

}

protected String read(InputStream stream) {
StringBuilder sb = new StringBuilder();
BufferedReader reader = new BufferedReader(
new InputStreamReader(stream));
try {
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
reader.close();
} catch (IOException e) {
}
}
return sb.toString();

}
}
24 changes: 24 additions & 0 deletions src/log4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# A default log4j configuration for log4j users.
#
# To use this configuration, deploy it into your application's WEB-INF/classes
# directory. You are also encouraged to edit it as you like.

# Configure the console as our one appender
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{HH:mm:ss,SSS} %-5p [%c] - %m%n

# tighten logging on the DataNucleus Categories
log4j.category.DataNucleus.JDO=WARN, A1
log4j.category.DataNucleus.Persistence=WARN, A1
log4j.category.DataNucleus.Cache=WARN, A1
log4j.category.DataNucleus.MetaData=WARN, A1
log4j.category.DataNucleus.General=WARN, A1
log4j.category.DataNucleus.Utility=WARN, A1
log4j.category.DataNucleus.Transaction=WARN, A1
log4j.category.DataNucleus.Datastore=WARN, A1
log4j.category.DataNucleus.ClassLoading=WARN, A1
log4j.category.DataNucleus.Plugin=WARN, A1
log4j.category.DataNucleus.ValueGeneration=WARN, A1
log4j.category.DataNucleus.Enhancer=WARN, A1
log4j.category.DataNucleus.SchemaTool=WARN, A1
31 changes: 31 additions & 0 deletions war/WEB-INF/appengine-web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>angular-file-upload</application>
<version>1</version>

<!--
Allows App Engine to send multiple requests to one instance in parallel:
-->
<threadsafe>true</threadsafe>

<!-- Configure java.util.logging -->
<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
</system-properties>

<!--
HTTP Sessions are disabled by default. To enable HTTP sessions specify:
<sessions-enabled>true</sessions-enabled>
It's possible to reduce request latency by configuring your application to
asynchronously write HTTP session data to the datastore:
<async-session-persistence enabled="true" />
With this feature enabled, there is a very small chance your app will see
stale session data. For details, see
http://code.google.com/appengine/docs/java/config/appconfig.html#Enabling_Sessions
-->

</appengine-web-app>
Binary file added war/WEB-INF/lib/appengine-api-1.0-sdk-1.7.5.jar
Binary file not shown.
Binary file added war/WEB-INF/lib/appengine-api-labs.jar
Binary file not shown.
Binary file added war/WEB-INF/lib/appengine-endpoints.jar
Binary file not shown.
Binary file added war/WEB-INF/lib/appengine-jsr107cache-1.7.5.jar
Binary file not shown.
Binary file added war/WEB-INF/lib/asm-4.0.jar
Binary file not shown.
Binary file added war/WEB-INF/lib/commons-fileupload-1.3.jar
Binary file not shown.
Binary file added war/WEB-INF/lib/datanucleus-api-jdo-3.1.3.jar
Binary file not shown.
Binary file added war/WEB-INF/lib/datanucleus-api-jpa-3.1.3.jar
Binary file not shown.
Binary file added war/WEB-INF/lib/datanucleus-appengine-2.1.2.jar
Binary file not shown.
Binary file added war/WEB-INF/lib/datanucleus-core-3.1.3.jar
Binary file not shown.
Binary file added war/WEB-INF/lib/geronimo-jpa_2.0_spec-1.0.jar
Binary file not shown.
Binary file added war/WEB-INF/lib/jdo-api-3.0.1.jar
Binary file not shown.
Binary file added war/WEB-INF/lib/jsr107cache-1.1.jar
Binary file not shown.
Binary file added war/WEB-INF/lib/jta-1.1.jar
Binary file not shown.
13 changes: 13 additions & 0 deletions war/WEB-INF/logging.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# A default java.util.logging configuration.
# (All App Engine logging is through java.util.logging by default).
#
# To use this configuration, copy it into your application's WEB-INF
# folder and add the following to your appengine-web.xml:
#
# <system-properties>
# <property name="java.util.logging.config.file" value="WEB-INF/logging.properties"/>
# </system-properties>
#

# Set the default logging level for all loggers to WARNING
.level = WARNING
25 changes: 25 additions & 0 deletions war/WEB-INF/web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>file_upload</servlet-name>
<servlet-class>com.df.angularfileupload.FileUpload</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>file_upload</servlet-name>
<url-pattern>/upload</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>SystemServiceServlet</servlet-name>
<servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
<init-param>
<param-name>services</param-name>
<param-value/>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SystemServiceServlet</servlet-name>
<url-pattern>/_ah/spi/*</url-pattern>
</servlet-mapping>
</web-app>
34 changes: 34 additions & 0 deletions war/common.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/** Add css rules here for your application. */


/** Example rules used by the template application (remove for your app) */
h1 {
font-size: 2em;
font-weight: bold;
color: #777777;
margin: 40px 0px 70px;
text-align: center;
}

.sendButton {
display: block;
font-size: 16pt;
}

/** Most GWT widgets already have a style name defined */
.gwt-DialogBox {
width: 400px;
}

.dialogVPanel {
margin: 5px;
}

.serverResponseLabelError {
color: red;
}

/** Set ids using widget.getElement().setId("idOfElement") */
#closeButton {
margin: 15px 6px 6px;
}
Binary file added war/favicon.ico
Binary file not shown.
69 changes: 69 additions & 0 deletions war/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet" href="common.css">
<title>Angular file upload sample</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js"></script>
</head>

<body>
<script src="js/angular-file-upload.js"></script>

<h1>Angular file upload sample</h1>
<h3>Visit <a href="https://github.com/danialfarid/angular-file-upload">angular-file-upload</a> on github</h3>

<div ng-app="fileUpload" ng-controller="MyCtrl">
Input to be sent along with the file: <input type="text" ng-model="input">
<br/>
<input type="file" ng-file-select="onFileSelect($files)" >
<input type="file" ng-file-select="onFileSelect($files)" multiple>
<br/>
Selected Files:
<ol>
<li ng-repeat="f in selectedFiles">
{{f.name}} - size: {{f.size}}B - type: {{f.type}}
</li>
</ol>
<br/>
Server Response:
<ul>
<li style="font-size:smaller;color:grey" ng-repeat="result in uploadResult" ng-bind-html-unsafe="result"></li>
</ul>
</div>

<script type="text/javascript">
//if FileAPI.min.js is located somewhere different than angular-file-upload.js file add the following
/*
FileAPI = {
staticPath: 'path/to/FileAPI.min.js/directory/'
}
*/
angular.module('fileUpload', ['angularFileUpload']);

var MyCtrl = ['$scope', '$http', function ($scope, $http) {

$scope.selectedFiles = [];

$scope.onFileSelect = function ($files) {
$scope.uploadResult = [];
$scope.selectedFiles = $files;
for (var i = 0; i < $files.length; i++) {
var $file =$files[i];
$http.uploadFile({
url: 'upload',
data: {input: $scope.input},
file: $file
}).success(function (data, status, headers, config) {
$scope.uploadResult.push(data.toString());
// to fix IE not refreshing the model
if (!$scope.$$phase) {
$scope.$apply();
}
});
}
}
}];
</script>
</body>
</html>
Binary file added war/js/FileAPI.flash.swf
Binary file not shown.
Loading

0 comments on commit c215133

Please sign in to comment.