Erick
Erick

Reputation: 147

Storing a image with groovy

So, I've started to work with grails a couple of days ago and now I'm having a problem:

class Activity {

  String title
  String description
  String category
  String tags
  String priority

  //<?> image <-
  User owner

  static hasMany = [tis: TimeInvested]

  static constraints = {
    title unique: true
    category nullable: true, inList : ["Trabalho", "Lazer"]
    tags nullable: true
    description nullable: true
    priority nullable: true, inList :["Alta", "Media", "Baixa"]
  }

  Double getInvestedHours(){
    def soma = 0.0d
    this.tis.each {
      soma += it.hours
    }
    soma
  }

  String toString() {
    title
  }
}

As you can see I'm trying to create a activity that has some properties and of this is a image, but I don't know in what type store this image. Also I'm trying to get the its value from a html page, like this:

<form class="form-inline povmt-form" action="${createLink(controller: 'Activity', action: 'save')}" method="POST">
  <div class="form-group">
    <input type="hidden" name="owner.id" value="${session.user.id}">
	<div class="input-group" style="width: 100%">
	<div class="input-group-addon"  style="width: 30%">Titulo</div>
	<input type="text" name="title" class="form-control" placeholder="Titulo" required>
  </div>
  <div class="input-group" style="width: 100%">
    <div class="input-group-addon"  style="width: 30%">Descrição</div>
    <input type="text" name="description" class="form-control" placeholder="Descrição">
  </div>
  <div class="input-group" style="width: 100%">
	<div class="input-group-addon"  style="width: 30%">Imagem</div>
	<input type="file" name="image" class="form-control" accept="image/*" capture="camera">
  </div>
  <div class="input-group" style="width: 100%">
    <div class="input-group-addon" style="width: 30%">Categoria</div>
	<g:select name="category" class="form-control" from="${['Trabalho', 'Lazer']}"/>
    <!-- <select name="" id=""></select> -->
  </div>
  <div class="input-group" style="width: 100%">
    <div class="input-group-addon" style="width: 30%">Prioridade</div>
	<g:select name="priority" class="form-control" from="${['Alta', 'Media', 'Baixa']}"/>
  </div>
  <div class="input-group" style="width: 100%; display: none">
    <div class="input-group-addon" style="width: 30%">Tags</div>
    <input type="text" name="tags" class="form-control" placeholder="Tags">
  </div>
  <input type="submit" class="btn btn-primary btn-lg" value="Adicionar atividade" style="width: 100%; margin-top: 40px;">
  </div>
</form>

Do you guys, have any ideas of how can I get a image from a form in a html page and save into a groovy variable to create a new activity? I'm really newbie at web development so any help is welcome :)

P.S: English is not my mother language so sorry for any mistakes.

Upvotes: 0

Views: 901

Answers (1)

user235273
user235273

Reputation:

What I usually do is upload image to a separate storage like say Amazon S3. You can have a domain class for saving image details like url, key object id, name etc. Then in the Activity class reference your image object. In grails by convention controllers should be named like ActivityController.groovy. Activity.groovy generally refers to domain class. And for getting input and validating it you use a command class like ActivityCommand which can be inside your controller class.

// Activity.groovy (domain)
class Activity {
    Image image
    // ..
}

// Image.groovy (domain)
class Image {
    // ...
    String name
    String objId
    String url
    // ..
}

You can use <g:uploadForm> and have <input type="file" name="image"> in it. Then in your controller get the image like:

// ActivityController.groovy
// ..
def save() {
   def file = request.getFile('image')  //MultipartFile (spring)
   byte[] ba = file.getBytes()
   // call S3 upload service ... which will return the Image object.
   activityService.save(/*params which includes the image object*/)
}

// ActivityService.groovy
// ..
def saveActivity(Image img, /* .. */) {
    Activity a = new Activity()
    a.setImage(img)
    // .. 
    a.save()  // saves to DB
}

Upvotes: 1

Related Questions