Skip to content

Commit

Permalink
Merge pull request #6 from adlius/fix-file-upload
Browse files Browse the repository at this point in the history
Fix file upload
  • Loading branch information
bp-cos authored May 17, 2024
2 parents e57433d + a2ae236 commit 2e2bec0
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 102 deletions.
2 changes: 1 addition & 1 deletion app/models/file-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default class FileProviderModel extends BaseFileItem {
@hasMany('file', { inverse:'parentFolder', polymorphic: true })
files!: AsyncHasMany<FileModel>;

@belongsTo('osf-model', { inverse: 'files', polymorphic: true })
@belongsTo('abstract-node', { inverse: 'files', polymorphic: true })
target!: (AsyncBelongsTo<AbstractNodeModel> & AbstractNodeModel) |
(AsyncBelongsTo<DraftNodeModel> & DraftNodeModel) |
(AsyncBelongsTo<PreprintModel> & PreprintModel);
Expand Down
5 changes: 3 additions & 2 deletions app/models/preprint.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { attr, belongsTo, hasMany, AsyncBelongsTo, AsyncHasMany } from '@ember-data/model';
import { computed } from '@ember/object';
import { alias } from '@ember/object/computed';
import AbstractNodeModel from 'ember-osf-web/models/abstract-node';
import CitationModel from 'ember-osf-web/models/citation';
import FileProviderModel from 'ember-osf-web/models/file-provider';
import PreprintRequestModel from 'ember-osf-web/models/preprint-request';
Expand All @@ -12,7 +13,7 @@ import FileModel from './file';
import IdentifierModel from './identifier';
import LicenseModel from './license';
import NodeModel from './node';
import OsfModel, { Permission } from './osf-model';
import { Permission } from './osf-model';
import PreprintProviderModel from './preprint-provider';
import SubjectModel from './subject';

Expand All @@ -37,7 +38,7 @@ export enum PreprintPreregLinkInfoEnum {
PREREG_BOTH = 'prereg_both',
}

export default class PreprintModel extends OsfModel {
export default class PreprintModel extends AbstractNodeModel {
@attr('fixstring') title!: string;
@attr('date') dateCreated!: Date;
@attr('date') datePublished!: Date;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export default class PreprintStateMachine extends Component<StateMachineArgs>{
this.preprint = this.store.createRecord('preprint', {
provider: this.provider,
});
this.preprint.save();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { action, notifyPropertyChange } from '@ember/object';
import { action } from '@ember/object';
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { inject as service } from '@ember/service';
import Intl from 'ember-intl/services/intl';
import Toast from 'ember-toastr/services/toast';
import { TrackedWeakMap } from 'tracked-built-ins';
import PreprintModel from 'ember-osf-web/models/preprint';
import PreprintStateMachine from 'ember-osf-web/preprints/-components/submit/preprint-state-machine/component';
import { task } from 'ember-concurrency';
import { waitFor } from '@ember/test-waiters';

interface PreprintUploadArgs {
manager: PreprintStateMachine;
Expand All @@ -22,12 +22,6 @@ interface PreprintUploadArgs {
export default class PreprintUpload extends Component<PreprintUploadArgs> {
@service intl!: Intl;
@service toast!: Toast;
@tracked uploading: any[] = [];
@tracked uploadCompleted: any[] = [];
@tracked uploadErrored: any[] = [];
@tracked uploadConflicted: any[] = [];

uploadProgress = new TrackedWeakMap();

get clickableElementSelectors() {
if (this.args.clickableElementId) {
Expand All @@ -52,94 +46,19 @@ export default class PreprintUpload extends Component<PreprintUploadArgs> {
};
}


@action
updateUploadProgress(_: any, __: any, file: any, progress: any) {
this.uploadProgress.set(file, progress);
}

@action
buildUrl(files: any[]) {
const { name, newUploadLink } = files[0];
if (newUploadLink) {
return newUploadLink;
}

const url = new URL(this.args.manager.preprint.files[0].upload as string);

const { name } = files[0];
const url = new URL(this.args.manager.preprint.files.firstObject!.links.upload as string);
url.searchParams.append('kind', 'file');
url.searchParams.append('name', name);
return url.toString();
}

@action
addedFile(_: any, __: any, file: any) {
const cache = new TrackedWeakMap();
const initialValue = file.status;
Object.defineProperty(file, 'status', {
get() {
let existingValue = cache.get(file);
if (!existingValue) {
existingValue = initialValue;
cache.set(file, existingValue);
}
return existingValue;
},
set(value) {
cache.set(file, value);
},
});
this.uploadProgress.set(file, 0);
if (!this.uploadErrored.includes(file)
&& !this.uploading.includes(file)
&& !this.uploadConflicted.includes(file)
) {
this.uploading.pushObject(file);
}
notifyPropertyChange(this, 'uploading');
}

@action
error(_: any, dropzoneInstance: any, file: any) {
const { xhr: { status, responseText } } = file;
if (status === 409) {
const { data: { links: { upload } } } = JSON.parse(responseText);
file.newUploadLink = upload;
if (this.args.allowVersioning) {
dropzoneInstance.processFile(file);
return;
}
this.uploading.removeObject(file);
this.uploadConflicted.pushObject(file);
} else {
this.uploading.removeObject(file);
if (!this.uploadErrored.includes(file)) {
this.uploadErrored.pushObject(file);
}
}
this.uploadProgress.set(file, 0);
notifyPropertyChange(this, 'uploading');
notifyPropertyChange(this, 'uploadErrored');
}

@action
success(_: any, __: any, file: any, ___: any) {
this.uploading.removeObject(file);
if (this.uploadErrored.includes(file)) {
this.uploadErrored.removeObject(file);
}
if (this.uploadConflicted.includes(file)) {
this.uploadConflicted.removeObject(file);
}
this.uploadCompleted.pushObject(file);
notifyPropertyChange(this, 'uploading');
notifyPropertyChange(this, 'uploadCompleted');
notifyPropertyChange(this, 'uploadConflicted');
}


@action
setClickableElementId(id: string) {
this.args.clickableElementId = id;
@task
@waitFor
async createPreprint() {
await this.args.preprint.save();
await this.args.preprint.files;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,16 @@
@buildUrl={{this.buildUrl}}
@options={{this.dropzoneOptions}}
@dropzone={{false}}
@addedfile={{action this.addedFile}}
@dragenter={{@dragEnter}}
@dragleave={{@dragLeave}}
@dragover={{@dragOver}}
@drop={{@drop}}
@success={{action this.success}}
@error={{action this.error}}
@enable={{@enable}}
@uploadprogress={{this.updateUploadProgress}}
@id={{id}}
@clickable={{this.clickableElementSelectors}}
@preUpload={{perform this.createPreprint}}
>
{{yield (hash
setClickableElementId=this.setClickableElementId
)}}
{{yield}}
</DropzoneWidget>
{{/let}}
</div>

0 comments on commit 2e2bec0

Please sign in to comment.