// OMERO_C++_FileAttachements.cpp // // Domain #include #include #include #include #include #include #include #include #include // Std #include #include #include using namespace std; using namespace omero::rtypes; int main(int argc, char* argv[]) { //omero objects/services omero::client_ptr omero; omero::api::ServiceFactoryPrx sf; omero::api::GatewayPrx gateway; omero::api::IQueryPrx query; omero::api::IUpdatePrx up; omero::api::RawFileStorePrx rawfilestore; //something to retrieve off the server, say an Image Ice::Long imgId = 101; //intialise connection and services we need omero = new omero::client("my.server.somewhere.com", 4063); sf = omero->createSession("myusername", "mypassword"); gateway = sf->createGateway(); query = sf->getQueryService(); rawfilestore = sf->createRawFileStore(); up = sf->getUpdateService(); //Get the image with the annotations loaded string querystr = "select i from Image i left outer join fetch i.annotationLinks alinks left outer join fetch alinks.child as a where i.id = :uId"; omero::sys::ParametersIPtr p = new omero::sys::ParametersI(); p->add("uId", rlong(imgId)); omero::model::ImagePtr img = omero::model::ImagePtr::dynamicCast(query->findByQuery(querystr, p)); //open our movie file string movieFile = "test.mpg"; string fullPath = "C://Temp//"+movieFile; ifstream infile(fullPath.data(), ios::in); if (infile.is_open()) { //work out our file size in bytes long begin, end, size; begin = infile.tellg(); infile.seekg (0, ios::end); end = infile.tellg(); size = end-begin; //create our OriginalFile object omero::model::OriginalFilePtr originalFile = new omero::model::OriginalFileI(); omero::model::FormatPtr f = omero::model::FormatPtr::dynamicCast(query->findByQuery("from Format as f where f.value='video/mpeg'", 0)); originalFile->setName(rstring(movieFile)); originalFile->setPath(rstring(fullPath)); originalFile->setSize(rlong(size)); originalFile->setSha1(rstring("pending")); originalFile->setFormat(f); //save our OriginalFile to the server so it gets an ID originalFile = omero::model::OriginalFilePtr::dynamicCast(up->saveAndReturnObject(originalFile)); //setup our rawfilestore so we can write the file on the server rawfilestore->setFileId(originalFile->getId()->getValue()); //get our file into a Ice::ByteSeq object Ice::ByteSeq bytes; bytes.resize(size); infile.read(reinterpret_cast(&bytes[0]), bytes.size()); //now close the filestream infile.close(); //write the bytes to the server rawfilestore->write(bytes, 0, size); //setup our file annotation omero::model::FileAnnotationPtr fileannot = new omero::model::FileAnnotationI(); fileannot->setFile(originalFile); //and associate it to the image omero::model::ImageAnnotationLinkPtr annotlink = img->linkAnnotation(fileannot); //and finally save the annotationlink on the server up->saveObject(annotlink); } return 0; }