MOBILE-2312 sharedfiles: Implement helper and upload handler
parent
efe4b0f66d
commit
efb47aa58e
|
@ -54,6 +54,7 @@ import { CoreLoginModule } from '../core/login/login.module';
|
|||
import { CoreMainMenuModule } from '../core/mainmenu/mainmenu.module';
|
||||
import { CoreCoursesModule } from '../core/courses/courses.module';
|
||||
import { CoreFileUploaderModule } from '../core/fileuploader/fileuploader.module';
|
||||
import { CoreSharedFilesModule } from '../core/sharedfiles/sharedfiles.module';
|
||||
|
||||
|
||||
// For translate loader. AoT requires an exported function for factories.
|
||||
|
@ -84,6 +85,7 @@ export function createTranslateLoader(http: HttpClient) {
|
|||
CoreMainMenuModule,
|
||||
CoreCoursesModule,
|
||||
CoreFileUploaderModule,
|
||||
CoreSharedFilesModule,
|
||||
CoreComponentsModule
|
||||
],
|
||||
bootstrap: [IonicApp],
|
||||
|
|
|
@ -1062,7 +1062,7 @@ export class CoreSite {
|
|||
}
|
||||
|
||||
if (alertMessage) {
|
||||
let alert = this.domUtils.showAlert('core.notice', alertMessage, undefined, 3000);
|
||||
let alert = this.domUtils.showAlert(this.translate.instant('core.notice'), alertMessage, undefined, 3000);
|
||||
alert.onDidDismiss(() => {
|
||||
if (inApp) {
|
||||
resolve(this.utils.openInApp(url, options));
|
||||
|
|
|
@ -361,7 +361,7 @@ export class CoreFileUploaderHelperProvider {
|
|||
return this.fileProvider.getFileObjectFromFileEntry(fileEntry).then((file) => {
|
||||
return this.confirmUploadFile(file.size).then(() => {
|
||||
return this.uploadGenericFile(fileEntry.toURL(), file.name, file.type, deleteAfterUpload, siteId).then(() => {
|
||||
this.domUtils.showAlert('core.success', 'core.fileuploader.fileuploaded');
|
||||
this.domUtils.showAlertTranslated('core.success', 'core.fileuploader.fileuploaded');
|
||||
});
|
||||
}).catch((err) => {
|
||||
if (err) {
|
||||
|
|
|
@ -236,7 +236,7 @@ export class CoreLoginEmailSignupPage {
|
|||
if (result.success) {
|
||||
// Show alert and ho back.
|
||||
let message = this.translate.instant('core.login.emailconfirmsent', {$a: params.email});
|
||||
this.domUtils.showAlert('core.success', message);
|
||||
this.domUtils.showAlert(this.translate.instant('core.success'), message);
|
||||
this.navCtrl.pop();
|
||||
} else {
|
||||
if (result.warnings && result.warnings.length) {
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
<ion-header>
|
||||
<ion-navbar>
|
||||
<ion-title>{{ 'core.sharedfiles.sharedfiles' | translate }}</ion-title>
|
||||
</ion-navbar>
|
||||
</ion-header>
|
||||
<ion-content>
|
||||
<core-loading [hideUntil]="loaded">
|
||||
<ion-list>
|
||||
<ion-item text-wrap>
|
||||
<p class="item-heading">{{ 'core.sharedfiles.chooseaccountstorefile' | translate }}</p>
|
||||
<p>{{fileName}}</p>
|
||||
</ion-item>
|
||||
<a ion-item *ngFor="let site of sites" (click)="storeInSite(site.id)">
|
||||
<img [src]="site.avatar" item-start>
|
||||
<h2>{{site.fullName}}</h2>
|
||||
<p><core-format-text clean="true" [text]="site.siteName"></core-format-text></p>
|
||||
<p>{{site.siteUrl}}</p>
|
||||
</a>
|
||||
</ion-list>
|
||||
</core-loading>
|
||||
</ion-content>
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
// (C) Copyright 2015 Martin Dougiamas
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
import { NgModule } from '@angular/core';
|
||||
import { IonicPageModule } from 'ionic-angular';
|
||||
import { CoreSharedFilesChooseSitePage } from './choose-site';
|
||||
import { TranslateModule } from '@ngx-translate/core';
|
||||
import { CoreComponentsModule } from '../../../../components/components.module';
|
||||
import { CoreDirectivesModule } from '../../../../directives/directives.module';
|
||||
|
||||
@NgModule({
|
||||
declarations: [
|
||||
CoreSharedFilesChooseSitePage
|
||||
],
|
||||
imports: [
|
||||
CoreComponentsModule,
|
||||
CoreDirectivesModule,
|
||||
IonicPageModule.forChild(CoreSharedFilesChooseSitePage),
|
||||
TranslateModule.forChild()
|
||||
]
|
||||
})
|
||||
export class CoreSharedFilesChooseSitePageModule {}
|
|
@ -0,0 +1,89 @@
|
|||
// (C) Copyright 2015 Martin Dougiamas
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
import { IonicPage, NavController, NavParams } from 'ionic-angular';
|
||||
import { CoreFileProvider } from '../../../../providers/file';
|
||||
import { CoreSitesProvider } from '../../../../providers/sites';
|
||||
import { CoreDomUtilsProvider } from '../../../../providers/utils/dom';
|
||||
import { CoreSharedFilesHelperProvider } from '../../providers/helper';
|
||||
|
||||
/**
|
||||
* Modal to display the list of sites to choose one to store a shared file.
|
||||
*/
|
||||
@IonicPage()
|
||||
@Component({
|
||||
selector: 'page-core-shared-files-choose-site',
|
||||
templateUrl: 'choose-site.html',
|
||||
})
|
||||
export class CoreSharedFilesChooseSitePage implements OnInit {
|
||||
|
||||
fileName: string;
|
||||
sites: any[];
|
||||
loaded: boolean;
|
||||
|
||||
protected filePath: string;
|
||||
protected fileEntry: any;
|
||||
|
||||
constructor(private navCtrl: NavController, navParams: NavParams, private sharedFilesHelper: CoreSharedFilesHelperProvider,
|
||||
private sitesProvider: CoreSitesProvider, private domUtils: CoreDomUtilsProvider,
|
||||
private fileProvider: CoreFileProvider) {
|
||||
this.filePath = navParams.get('filePath');
|
||||
}
|
||||
|
||||
/**
|
||||
* Component being initialized.
|
||||
*/
|
||||
ngOnInit() {
|
||||
if (!this.filePath) {
|
||||
this.domUtils.showErrorModal('Error reading file.');
|
||||
this.navCtrl.pop();
|
||||
return;
|
||||
}
|
||||
|
||||
let fileAndDir = this.fileProvider.getFileAndDirectoryFromPath(this.filePath);
|
||||
this.fileName = fileAndDir.name;
|
||||
|
||||
// Get the file.
|
||||
this.fileProvider.getFile(this.filePath).then((fe) => {
|
||||
this.fileEntry = fe;
|
||||
this.fileName = this.fileEntry.name;
|
||||
}).catch(() => {
|
||||
this.domUtils.showErrorModal('Error reading file.');
|
||||
this.navCtrl.pop();
|
||||
});
|
||||
|
||||
// Get the sites.
|
||||
this.sitesProvider.getSites().then((sites) => {
|
||||
this.sites = sites;
|
||||
}).finally(() => {
|
||||
this.loaded = true;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Store the file in a certain site.
|
||||
*
|
||||
* @param {string} siteId Site ID.
|
||||
*/
|
||||
storeInSite(siteId: string) : void {
|
||||
this.loaded = false;
|
||||
this.sharedFilesHelper.storeSharedFileInSite(this.fileEntry, siteId).then(() => {
|
||||
this.navCtrl.pop();
|
||||
}).finally(() => {
|
||||
this.loaded = true;
|
||||
});
|
||||
};
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
<ion-header>
|
||||
<ion-navbar>
|
||||
<ion-title><core-format-text [text]="title"></core-format-text></ion-title>
|
||||
|
||||
<ion-buttons end *ngIf="isModal">
|
||||
<button ion-button icon-only (click)="closeModal()" [attr.aria-label]="'core.close' | translate">
|
||||
<ion-icon name="close"></ion-icon>
|
||||
</button>
|
||||
</ion-buttons>
|
||||
</ion-navbar>
|
||||
</ion-header>
|
||||
<ion-content>
|
||||
<ion-refresher [enabled]="filesLoaded" (ionRefresh)="refreshFiles($event)">
|
||||
<ion-refresher-content pullingText="{{ 'core.pulltorefresh' | translate }}"></ion-refresher-content>
|
||||
</ion-refresher>
|
||||
<!-- Allow selecting the site to view. -->
|
||||
<core-site-picker [hidden]="!filesLoaded" [initialSite]="siteId" (siteSelected)="changeSite($event)"></core-site-picker>
|
||||
<ion-item-divider color="light"></ion-item-divider>
|
||||
<core-loading [hideUntil]="filesLoaded">
|
||||
<ion-list *ngIf="files && files.length > 0">
|
||||
<div *ngFor="let file of files; let idx = index">
|
||||
<core-local-file *ngIf="file.isFile" [file]="file" [manage]="manage" [overrideClick]="pick" (onClick)="filePicked(file)" (onDelete)="fileDeleted(idx)" (onRename)="fileRenamed(idx, $event)"></core-local-file>
|
||||
<a ion-item text-wrap class="item-media" *ngIf="!file.isFile" (click)="openFolder(file)">
|
||||
<img src="assets/img/files/folder-64.png" alt="{{ 'core.folder' | translate }}" role="presentation" item-start>
|
||||
<p>{{ file.name }}</p>
|
||||
</a>
|
||||
</div>
|
||||
</ion-list>
|
||||
<core-empty-box *ngIf="files && !files.length && manage" icon="folder" [message]="'core.sharedfiles.nosharedfiles' | translate"></core-empty-box>
|
||||
<core-empty-box *ngIf="files && !files.length && !manage" icon="folder" [message]="'core.sharedfiles.nosharedfilestoupload' | translate"></core-empty-box>
|
||||
</core-loading>
|
||||
</ion-content>
|
|
@ -0,0 +1,33 @@
|
|||
// (C) Copyright 2015 Martin Dougiamas
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
import { NgModule } from '@angular/core';
|
||||
import { IonicPageModule } from 'ionic-angular';
|
||||
import { CoreSharedFilesListPage } from './list';
|
||||
import { TranslateModule } from '@ngx-translate/core';
|
||||
import { CoreComponentsModule } from '../../../../components/components.module';
|
||||
import { CoreDirectivesModule } from '../../../../directives/directives.module';
|
||||
|
||||
@NgModule({
|
||||
declarations: [
|
||||
CoreSharedFilesListPage
|
||||
],
|
||||
imports: [
|
||||
CoreComponentsModule,
|
||||
CoreDirectivesModule,
|
||||
IonicPageModule.forChild(CoreSharedFilesListPage),
|
||||
TranslateModule.forChild()
|
||||
]
|
||||
})
|
||||
export class CoreSharedFilesListPageModule {}
|
|
@ -0,0 +1,181 @@
|
|||
// (C) Copyright 2015 Martin Dougiamas
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
import { Component, OnInit, OnDestroy } from '@angular/core';
|
||||
import { IonicPage, ViewController, NavParams, NavController } from 'ionic-angular';
|
||||
import { TranslateService } from '@ngx-translate/core';
|
||||
import { CoreEventsProvider } from '../../../../providers/events';
|
||||
import { CoreFileProvider } from '../../../../providers/file';
|
||||
import { CoreSitesProvider } from '../../../../providers/sites';
|
||||
import { CoreTextUtilsProvider } from '../../../../providers/utils/text';
|
||||
import { CoreSharedFilesProvider } from '../../providers/sharedfiles';
|
||||
|
||||
/**
|
||||
* Modal to display the list of shared files.
|
||||
*/
|
||||
@IonicPage()
|
||||
@Component({
|
||||
selector: 'page-core-shared-files-list',
|
||||
templateUrl: 'list.html',
|
||||
})
|
||||
export class CoreSharedFilesListPage implements OnInit, OnDestroy {
|
||||
|
||||
siteId: string;
|
||||
isModal: boolean;
|
||||
manage: boolean;
|
||||
pick: boolean; // To pick a file you MUST use a modal.
|
||||
path: string = '';
|
||||
title: string;
|
||||
filesLoaded: boolean;
|
||||
files: any[];
|
||||
|
||||
protected mimetypes: string[];
|
||||
protected shareObserver;
|
||||
|
||||
constructor(private viewCtrl: ViewController, navParams: NavParams, private sharedFilesProvider: CoreSharedFilesProvider,
|
||||
private sitesProvider: CoreSitesProvider, private textUtils: CoreTextUtilsProvider, private translate: TranslateService,
|
||||
private fileProvider: CoreFileProvider, private eventsProvider: CoreEventsProvider, private navCtrl: NavController) {
|
||||
this.siteId = navParams.get('siteId') || this.sitesProvider.getCurrentSiteId();
|
||||
this.mimetypes = navParams.get('mimetypes');
|
||||
this.isModal = !!navParams.get('isModal');
|
||||
this.manage = !!navParams.get('manage');
|
||||
this.pick = !!navParams.get('pick');
|
||||
this.path = navParams.get('path') || '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Component being initialized.
|
||||
*/
|
||||
ngOnInit() {
|
||||
this.loadFiles();
|
||||
|
||||
// Listen for new files shared with the app.
|
||||
this.shareObserver = this.eventsProvider.on(CoreEventsProvider.FILE_SHARED, (data) => {
|
||||
if (data.siteId == this.siteId) {
|
||||
// File was stored in current site, refresh the list.
|
||||
this.filesLoaded = false;
|
||||
this.loadFiles().finally(() => {
|
||||
this.filesLoaded = true;
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the files.
|
||||
*/
|
||||
protected loadFiles() {
|
||||
if (this.path) {
|
||||
this.title = this.fileProvider.getFileAndDirectoryFromPath(this.path).name;
|
||||
} else {
|
||||
this.title = this.translate.instant('core.sharedfiles.sharedfiles');
|
||||
}
|
||||
|
||||
return this.sharedFilesProvider.getSiteSharedFiles(this.siteId, this.path, this.mimetypes).then((files) => {
|
||||
this.files = files;
|
||||
this.filesLoaded = true;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Close modal.
|
||||
*/
|
||||
closeModal() : void {
|
||||
this.viewCtrl.dismiss();
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh the list of files.
|
||||
*
|
||||
* @param {any} refresher Refresher.
|
||||
*/
|
||||
refreshFiles(refresher: any) : void {
|
||||
this.loadFiles().finally(() => {
|
||||
refresher.complete();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a file is deleted. Remove the file from the list.
|
||||
*
|
||||
* @param {number} index Position of the file.
|
||||
*/
|
||||
fileDeleted(index: number) : void {
|
||||
this.files.splice(index, 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when a file is renamed. Update the list.
|
||||
*
|
||||
* @param {number} index Position of the file.
|
||||
* @param {any} file New FileEntry.
|
||||
*/
|
||||
fileRenamed(index: number, file: any) : void {
|
||||
this.files[index] = file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Open a subfolder.
|
||||
*
|
||||
* @param {any} folder The folder to open.
|
||||
*/
|
||||
openFolder(folder: any) : void {
|
||||
let path = this.textUtils.concatenatePaths(this.path, folder.name);
|
||||
if (this.isModal) {
|
||||
// In Modal we don't want to open a new page because we cannot dismiss the modal from the new page.
|
||||
this.path = path;
|
||||
this.filesLoaded = false;
|
||||
this.loadFiles();
|
||||
} else {
|
||||
this.navCtrl.push('CoreSharedFilesListPage', {
|
||||
path: path,
|
||||
manage: this.manage,
|
||||
pick: this.pick,
|
||||
siteId: this.siteId,
|
||||
mimetypes: this.mimetypes,
|
||||
isModal: this.isModal
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Change site loaded.
|
||||
*
|
||||
* @param {string} id Site to load.
|
||||
*/
|
||||
changeSite(id: string) : void {
|
||||
this.siteId = id;
|
||||
this.path = '';
|
||||
this.filesLoaded = false;
|
||||
this.loadFiles();
|
||||
}
|
||||
|
||||
/**
|
||||
* A file was picked.
|
||||
*
|
||||
* @param {any} file Picked file.
|
||||
*/
|
||||
filePicked(file: any) : void {
|
||||
this.viewCtrl.dismiss(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Component destroyed.
|
||||
*/
|
||||
ngOnDestroy() {
|
||||
if (this.shareObserver) {
|
||||
this.shareObserver.off();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,176 @@
|
|||
// (C) Copyright 2015 Martin Dougiamas
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { AlertController, ModalController } from 'ionic-angular';
|
||||
import { TranslateService } from '@ngx-translate/core';
|
||||
import { CoreAppProvider } from '../../../providers/app';
|
||||
import { CoreFileProvider } from '../../../providers/file';
|
||||
import { CoreLoggerProvider } from '../../../providers/logger';
|
||||
import { CoreInitDelegate } from '../../../providers/init';
|
||||
import { CoreSitesProvider } from '../../../providers/sites';
|
||||
import { CoreDomUtilsProvider } from '../../../providers/utils/dom';
|
||||
import { CoreUtilsProvider } from '../../../providers/utils/utils';
|
||||
import { CoreSharedFilesProvider } from './sharedfiles';
|
||||
import { CoreFileUploaderProvider } from '../../fileuploader/providers/fileuploader';
|
||||
|
||||
/**
|
||||
* Helper service to share files with the app.
|
||||
*/
|
||||
@Injectable()
|
||||
export class CoreSharedFilesHelperProvider {
|
||||
protected logger;
|
||||
|
||||
constructor(logger: CoreLoggerProvider, private alertCtrl: AlertController, private translate: TranslateService,
|
||||
private utils: CoreUtilsProvider, private sitesProvider: CoreSitesProvider, private modalCtrl: ModalController,
|
||||
private fileUploaderProvider: CoreFileUploaderProvider, private initDelegate: CoreInitDelegate,
|
||||
private sharedFilesProvider: CoreSharedFilesProvider, private domUtils: CoreDomUtilsProvider,
|
||||
private fileProvider: CoreFileProvider, private appProvider: CoreAppProvider) {
|
||||
this.logger = logger.getInstance('CoreSharedFilesHelperProvider');
|
||||
}
|
||||
|
||||
/**
|
||||
* Ask a user if he wants to replace a file (using originalName) or rename it (using newName).
|
||||
*
|
||||
* @param {string} originalName Original name.
|
||||
* @param {string} newName New name.
|
||||
* @return {Promise<string>} Promise resolved with the name to use when the user chooses. Rejected if user cancels.
|
||||
*/
|
||||
askRenameReplace(originalName: string, newName: string) : Promise<string> {
|
||||
const deferred = this.utils.promiseDefer(),
|
||||
alert = this.alertCtrl.create({
|
||||
title: this.translate.instant('core.sharedfiles.sharedfiles'),
|
||||
message: this.translate.instant('core.sharedfiles.chooseactionrepeatedfile', {$a: newName}),
|
||||
buttons: [
|
||||
{
|
||||
text: this.translate.instant('core.sharedfiles.rename'),
|
||||
handler: () => {
|
||||
deferred.resolve(newName);
|
||||
}
|
||||
},
|
||||
{
|
||||
text: this.translate.instant('core.sharedfiles.replace'),
|
||||
handler: () => {
|
||||
deferred.resolve(originalName);
|
||||
}
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
alert.present();
|
||||
return deferred.promise;
|
||||
}
|
||||
|
||||
/**
|
||||
* Go to the choose site view.
|
||||
*
|
||||
* @param {string} filePath File path to send to the view.
|
||||
*/
|
||||
goToChooseSite(filePath: string) : void {
|
||||
let navCtrl = this.appProvider.getRootNavController();
|
||||
navCtrl.push('CoreSharedFilesChooseSitePage', {filePath: filePath});
|
||||
}
|
||||
|
||||
/**
|
||||
* Open the view to select a shared file.
|
||||
*
|
||||
* @param {string[]} [mimetypes] List of supported mimetypes. If undefined, all mimetypes supported.
|
||||
* @return {Promise<any>} Promise resolved when a file is picked, rejected if file picker is closed without selecting a file.
|
||||
*/
|
||||
pickSharedFile(mimetypes?: string[]) : Promise<any> {
|
||||
return new Promise((resolve, reject) => {
|
||||
let modal = this.modalCtrl.create('CoreSharedFilesListPage', {mimetypes: mimetypes, isModal: true, pick: true});
|
||||
modal.present();
|
||||
|
||||
modal.onDidDismiss((file: any) => {
|
||||
if (!file) {
|
||||
// User cancelled.
|
||||
reject();
|
||||
return;
|
||||
}
|
||||
|
||||
const error = this.fileUploaderProvider.isInvalidMimetype(mimetypes, file.fullPath);
|
||||
if (error) {
|
||||
reject(error);
|
||||
} else {
|
||||
resolve({
|
||||
path: file.fullPath,
|
||||
treated: false
|
||||
});
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if there is a new file received in iOS and move it to the shared folder of current site.
|
||||
* If more than one site is found, the user will have to choose the site where to store it in.
|
||||
* If more than one file is found, treat only the first one.
|
||||
*
|
||||
* @return {Promise<any>} Promise resolved when done.
|
||||
*/
|
||||
searchIOSNewSharedFiles() : Promise<any> {
|
||||
return this.initDelegate.ready().then(() => {
|
||||
let navCtrl = this.appProvider.getRootNavController();
|
||||
if (navCtrl && navCtrl.getActive().id == 'CoreSharedFilesChooseSite') {
|
||||
// We're already treating a shared file. Abort.
|
||||
return Promise.reject(null);
|
||||
}
|
||||
|
||||
return this.sharedFilesProvider.checkIOSNewFiles().then((fileEntry) => {
|
||||
return this.sitesProvider.getSitesIds().then((siteIds) => {
|
||||
if (!siteIds.length) {
|
||||
// No sites stored, show error and delete the file.
|
||||
this.domUtils.showErrorModal('core.sharedfiles.errorreceivefilenosites', true);
|
||||
return this.sharedFilesProvider.deleteInboxFile(fileEntry);
|
||||
} else if (siteIds.length == 1) {
|
||||
return this.storeSharedFileInSite(fileEntry, siteIds[0]);
|
||||
} else {
|
||||
this.goToChooseSite(fileEntry.fullPath);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a shared file in a site's shared files folder.
|
||||
*
|
||||
* @param {any} fileEntry Shared file entry.
|
||||
* @param {string} [siteId] Site ID. If not defined, current site.
|
||||
* @return {Promise<any>} Promise resolved when done.
|
||||
*/
|
||||
storeSharedFileInSite(fileEntry: any, siteId?: string) : Promise<any> {
|
||||
siteId = siteId || this.sitesProvider.getCurrentSiteId();
|
||||
|
||||
// First of all check if there's already a file with the same name in the shared files folder.
|
||||
const sharedFilesDirPath = this.sharedFilesProvider.getSiteSharedFilesDirPath(siteId);
|
||||
return this.fileProvider.getUniqueNameInFolder(sharedFilesDirPath, fileEntry.name).then((newName) => {
|
||||
if (newName == fileEntry.name) {
|
||||
// No file with the same name. Use the original file name.
|
||||
return newName;
|
||||
} else {
|
||||
// Repeated name. Ask the user what he wants to do.
|
||||
return this.askRenameReplace(fileEntry.name, newName);
|
||||
}
|
||||
}).then((name) => {
|
||||
return this.sharedFilesProvider.storeFileInSite(fileEntry, name, siteId).catch(function(err) {
|
||||
this.domUtils.showErrorModal(err || 'Error moving file.');
|
||||
}).finally(() => {
|
||||
this.sharedFilesProvider.deleteInboxFile(fileEntry);
|
||||
this.domUtils.showAlertTranslated('core.success', 'core.sharedfiles.successstorefile');
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
|
@ -225,7 +225,7 @@ export class CoreSharedFilesProvider {
|
|||
// Create dir if it doesn't exist already.
|
||||
return this.fileProvider.createDir(sharedFilesFolder).then(() => {
|
||||
return this.fileProvider.moveFile(entry.fullPath, newPath).then((newFile) => {
|
||||
this.eventsProvider.trigger(CoreEventsProvider.FILE_SHARED, {siteid: siteId, name: newName});
|
||||
this.eventsProvider.trigger(CoreEventsProvider.FILE_SHARED, {siteId: siteId, name: newName});
|
||||
return newFile;
|
||||
});
|
||||
});
|
||||
|
|
|
@ -0,0 +1,64 @@
|
|||
// (C) Copyright 2015 Martin Dougiamas
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
import { Injectable } from '@angular/core';
|
||||
import { Platform } from 'ionic-angular';
|
||||
import { CoreFileUploaderHandler, CoreFileUploaderHandlerData } from '../../fileuploader/providers/delegate';
|
||||
import { CoreSharedFilesHelperProvider } from './helper';
|
||||
/**
|
||||
* Handler to upload files from the album.
|
||||
*/
|
||||
@Injectable()
|
||||
export class CoreSharedFilesUploadHandler implements CoreFileUploaderHandler {
|
||||
name = 'CoreSharedFilesUpload';
|
||||
priority = 1300;
|
||||
|
||||
constructor(private sharedFilesHelper: CoreSharedFilesHelperProvider, private platform: Platform) {}
|
||||
|
||||
/**
|
||||
* Whether or not the handler is enabled on a site level.
|
||||
*
|
||||
* @return {boolean|Promise<boolean>} True or promise resolved with true if enabled.
|
||||
*/
|
||||
isEnabled(): boolean|Promise<boolean> {
|
||||
return this.platform.is('ios');
|
||||
}
|
||||
|
||||
/**
|
||||
* Given a list of mimetypes, return the ones that are supported by the handler.
|
||||
*
|
||||
* @param {string[]} [mimetypes] List of mimetypes.
|
||||
* @return {string[]} Supported mimetypes.
|
||||
*/
|
||||
getSupportedMimetypes(mimetypes: string[]) : string[] {
|
||||
return mimetypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the data to display the handler.
|
||||
*
|
||||
* @return {CoreFileUploaderHandlerData} Data.
|
||||
*/
|
||||
getData() : CoreFileUploaderHandlerData {
|
||||
return {
|
||||
title: 'core.sharedfiles.sharedfiles',
|
||||
class: 'core-sharedfiles-fileuploader-handler',
|
||||
icon: 'folder',
|
||||
action: (maxSize?: number, upload?: boolean, allowOffline?: boolean, mimetypes?: string[]) => {
|
||||
// Don't use the params because the file won't be uploaded, it is returned to the fileuploader.
|
||||
return this.sharedFilesHelper.pickSharedFile(mimetypes);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -13,7 +13,11 @@
|
|||
// limitations under the License.
|
||||
|
||||
import { NgModule } from '@angular/core';
|
||||
import { Platform } from 'ionic-angular';
|
||||
import { CoreSharedFilesProvider } from './providers/sharedfiles';
|
||||
import { CoreSharedFilesHelperProvider } from './providers/helper';
|
||||
import { CoreSharedFilesUploadHandler } from './providers/upload-handler';
|
||||
import { CoreFileUploaderDelegate } from '../fileuploader/providers/delegate';
|
||||
|
||||
@NgModule({
|
||||
declarations: [
|
||||
|
@ -21,7 +25,23 @@ import { CoreSharedFilesProvider } from './providers/sharedfiles';
|
|||
imports: [
|
||||
],
|
||||
providers: [
|
||||
CoreSharedFilesProvider
|
||||
CoreSharedFilesProvider,
|
||||
CoreSharedFilesHelperProvider,
|
||||
CoreSharedFilesUploadHandler
|
||||
]
|
||||
})
|
||||
export class CoreSharedFilesModule {}
|
||||
export class CoreSharedFilesModule {
|
||||
constructor(platform: Platform, delegate: CoreFileUploaderDelegate, handler: CoreSharedFilesUploadHandler,
|
||||
helper: CoreSharedFilesHelperProvider) {
|
||||
// Register the handler.
|
||||
delegate.registerHandler(handler);
|
||||
|
||||
if (platform.is('ios')) {
|
||||
// Check if there are new files at app start and when the app is resumed.
|
||||
helper.searchIOSNewSharedFiles();
|
||||
platform.resume.subscribe(() => {
|
||||
helper.searchIOSNewSharedFiles();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue