Skip to content

Commit

Permalink
Issue #SB-16923 merge: Merge pull request #219 from balakrishna10/rel…
Browse files Browse the repository at this point in the history
…ease-2.7.0

Issue #SB-16923 feat: SDK changes for content sharing
  • Loading branch information
swayangjit authored Jan 6, 2020
2 parents 064f4f5 + 66def08 commit ca750aa
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/content/def/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export interface ContentImport {
export interface ContentExportRequest {
destinationFolder: string;
contentIds: string[];
saveLocally?: boolean;
}

export interface ContentMarkerRequest {
Expand Down
83 changes: 83 additions & 0 deletions src/content/handlers/export/copy-to-destination.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { CopyToDestination } from './copy-to-destination';
import {FileService} from '../../../util/file/def/file-service';
import {ContentEntry} from '../../db/schema';
import {Response} from '../../../api'

describe('CopyToDestination', () => {
let copyToDestination: CopyToDestination;
const mockFileService: Partial<FileService> = {};

beforeAll(() => {
copyToDestination = new CopyToDestination();
});

beforeEach(() => {
jest.clearAllMocks();
});

it('should be create a instance of copy asset', () => {
expect(copyToDestination).toBeTruthy();
});

it('should be copied a file by invoked exicute() for error MEssage', async (done) => {
// arrange
const contentEntrySchema: ContentEntry.SchemaMap[] = [{
identifier: 'IDENTIFIER',
server_data: 'SERVER_DATA',
local_data: '{"children": [{"DOWNLOAD": 1}, "do_234", "do_345"], "artifactUrl": "http:///do_123"}',
mime_type: 'MIME_TYPE',
manifest_version: 'MAINFEST_VERSION',
content_type: 'CONTENT_TYPE',
content_state: 2,
}];

const exportContext = {
destinationFolder: 'SAMPLE_DESTINATION_FOLDER',
tmpLocationPath: 'SAMPLE_TEMP_PATH',
contentModelsToExport: contentEntrySchema,
items: ['artifactUrl'],
metadata: {'SAMPLE_KEY': 'SAMPLE_META_DATA'},
ecarFilePath: 'sampledir/samplefile'
};
const response: Response = new Response();

response.body = exportContext;
// act
await copyToDestination.execute(response, 'SAMPLE_DESTINATION_FOLDER').then((result) => {
// assert
expect(result).toEqual(response);
done();
});
});

it('should be copied a file by invoked exicute() for error MEssage', async (done) => {
// arrange
const contentEntrySchema: ContentEntry.SchemaMap[] = [{
identifier: 'IDENTIFIER',
server_data: 'SERVER_DATA',
local_data: '{"children": [{"DOWNLOAD": 1}, "do_234", "do_345"], "artifactUrl": "http:///do_123"}',
mime_type: 'MIME_TYPE',
manifest_version: 'MAINFEST_VERSION',
content_type: 'CONTENT_TYPE',
content_state: 2,
}];

const exportContext = {
destinationFolder: 'SAMPLE_DESTINATION_FOLDER',
tmpLocationPath: 'SAMPLE_TEMP_PATH',
contentModelsToExport: contentEntrySchema,
items: ['artifactUrl'],
metadata: {'SAMPLE_KEY': 'SAMPLE_META_DATA'},
ecarFilePath: 'sampledir/samplefile'
};
const response: Response = new Response();

response.body = exportContext;
// act
await copyToDestination.execute(response, 'SAMPLE_DESTINATION_FOLDER').catch((result) => {
// assert
done();
});
});

});
28 changes: 28 additions & 0 deletions src/content/handlers/export/copy-to-destination.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Response } from '../../../api';
import {FileUtil} from '../../../util/file/util/file-util';
import { ContentExportRequest } from '../..';

export class CopyToDestination {

constructor() {
}

public async execute(exportResponse: Response, contentExportRequest: ContentExportRequest): Promise<Response> {
return new Promise<Response>((resolve, reject) => {
let destinationFolder;
if (contentExportRequest.saveLocally) {
destinationFolder = contentExportRequest.destinationFolder;
} else {
destinationFolder = cordova.file.externalCacheDirectory;
}
buildconfigreader.copyFile(FileUtil.getDirecory(exportResponse.body.ecarFilePath), destinationFolder,
FileUtil.getFileName(exportResponse.body.ecarFilePath),
() => {
resolve(exportResponse);
}, err => {
console.error(err);
resolve(err);
});
});
}
}
22 changes: 22 additions & 0 deletions src/content/handlers/export/deletete-temp-dir.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {Response} from '../../../api';
import {ExportContentContext} from '../..';

export class DeleteTempDir {

constructor() {
}

public async execute(exportContext: ExportContentContext): Promise<Response> {
const response: Response = new Response();
return new Promise<Response>((resolve, reject) => {
const tmpDirPath = exportContext.destinationFolder!.concat('tmp/');
buildconfigreader.rm(tmpDirPath, '', () => {
response.body = exportContext;
resolve(response);
}, (e) => {
response.body = exportContext;
resolve(response);
});
});
}
}
12 changes: 9 additions & 3 deletions src/content/handlers/export/generate-export-share-telemetry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ContentExportResponse, ExportContentContext} from '../..';
import {ContentExportResponse, ExportContentContext, ContentExportRequest} from '../..';
import {Response} from '../../../api';
import {Item, ShareDirection, ShareItemType, ShareType, TelemetryService, TelemetryShareRequest} from '../../../telemetry';
import {ContentUtil} from '../../util/content-util';
Expand All @@ -7,7 +7,7 @@ export class GenerateExportShareTelemetry {
constructor(private telemetryService: TelemetryService) {
}

execute(exportContentContext: ExportContentContext): Promise<Response> {
execute(exportContentContext: ExportContentContext, fileName: string, contentExportRequest: ContentExportRequest): Promise<Response> {
const response: Response = new Response();
const items: Item[] = [];
for (const element of exportContentContext.items!) {
Expand All @@ -28,7 +28,13 @@ export class GenerateExportShareTelemetry {
};
return this.telemetryService.share(req).toPromise()
.then(() => {
const exportResponse: ContentExportResponse = {exportedFilePath: exportContentContext.ecarFilePath!};
let exportedFilePath;
if (contentExportRequest.saveLocally) {
exportedFilePath = contentExportRequest.destinationFolder.concat(fileName);
} else {
exportedFilePath = cordova.file.externalCacheDirectory.concat(fileName);
}
const exportResponse: ContentExportResponse = {exportedFilePath: exportedFilePath};
response.body = exportResponse;
return Promise.resolve(response);
}).catch(() => {
Expand Down
19 changes: 14 additions & 5 deletions src/content/impl/content-service-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ import {SdkConfig} from '../../sdk-config';
import {DeviceInfo} from '../../util/device';
import {GetContentHeirarchyHandler} from './../handlers/get-content-heirarchy-handler';
import {catchError, map, mapTo, mergeMap, take} from 'rxjs/operators';
import { CopyToDestination } from '../handlers/export/copy-to-destination';
import { DeleteTempDir } from './../handlers/export/deletete-temp-dir';

@injectable()
export class ContentServiceImpl implements ContentService, DownloadCompleteDelegate, SdkServiceOnInitDelegate {
Expand Down Expand Up @@ -276,9 +278,9 @@ export class ContentServiceImpl implements ContentService, DownloadCompleteDeleg
contentModelsToExport: contentsInDb,
tmpLocationPath: tempLocationPath.nativeURL
};
return new CleanTempLoc(this.fileService).execute(exportContentContext);
}).then((exportResponse: Response) => {
return new CreateTempLoc(this.fileService).execute(exportResponse.body);
// return new CleanTempLoc(this.fileService).execute(exportContentContext);
// }).then((exportResponse: Response) => {
return new CreateTempLoc(this.fileService).execute(exportContentContext);
}).then((exportResponse: Response) => {
return new CreateContentExportManifest(this.dbService, exportHandler).execute(exportResponse.body);
}).then((exportResponse: Response) => {
Expand All @@ -292,9 +294,16 @@ export class ContentServiceImpl implements ContentService, DownloadCompleteDeleg
}).then((exportResponse: Response) => {
return new EcarBundle(this.fileService, this.zipService).execute(exportResponse.body);
}).then((exportResponse: Response) => {
return new DeleteTempEcar(this.fileService).execute(exportResponse.body);
return new CopyToDestination().execute(exportResponse, contentExportRequest);
// }).then((exportResponse: Response) => {
// return new DeleteTempEcar(this.fileService).execute(exportResponse.body);
}).then((exportResponse: Response) => {
return new DeleteTempDir().execute(exportResponse.body);
}).then((exportResponse: Response) => {
return new GenerateExportShareTelemetry(this.telemetryService).execute(exportResponse.body);
const fileName = ContentUtil.getExportedFileName(contentsInDb);
return new GenerateExportShareTelemetry(
this.telemetryService).execute(exportResponse.body, fileName, contentExportRequest
);
}).then((exportResponse: Response<ContentExportResponse>) => {
return exportResponse.body;
});
Expand Down

0 comments on commit ca750aa

Please sign in to comment.