Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Commit

Permalink
Fix for #58: let me upload a Java jar to Herd
Browse files Browse the repository at this point in the history
  • Loading branch information
FroMage committed Jun 22, 2012
1 parent 8ce9bfb commit da82753
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 10 deletions.
29 changes: 22 additions & 7 deletions app/controllers/UploadAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import play.Logger;
import play.data.validation.Required;
import play.data.validation.Validation;
import play.mvc.Before;
import util.JavaExtensions;
import util.ModuleChecker;
Expand Down Expand Up @@ -221,22 +222,23 @@ public static void deleteFile(Long id, String path, boolean returnToBrowse) thro
Uploads.view(id);
}

public static void uploadRepo(Long id, @Required File repo) throws ZipException, IOException{
public static void uploadRepo(Long id, @Required File repo, String module, String version) throws ZipException, IOException{
models.Upload upload = Uploads.getUpload(id);
File uploadsDir = Util.getUploadDir(upload.id);

if(validationFailed()){
Uploads.uploadRepoForm(id);
}

String name = repo.getName();
String name = repo.getName().toLowerCase();

if(name.endsWith(".zip"))
uploadZip(repo, uploadsDir);
else{
boolean found = false;
for(String postfix : SupportedExtensions){
if(name.endsWith(postfix)){
uploadArchive(repo, postfix, uploadsDir);
uploadArchive(repo, postfix, uploadsDir, module, version, id);
found = true;
break;
}
Expand All @@ -249,15 +251,28 @@ public static void uploadRepo(Long id, @Required File repo) throws ZipException,
Uploads.view(id);
}

private static void uploadArchive(File file, String postfix, File uploadsDir) throws IOException {
private static void uploadArchive(File file, String postfix, File uploadsDir, String module, String version, Long uploadId) throws IOException {
try{
// parse
ModuleSpec spec = ModuleSpec.parse(file.getName(), postfix);
// parse unless it's a jar and we have alternate info
ModuleSpec spec;
Logger.info("postfix: %s, module: %s, version: %s", postfix, module, version);
if(postfix.equals(".jar") && (!StringUtils.isEmpty(module) || !StringUtils.isEmpty(version))){
Validation.required("module", module);
if("default".equals(module))
Validation.addError("module", "Default module not allowed");
Validation.required("version", version);
if(validationFailed()){
Uploads.uploadRepoForm(uploadId);
}
spec = new ModuleSpec(module, version);
}else{
spec = ModuleSpec.parse(file.getName(), postfix);
}

// build dest file
String path = spec.name.replace('.', File.separatorChar)
+ File.separatorChar + spec.version
+ File.separatorChar + file.getName();
+ File.separatorChar + spec.name + '-' + spec.version + postfix;
File destFile = new File(uploadsDir, path);

// check
Expand Down
1 change: 0 additions & 1 deletion app/controllers/Uploads.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,4 @@ public static void uploadRepoForm(Long id){
models.Upload upload = Uploads.getUpload(id);
render(upload);
}

}
85 changes: 83 additions & 2 deletions app/views/Uploads/uploadRepoForm.html
Original file line number Diff line number Diff line change
@@ -1,22 +1,103 @@
#{extends 'main.html' /}
#{set title:'Upload a repo archive or artifact' /}
#{set hasForm: true /}
#{set 'moreStyles'}
<style type="text/css">
.jar {
display: none;
}
</style>
#{/set}
#{set 'moreScripts'}
<script type="text/javascript">
function checkUpload(input){
var path = jQuery(input).val();
if(!path){
hideJarForm();
return;
}
var sep = path.lastIndexOf("\\");
if(sep == -1)
sep = path.lastIndexOf("/");
var name;
if(sep != -1)
name = path.substring(sep+1);
else
name = path;
if(!name){
hideJarForm();
return;
}
if(name.toLowerCase().match(/\.jar$/)){
// get rid of extension
name = name.substring(0, name.length - 4);
// split at version separator if any
sep = name.indexOf("-");
if(sep != -1){
jQuery("#module").val(name.substring(0, sep));
jQuery("#version").val(name.substring(sep+1));
}else{
jQuery("#module").val(name);
}
showJarForm();
}else{
hideJarForm();
}
}
function hideJarForm(){
jQuery(".jar").hide();
}
function showJarForm(){
jQuery(".jar").show();
}
</script>
#{/set}

<p>
<a href="@{Uploads.view(upload.id)}" title="Cancel and go back to upload">Back to upload</a>
</p>

<!-- Those are not visible by default so we push them up -->
#{ifError 'module'}
<div class="alert alert-error">
Module name required if you specify a version
</div>
#{/ifError}
#{ifError 'version'}
<div class="alert alert-error">
Module version required if you specify a name
</div>
#{/ifError}

#{form @UploadAPI.uploadRepo(upload.id), enctype:'multipart/form-data'}

<fieldset>
<legend>Upload a repo archive</legend>
<div class="control-group #{errorClassBootstrap 'repo'/}">
<label class="control-label" for="Repo">Repo archive file (.zip) or module artifact (.car, .jar or .src)</label>
<label class="control-label" for="repo">Repo archive file (.zip) or module artifact (.car, .jar or .src)</label>
<div class="controls">
<input name="repo" type="file" value="${flash.repo}" title="Choose file to upload"/>
<input name="repo" type="file" value="${flash.repo}" onchange="checkUpload(this);" title="Choose file to upload"/>
<span class="help-inline">​#{error 'repo'/}</span>
</div>
</div>
<div class="alert alert-info jar">
It appears that you are uploading a Jar file. We tried to guess the module name and version
but you can change them if you disagree.
</div>
<div class="control-group #{errorClassBootstrap 'module'/} jar">
<label class="control-label" for="module">Module name for the given Jar file</label>
<div class="controls">
<input name="module" id="module" value="${flash.module}" title="Choose module name"/>
<span class="help-inline">​#{error 'module'/}</span>
</div>
</div>
<div class="control-group #{errorClassBootstrap 'version'/} jar">
<label class="control-label" for="version">Module version for the given Jar file</label>
<div class="controls">
<input name="version" id="version" value="${flash.version}" title="Choose module version"/>
<span class="help-inline">​#{error 'version'/}</span>
</div>
</div>
<div class="form-actions">
<a class="btn" href="@{Uploads.view(upload.id)}" title="Cancel and go back to upload">Cancel</a>
<input type="submit" class="btn btn-primary" value="Upload file"
Expand Down

0 comments on commit da82753

Please sign in to comment.