Blob Image Form File for Yaml Test Object

How to set up blob test image using yaml structure?

Also, what is the database structure for the blob file? (Databases)

+5
source share
4 answers

I experienced the same problem some time ago in a project. However, since I could not find a way to solve this problem using fixtures (since the database stores the blob object as a string, as described above), I created a workaround to at least solve this problem in a test scenario. I created the following file /app/job/Bootstrap.java:

import play.test.*;
import play.jobs.*;
import play.db.DB;
import models.*;

import java.util.List;

@OnApplicationStart
public class Bootstrap extends Job {
     public void doJob() {
        // Load default data if the database is empty
        if(Item.count() == 0) {
            Fixtures.loadModels("my_fixtures.yml");
            List<Item> allItems = Item.findAll();
            for (Item a: allItems){
                DB.execute("UPDATE `Item` SET image='item_" + a.name.toLowerCase() + ".png|image/png' WHERE id=" + a.getId());
            }
        }
    }
}

, , - , "Item".
- "", ! , my_fixtures.yml. , .

, OP, .

EDIT: , , application.conf, : "item_ <item_name_in_lowercase > " ".png"

+3

, .

, , application.conf. , .

, , , , (VARCHAR, TEXT)

+1

"/", , (application.conf)

(varchar ) : mime. :

12345asbcdefghi12345abcdfed|image/jpeg

- . Play UUID , . , , . (: , , , !)

The second part (after |) is the myme type. Play uses the magic-myme library for automatic detection.

You can see the code here .

+1
source

Here is a modified version of Unji's answer loading images from a folder in conf, note that I deleted all import statements:

/**
 * A job executed when the application starts.
 */
@OnApplicationStart
public class Bootstrap extends Job {

  /**
   * Loads the initial data if there are no
   * WebAdministrators at the database.
   * <p>
   *   It loads images on the post with the following criteria:
   *   <ol>
   *     <li>file loaction: /conf/initialMedia/</li>
   *     <li>file name: {post.title.toCamelCase()}-{i}.jpg</li>
   *   </ol>
   *   Where i must start in 0.
   * </p>
   */
  @Override
  public void doJob() {
    // Check if the database is empty
    if(WebAdministrator.count() == 0) {
      Logger.info("Loading Initial Data.");
      Fixtures.loadModels("initial-data.yml");
      List<Post> posts = Post.findAll();
      for (Post post: posts) {
        Logger.info("Looking for files for post: [" + post.title + "]");
        for (int i=0; true; i++) {
          VirtualFile vf = VirtualFile.fromRelativePath("/conf/initialMedia/"
              + JavaExtensions.camelCase(post.title) + "-" + i + ".jpg");
          File imageFile = vf.getRealFile();

          if (imageFile.exists()) {
            try {
              Blob blobImage = new Blob();
              blobImage.set(new FileInputStream(imageFile), MimeTypes.getContentType(imageFile.getName()));
              MediaItem mediaItem = new Image(blobImage);
              mediaItem.save();
              post.mediaItems.add(mediaItem);
              post.save();
              Logger.info("File: [%s] Loaded", imageFile.getAbsolutePath());
            } catch (FileNotFoundException e) {
              // this should never happen.
            }
          } else {
            Logger.info("Media Loaded for post [%s]: %d files.", post.title, i);
            break;
          }
        }
      }
    }
  }
}
0
source

All Articles