imagestore.cpp
Go to the documentation of this file.
1 #include "imagestore.hpp"
2 
3 qiLogCategory("test.qi.fileExample.imageStore");
4 
5 #include <map>
6 
7 #include <boost/filesystem/operations.hpp>
8 
10 
11 namespace alice
12 {
13 class ImageStoreImpl : public ImageStore
14 {
15 public:
16  ImageStoreImpl() = default;
17 
18  ~ImageStoreImpl() override
19  {
20  for (const auto& slot : _fileRegistry)
21  {
22  boost::filesystem::remove(slot.second);
23  }
24  }
25 
26  void storeImage(qi::FilePtr imageFile, std::string name) override
27  {
28  // Note that ideally this would be implemented in an asynchronous way,
29  // but for simplicity we will do it synchronously.
30 
31  // First, make a local copy in a temporary files directory:
32  const auto tempFilePath = generateTemporaryFilePath();
33 
34  // This call will block until the end because it returns a FutureSync.
35  qi::copyToLocal(imageFile, tempFilePath);
36 
37  // We now have a local copy of the remote file,
38  // so we don't need the remote access anymore.
39  imageFile.reset();
40 
41  // Now we can work with the local file.
42  storeFileInDatabase(name, tempFilePath);
43  }
44 
45  qi::FilePtr getImage(std::string name) override
46  {
47  const auto fileLocation = findFileLocation(name);
48 
49  // Now we can open it and provide it to the user for reading.
50  return qi::openLocalFile(fileLocation);
51  }
52 
53 private:
54  using FileRegistry = std::map<std::string, qi::Path>;
56 
58  {
59  static int idx = 0;
60  static const std::string PATH_PREFIX = "./temp_image_file_";
61  static const std::string PATH_SUFFIX = ".data";
62 
63  std::stringstream newPath;
64  newPath << PATH_PREFIX << idx << PATH_SUFFIX;
65  ++idx;
66  return newPath.str();
67  }
68 
69  void storeFileInDatabase(const std::string& name, const qi::Path& path)
70  {
71  //...fake it
73  }
74 
75  qi::Path findFileLocation(const std::string& name)
76  {
77  return _fileRegistry[name];
78  }
79 };
80 QI_REGISTER_OBJECT(ImageStore, storeImage, getImage);
81 QI_REGISTER_IMPLEMENTATION(ImageStore, ImageStoreImpl);
82 
84 {
85  static ImageStorePtr imageStore = boost::make_shared<ImageStoreImpl>();
86  return imageStore;
87 }
88 }
alice::ImageStoreImpl::FileRegistry
std::map< std::string, qi::Path > FileRegistry
Definition: imagestore.cpp:54
alice::getImageStore
ImageStorePtr getImageStore()
Definition: imagestore.cpp:83
alice::ImageStore
Definition: imagestore.hpp:11
alice::ImageStorePtr
qi::Object< ImageStore > ImageStorePtr
Definition: imagestore.hpp:26
alice::ImageStoreImpl::ImageStoreImpl
ImageStoreImpl()=default
alice::ImageStoreImpl::_fileRegistry
FileRegistry _fileRegistry
Definition: imagestore.cpp:55
alice::ImageStoreImpl::findFileLocation
qi::Path findFileLocation(const std::string &name)
Definition: imagestore.cpp:75
alice::QI_REGISTER_IMPLEMENTATION
QI_REGISTER_IMPLEMENTATION(ImageStore, ImageStoreImpl)
alice::ImageStoreImpl
Definition: imagestore.cpp:13
alice::ImageStoreImpl::storeFileInDatabase
void storeFileInDatabase(const std::string &name, const qi::Path &path)
Definition: imagestore.cpp:69
alice::ImageStoreImpl::getImage
qi::FilePtr getImage(std::string name) override
Definition: imagestore.cpp:45
alice::ImageStoreImpl::storeImage
void storeImage(qi::FilePtr imageFile, std::string name) override
Definition: imagestore.cpp:26
alice
Definition: imagestore.cpp:11
qi::copyToLocal
FutureSync< void > copyToLocal(FilePtr file, Path localPath)
Definition: fileoperation.cpp:19
alice::ImageStoreImpl::generateTemporaryFilePath
qi::Path generateTemporaryFilePath()
Definition: imagestore.cpp:57
setup.name
name
Definition: setup.py:84
alice::QI_REGISTER_OBJECT
QI_REGISTER_OBJECT(ImageStore, storeImage, getImage)
alice::ImageStoreImpl::~ImageStoreImpl
~ImageStoreImpl() override
Definition: imagestore.cpp:18
bob::storeImage
void storeImage(alice::ImageStorePtr imageStore, const std::string &name, const qi::Path &imageFilePath)
Definition: file_example.cpp:27
qi::openLocalFile
QICORE_API FilePtr openLocalFile(const qi::Path &localPath)
Definition: fileimpl.cpp:186
setup.path
path
Definition: setup.py:114
qiLogCategory
qiLogCategory("test.qi.fileExample.imageStore")
QI_TYPE_INTERFACE
QI_TYPE_INTERFACE(alice::ImageStore)
imagestore.hpp
qi::FilePtr
qi::Object< File > FilePtr
Pointer to a file with shared/remote semantic.
Definition: file.hpp:213


naoqi_libqicore
Author(s): Aldebaran
autogenerated on Wed Sep 14 2022 02:22:41