Content-Length: 893549 | pFad | http://github.com/NativeScript/NativeScript/commit/ab32aeaaa3715e5178f349646f14a632c771ce61

46 feat(file-system): append, appendText & createFile (#10285) · NativeScript/NativeScript@ab32aea · GitHub
Skip to content

Commit ab32aea

Browse files
authored
feat(file-system): append, appendText & createFile (#10285)
1 parent 7bb0918 commit ab32aea

File tree

36 files changed

+2069
-644
lines changed

36 files changed

+2069
-644
lines changed

apps/automated/src/file-system/file-system-tests.ts

+60-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as fs from '@nativescript/core/file-system';
55

66
import * as TKUnit from '../tk-unit';
77
import * as appModule from '@nativescript/core/application';
8-
import { isIOS, Device, platformNames } from '@nativescript/core';
8+
import { isIOS, Device, platformNames, isAndroid } from '@nativescript/core';
99

1010
export var testPathNormalize = function () {
1111
// >> file-system-normalize
@@ -719,3 +719,62 @@ export function test_FileCopy(done) {
719719
.then(() => done())
720720
.catch(done);
721721
}
722+
723+
export function testAndroidCreate() {
724+
let testFunc = function testFunc() {
725+
const file = fs.File.android.createFile({
726+
directory: fs.AndroidDirectory.DOWNLOADS,
727+
name: `${Date.now()}.txt`,
728+
mime: 'text/plain',
729+
relativePath: `NativeScript`,
730+
});
731+
732+
file.writeTextSync('some text');
733+
734+
return file;
735+
};
736+
if (isAndroid) {
737+
const file = testFunc();
738+
TKUnit.assertEqual(file.readTextSync(), 'some text', `The contents of the new file created in the 'AndroidDirectory.DOWNLOADS' folder are not as expected.`);
739+
file.removeSync();
740+
TKUnit.assertTrue(!fs.File.exists(file.path));
741+
} else {
742+
TKUnit.assertThrows(testFunc, `Trying to retrieve createFile on a platform different from Android should throw!`, `createFile is available on Android only!`);
743+
}
744+
}
745+
746+
export function test_FileAppend(done) {
747+
const content = 'Hello World';
748+
const hello_world = global.isIOS ? NSString.stringWithString(content).dataUsingEncoding(NSUTF8StringEncoding) : new java.lang.String(content).getBytes('UTF-8');
749+
const file = fs.File.fromPath(fs.path.join(fs.knownFolders.temp().path, `${Date.now()}-app.txt`));
750+
file
751+
.appendText('Hello')
752+
.then(() => file.appendText(' World'))
753+
.then(() => {
754+
TKUnit.assert(file.size === hello_world.length);
755+
return file.readText();
756+
})
757+
.then((value) => {
758+
TKUnit.assert(value === content);
759+
760+
return Promise.allSettled([file.remove()]);
761+
})
762+
.then(() => done())
763+
.catch(done);
764+
}
765+
766+
export function test_FileAppendText(done) {
767+
const content = 'Hello World';
768+
const file = fs.File.fromPath(fs.path.join(fs.knownFolders.temp().path, `${Date.now()}-app.txt`));
769+
file
770+
.appendText('Hello')
771+
.then(() => file.appendText(' World'))
772+
.then(() => file.readText())
773+
.then((value) => {
774+
TKUnit.assert(value === content);
775+
776+
return Promise.allSettled([file.remove()]);
777+
})
778+
.then(() => done())
779+
.catch(done);
780+
}

apps/toolbox/src/pages/fs-helper.ts

+101-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Page, EventData, Application, File, Folder, knownFolders, path, getFileAccess, Utils } from '@nativescript/core';
1+
import { Page, EventData, Application, File, Folder, knownFolders, path, getFileAccess, Utils, Screen, Http, AndroidDirectory, ImageSource, alert } from '@nativescript/core';
22

33
let page: Page;
44

@@ -217,3 +217,103 @@ function getFileNameFromContent(content: string) {
217217
const file = getFileAccess().getFile(content);
218218
return decodeURIComponent(file.name).split('/').pop().toLowerCase();
219219
}
220+
221+
let lastDownload: File;
222+
export function createFileInDownloads() {
223+
if (!global.isAndroid) {
224+
return;
225+
}
226+
const width = Screen.mainScreen.widthPixels;
227+
const height = Screen.mainScreen.heightPixels;
228+
const randomImageUrl = `https://picsum.photos/${width}/${height}.jpg?random=${Date.now()}`;
229+
230+
Http.getFile(randomImageUrl).then((result: File) => {
231+
let file = File.android.createFile({
232+
directory: AndroidDirectory.DOWNLOADS,
233+
name: `${Date.now()}.jpg`,
234+
mime: 'image/jpeg',
235+
relativePath: `NativeScript`,
236+
});
237+
result
238+
.copy(file.path)
239+
.then((done) => {
240+
lastDownload = file;
241+
console.log('done: ' + done + '\n' + 'Original path: ' + result.path + '\n' + 'Copied to: ' + file.path + '\n' + 'Original size: ' + result.size + '\n' + 'Copy size: ' + file.size + '\n');
242+
alert(`File saved in ${AndroidDirectory.DOWNLOADS}/NativeScript`);
243+
})
244+
.catch((error) => {
245+
console.error(error);
246+
});
247+
});
248+
}
249+
250+
export function createFileInGallery() {
251+
if (!global.isAndroid) {
252+
return;
253+
}
254+
const width = Screen.mainScreen.widthPixels;
255+
const height = Screen.mainScreen.heightPixels;
256+
const randomImageUrl = `https://picsum.photos/${width}/${height}.jpg?random=${Date.now()}`;
257+
258+
Http.getFile(randomImageUrl).then((result: File) => {
259+
let file = File.android.createFile({
260+
directory: AndroidDirectory.PICTURES,
261+
name: `${Date.now()}.jpg`,
262+
mime: 'image/jpeg',
263+
relativePath: `NativeScript`,
264+
});
265+
result
266+
.copy(file.path)
267+
.then((done) => {
268+
console.log('done: ' + done + '\n' + 'Original path: ' + result + '\n' + 'Copied to: ' + file.path + '\n' + 'Original size: ' + result.size + '\n' + 'Copy size: ' + file.size + '\n');
269+
alert(`File saved in ${AndroidDirectory.PICTURES}/NativeScript`);
270+
})
271+
.catch((error) => {
272+
console.error(error);
273+
});
274+
});
275+
}
276+
277+
export function createFileInMusic() {
278+
if (!global.isAndroid) {
279+
return;
280+
}
281+
282+
Http.getFile('https://github.com/rafaelreis-hotmart/Audio-Sample-files/raw/master/sample.mp3').then((result: File) => {
283+
let file = File.android.createFile({
284+
directory: AndroidDirectory.MUSIC,
285+
name: `${Date.now()}.MP3`,
286+
mime: 'audio/mp3',
287+
relativePath: `NativeScript/MP3`,
288+
});
289+
290+
result
291+
.copy(file.path)
292+
.then((done) => {
293+
console.log('done: ' + done + '\n' + 'Original path: ' + result + '\n' + 'Copied to: ' + file.path + '\n' + 'Original size: ' + result.size + '\n' + 'Copy size: ' + file.size + '\n');
294+
295+
alert(`File saved in ${AndroidDirectory.MUSIC}/NativeScript/MP3`);
296+
})
297+
.catch((error) => {
298+
console.error(error);
299+
});
300+
});
301+
}
302+
303+
export function createAndAppendText() {
304+
const file = File.fromPath(path.join(knownFolders.temp().path, `${Date.now()}-app.txt`));
305+
file.appendTextSync('Hello');
306+
file.appendTextSync(' World');
307+
const data = file.readTextSync();
308+
console.log('createAndAppend:', data === 'Hello World');
309+
}
310+
311+
export function createAndAppendData() {
312+
const file = File.fromPath(path.join(knownFolders.temp().path, `${Date.now()}-app.txt`));
313+
const hello = global.isIOS ? NSString.stringWithString('Hello').dataUsingEncoding(NSUTF8StringEncoding) : new java.lang.String('Hello').getBytes('UTF-8');
314+
const world = global.isIOS ? NSString.stringWithString(' World').dataUsingEncoding(NSUTF8StringEncoding) : new java.lang.String(' World').getBytes('UTF-8');
315+
file.appendSync(hello);
316+
file.appendSync(world);
317+
const data = file.readTextSync();
318+
console.log('createAndAppendData:', data === 'Hello World');
319+
}

apps/toolbox/src/pages/fs-helper.xml

+5
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,10 @@
55
<Button text="Pick File" tap="pickFile" />
66
<Button text="Pick Multiple Files" tap="pickFiles" />
77
<Button text="Test Copy" tap="copyTest" />
8+
<Button text="External File Creation" tap="createFileInDownloads" />
9+
<Button text="Add file to gallery" tap="createFileInGallery" />
10+
<Button text="Add file to music" tap="createFileInMusic" />
11+
<Button text="Append Text" tap="createAndAppendText" />
12+
<Button text="Append Data" tap="createAndAppendData" />
813
</StackLayout>
914
</Page>

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/NativeScript/NativeScript/commit/ab32aeaaa3715e5178f349646f14a632c771ce61

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy