| 214 | } |
| 215 | |
| 216 | ExternalFile::ExternalFile(const Model& model, const std::string& filename, bool copyFile) : ResourceObject(ExternalFile::iddObjectType(), model) { |
| 217 | OS_ASSERT(getImpl<detail::ExternalFile_Impl>()); |
| 218 | |
| 219 | WorkflowJSON workflow = model.workflowJSON(); |
| 220 | |
| 221 | // we expect all strings to be UTF-8 encoded |
| 222 | path p = toPath(filename); |
| 223 | if (!exists(p)) { |
| 224 | boost::optional<path> op = workflow.findFile(filename); |
| 225 | if (!op) { |
| 226 | this->remove(); |
| 227 | LOG_AND_THROW("Cannot find file \"" << filename << "\" for " << this->briefDescription()); |
| 228 | } |
| 229 | p = op.get(); |
| 230 | } |
| 231 | OS_ASSERT(exists(p)); |
| 232 | |
| 233 | bool ok; |
| 234 | if (copyFile) { |
| 235 | path destDir; |
| 236 | std::vector<path> absoluteFilePaths = workflow.absoluteFilePaths(); |
| 237 | if (absoluteFilePaths.empty()) { |
| 238 | destDir = workflow.absoluteRootDir(); |
| 239 | } else { |
| 240 | destDir = absoluteFilePaths[0]; |
| 241 | } |
| 242 | path dest = destDir / p.filename(); |
| 243 | |
| 244 | if (exists(dest)) { |
| 245 | if (checksum(p) != checksum(dest)) { |
| 246 | this->remove(); |
| 247 | LOG_AND_THROW("File \"" << p.filename() << "\" already exists in \"" << destDir << "\""); |
| 248 | } |
| 249 | } else { |
| 250 | |
| 251 | try { |
| 252 | makeParentFolder(dest, path(), true); |
| 253 | } catch (std::exception&) { |
| 254 | this->remove(); |
| 255 | LOG_AND_THROW("Failed to created parent folder at \"" << dest << "\""); |
| 256 | } |
| 257 | try { |
| 258 | boost::filesystem::copy(p, dest); |
| 259 | } catch (std::exception&) { |
| 260 | this->remove(); |
| 261 | LOG_AND_THROW("Failed to copy file from \"" << p << "\" to \"" << dest << "\""); |
| 262 | } |
| 263 | } |
| 264 | OS_ASSERT(exists(dest)); |
| 265 | |
| 266 | ok = setFileName(toString(dest.filename())); |
| 267 | OS_ASSERT(ok); |
| 268 | } else { |
| 269 | ok = setFileName(filename); |
| 270 | OS_ASSERT(ok); |
| 271 | } |
| 272 | } |
| 273 |
nothing calls this directly
no test coverage detected