export: add library assets to zip

This commit is contained in:
Yueyu 2021-10-09 06:55:21 +08:00
parent 1b73e6c30b
commit 4fdfc480fe
4 changed files with 38 additions and 22 deletions

View file

@ -628,20 +628,14 @@ public class JavaScriptDirectInterface {
}
for (int i = 0; i < files.length(); i++) {
String file = files.optString(i);
if (file == null) {
continue;
}
File srcFile = new File(_activity.getFilesDir() + File.separator + file);
if (!srcFile.exists()) {
Log.e(LOG_TAG, "src file not exists" + file);
continue;
}
File targetFile = new File(folder.getAbsolutePath() + File.separator + file);
// Log.d(LOG_TAG, "copying assets" + file);
try {
ScratchJrUtil.copyFile(srcFile, targetFile);
} catch (IOException e) {
e.printStackTrace();
if (file != null) {
File targetFile = new File(folder.getAbsolutePath() + File.separator + file);
try {
this.copyAssetTo(file, targetFile);
} catch (IOException e) {
Log.e(LOG_TAG, "Asset for " + file + " copy failed.");
e.printStackTrace();
}
}
}
}
@ -657,6 +651,16 @@ public class JavaScriptDirectInterface {
return fullName;
}
private void copyAssetTo(String file, File targetFile) throws IOException {
File srcFile = new File(_activity.getFilesDir() + File.separator + file);
if (srcFile.exists()) {
ScratchJrUtil.copyFile(srcFile, targetFile);
} else {
InputStream inputStream = _activity.getAssets().open("HTML5/svglibrary/" + file);
ScratchJrUtil.copyFile(inputStream, targetFile);
}
}
@JavascriptInterface
public void sendSjrUsingShareDialog(String fileName, String emailSubject,
String emailBody, int shareType) {

View file

@ -98,6 +98,10 @@ public class ScratchJrUtil {
throws IOException
{
InputStream in = new FileInputStream(sourceLocation);
copyFile(in, targetLocation);
}
public static void copyFile(InputStream in, File targetLocation) throws IOException {
OutputStream out = new FileOutputStream(targetLocation);
// Copy the bits from instream to outstream

View file

@ -242,13 +242,7 @@ NSMutableDictionary *soundtimers;
NSString *subDir = [projectDir stringByAppendingPathComponent:key];
[fileManager createDirectoryAtPath:subDir withIntermediateDirectories:true attributes:nil error:nil];
for (NSString *file in [metadata valueForKey:key]) {
// copy file to target folder
// NSLog(@"%@ %@", key, file);
NSString *srcPath = [[IO getpath] stringByAppendingPathComponent:file];
NSString *toPath = [subDir stringByAppendingPathComponent:file];
if ([fileManager fileExistsAtPath:srcPath]) {
[fileManager copyItemAtPath:srcPath toPath:toPath error:nil];
}
[IO copyAssetTo:file :subDir];
}
}
@ -270,6 +264,20 @@ NSMutableDictionary *soundtimers;
return fullName;
}
+ (void) copyAssetTo: (NSString *) file :(NSString *) toFolder {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *srcPath = [[IO getpath] stringByAppendingPathComponent:file];
NSString *toPath = [toFolder stringByAppendingPathComponent:file];
if (![fileManager fileExistsAtPath:srcPath]) {
// It's not a user created asset, goto svglibrary to find it.
NSString* libraryPath = [@"/HTML5/svglibrary/" stringByAppendingString:file];
srcPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingString:libraryPath];
}
if ([fileManager fileExistsAtPath:srcPath]) {
[fileManager copyItemAtPath:srcPath toPath:toPath error:nil];
}
}
// Receive a .sjr file from inside the app. Send using native UI - Airdrop or Email
+ (NSString*) sendSjrUsingShareDialog:(NSString *)fileName :(NSString*)emailSubject :(NSString*)emailBody :(int)shareType {

View file

@ -4,7 +4,7 @@ import {setCanvasSize, drawThumbnail} from '../utils/lib';
import SVG2Canvas from '../utils/SVG2Canvas';
const database = 'projects';
const collectLibraryAssets = false;
const collectLibraryAssets = true;
// Sharing state
let zipFileName = '';