ProjectPackage.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "platform/Paths.h"
  2. #include "engine/ProjectPackage.h"
  3. #include "core/MissingFiles.h"
  4. #include "core/ProjectSafety.h"
  5. #include "engine/ProjectFile.h"
  6. namespace lynx::ProjectPackage
  7. {
  8. namespace
  9. {
  10. juce::String str (const std::string& s) { return juce::String::fromUTF8 (s.c_str()); }
  11. juce::File fileFor (const std::string& path)
  12. {
  13. const auto p = str (path);
  14. return juce::File::isAbsolutePath (p) ? juce::File (p) : juce::File();
  15. }
  16. } // namespace
  17. bool exportZip (const Song& song, const juce::String& projectName, const juce::File& zip, Result& result, juce::String& error)
  18. {
  19. result = {};
  20. const auto plan = safety::packagePlan (song, "samples");
  21. Song packed = song;
  22. std::map<std::string, std::string> relink;
  23. juce::ZipFile::Builder builder;
  24. for (const auto& [original, inside] : plan)
  25. {
  26. const auto source = fileFor (original);
  27. if (! source.existsAsFile())
  28. {
  29. result.missing.add (str (original));
  30. continue; // the project keeps the old path (the Missing files dialog can find it later)
  31. }
  32. builder.addFile (source, 0, str (inside)); // samples are already compressed audio or barely compress
  33. relink[original] = inside;
  34. ++result.files;
  35. }
  36. applyRelocations (packed, relink);
  37. const auto name = juce::File::createLegalFileName (projectName.isNotEmpty() ? projectName : juce::String ("untitled"));
  38. const auto json = ProjectFile::toJson (packed);
  39. builder.addEntry (new juce::MemoryInputStream (json.toRawUTF8(), json.getNumBytesAsUTF8(), true), 9, name + ProjectFile::extension,
  40. juce::Time::getCurrentTime());
  41. juce::TemporaryFile temp (zip);
  42. {
  43. juce::FileOutputStream out (temp.getFile());
  44. if (! out.openedOk() || ! builder.writeToStream (out, nullptr))
  45. {
  46. error = "could not write " + zip.getFullPathName();
  47. return false;
  48. }
  49. }
  50. if (! temp.overwriteTargetFileWithTemporary())
  51. {
  52. error = "could not write " + zip.getFullPathName();
  53. return false;
  54. }
  55. return true;
  56. }
  57. bool collectSamples (const Song& song, const juce::File& folder, std::map<std::string, std::string>& relink, Result& result,
  58. juce::String& error)
  59. {
  60. result = {};
  61. relink.clear();
  62. if (! folder.createDirectory())
  63. {
  64. error = "could not create " + folder.getFullPathName();
  65. return false;
  66. }
  67. for (const auto& original : safety::referencedSamples (song))
  68. {
  69. const auto source = fileFor (original);
  70. if (! source.existsAsFile())
  71. {
  72. result.missing.add (str (original));
  73. continue;
  74. }
  75. if (source.getParentDirectory() == folder) // already there
  76. {
  77. relink[original] = source.getFullPathName().toStdString();
  78. continue;
  79. }
  80. const auto stem = safety::uniqueName (source.getFileNameWithoutExtension().toStdString(), [&] (const std::string& s) {
  81. return folder.getChildFile (str (s) + source.getFileExtension()).exists();
  82. });
  83. const auto target = folder.getChildFile (str (stem) + source.getFileExtension());
  84. if (! source.copyFileTo (target))
  85. {
  86. error = "could not copy " + source.getFullPathName();
  87. return false;
  88. }
  89. relink[original] = target.getFullPathName().toStdString();
  90. ++result.files;
  91. }
  92. return true;
  93. }
  94. juce::File extractZip (const juce::File& zipFile, const juce::File& destinationRoot, juce::String& error)
  95. {
  96. juce::ZipFile zip (zipFile);
  97. if (zip.getNumEntries() == 0)
  98. {
  99. error = "not a project package (empty or not a zip file)";
  100. return {};
  101. }
  102. bool hasProject = false;
  103. for (int i = 0; i < zip.getNumEntries(); ++i)
  104. if (const auto* e = zip.getEntry (i); e != nullptr && (e->filename.endsWithIgnoreCase (ProjectFile::extension) || e->filename.endsWithIgnoreCase (".flp")))
  105. hasProject = true;
  106. if (! hasProject)
  107. {
  108. error = "the package has no project file (.lynx or .flp)";
  109. return {};
  110. }
  111. destinationRoot.createDirectory();
  112. const auto folder = destinationRoot.getNonexistentChildFile (juce::File::createLegalFileName (zipFile.getFileNameWithoutExtension()), {}, false);
  113. if (! folder.createDirectory())
  114. {
  115. error = "could not create " + folder.getFullPathName();
  116. return {};
  117. }
  118. if (const auto r = zip.uncompressTo (folder, false); r.failed()) // (JUCE refuses entries outside the folder)
  119. {
  120. error = r.getErrorMessage();
  121. return {};
  122. }
  123. for (const auto* pattern : { "*.lynx", "*.flp" })
  124. {
  125. auto found = folder.findChildFiles (juce::File::findFiles, true, pattern);
  126. found.sort();
  127. if (! found.isEmpty())
  128. return found.getFirst();
  129. }
  130. error = "the package has no project file (.lynx or .flp)";
  131. return {};
  132. }
  133. juce::File defaultExtractRoot()
  134. {
  135. return lynx::platform::documentsFolder().getChildFile ("LYNX/Projects/Unpacked");
  136. }
  137. } // namespace lynx::ProjectPackage