为测试对象 Yaml 播放框架图像 BLOB 文件

发布于 2024-11-27 11:34:01 字数 69 浏览 2 评论 0原文

如何使用 yaml 结构设置测试 Blob 图像?

另外,BLOB 文件的数据库结构是什么? (MySQL)

How do you set up a Test Blob Image using the yaml structure?

Also, what is the database structure for a BLOB file? (MySQL)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

苍风燃霜 2024-12-04 11:34:01

不久前,我在一个项目中遇到了同样的问题。然而,由于我找不到使用固定装置解决此问题的方法(因为数据库将 blob 对象存储为字符串,如 Pere 上面所解释的),因此我创建了一个解决方法,至少在测试用例场景中解决此问题。我创建了以下文件 /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());
            }
        }
    }
}

如果数据库中尚未存储“项目”,我要做的第一件事就是用初始数据填充数据库。
第二件事是迭代所有播放的“项目”!只是存储在数据库中,从“my_fixtures.yml”文件中读取。对于每个项目,字符串字段都会更新,如上例所示。

我知道这并不完全是OP中问题的答案,但它提供了一些解决这个问题的想法。

编辑:在上面给出的示例中,我假设图片被手动上传到您的附件文件夹中在您的 application.conf 中给出,每个图像名称类似于:“item_”带有“.png”扩展名

I have experienced the same kind of problem a while ago on a project. However as I could not find a way to solve this with the fixtures (as the database stores the blob object as a string as Pere explained above), I created a workaround to at least solve this problem in a test-case-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());
            }
        }
    }
}

The first thing I do is filling the database with initial data if there are no 'Item' already stored in the database.
The second thing is iterating over all the 'Item' which play! just stored in the database, which are read from the "my_fixtures.yml" file. Here for each item the string field will get updated as shown in the example above.

I know this is not exactly the answer to question in the OP, but it gives some kind idea to work around this issue..

EDIT: In the example given above I assume that the pictures are uploaded manually to your attachment folder as given in your application.conf, and that each image name is like: "item_<item_name_in_lowercase>" with a ".png" extension

苏辞 2024-12-04 11:34:01

嗯,在这一点上,比赛是相当奇怪的。

该 blob 未保存到数据库中,而是保存在 application.conf 中定义的上传文件夹中。它是保存在数据库中的文件的路径。

我现在无法检查,但我似乎记得它们被保存为文本表示形式(VARCHAR,TEXT)

Well, play is quite weird on that point.

The blob is not saved into the database but in a upload folder defined in your application.conf. It is the path toward the file that is saved in the database.

I cannot check it right now, but I seem to recall they are saved as textuel representations (VARCHAR, TEXT)

最佳男配角 2024-12-04 11:34:01

该 blob 保存在文件系统中,如果我没记错的话,默认情况下位于“数据/附件”下,但您可以在配置(application.conf)中更改它

在数据库中,它存储为具有两个组成部分的字符串(大多数数据库中为 varchar):名称和 mime 类型。它看起来像:

12345asbcdefghi12345abcdfed|image/jpeg

第一部分是文件的名称。当您上传文件时,Play 会生成一个唯一的 UUID 作为名称以避免冲突。是的,这意味着您将失去原来的名字。 (注意:现在我对名称部分有疑问,我发誓它已经丢失了,但我可能是错的!)

第二部分(在 | 之后)是 myme 类型。 Play 使用 magic-myme 库来自动检测它。

您可以在此处查看代码

The blob is saved in the file system, by default under "data/attachments" if I recall correctly, but you can change that in the configuration (application.conf)

In the database, it's stored as a String (varchar in most DB) with two components: the name and the mime type. It looks like:

12345asbcdefghi12345abcdfed|image/jpeg

The first part is the name of the file. When you upload a file Play generates a unique UUID as name to avoid collision. Yes, this means you are loosing the original name. (note: now I'm having doubts on the name part, I would swear it is lost, but I may be wrong!)

The second part (after the |) is the myme type. Play uses a magic-myme library to automatically detect it.

You can see the code here.

骄兵必败 2024-12-04 11:34:01

这是 Unji 答案的修改版本,它从 conf 中的文件夹加载图像,请注意,我已经删除了所有导入语句:

/**
 * 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;
          }
        }
      }
    }
  }
}

Here is a modified version of Unji's answer that loads the images from a folder in conf, please note that I have removed all the 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;
          }
        }
      }
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文