summaryrefslogtreecommitdiff
path: root/indra/newview/llviewerassetupload.cpp
blob: fbf3311ce4c26d5eb2c51358178437ff08ffca08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
/**
* @file llviewerassetupload.cpp
* @author optional
* @brief brief description of the file
*
* $LicenseInfo:firstyear=2011&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2011, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
* $/LicenseInfo$
*/

#include "llviewerprecompiledheaders.h"

#include "linden_common.h"
#include "llviewertexturelist.h"
#include "llimage.h"
#include "lltrans.h"
#include "lluuid.h"
#include "llvorbisencode.h"
#include "lluploaddialog.h"
#include "llpreviewscript.h"
#include "llnotificationsutil.h"
#include "lleconomy.h"
#include "llagent.h"
#include "llfloaterreg.h"
#include "llstatusbar.h"
#include "llinventorypanel.h"
#include "llsdutil.h"
#include "llviewerassetupload.h"
#include "llappviewer.h"
#include "llviewerstats.h"
#include "llvfile.h"
#include "llgesturemgr.h"
#include "llpreviewnotecard.h"
#include "llpreviewgesture.h"
#include "llcoproceduremanager.h"

void dialog_refresh_all();

static const U32 LL_ASSET_UPLOAD_TIMEOUT_SEC = 60;

LLResourceUploadInfo::LLResourceUploadInfo(LLTransactionID transactId,
        LLAssetType::EType assetType, std::string name, std::string description,
        S32 compressionInfo, LLFolderType::EType destinationType,
        LLInventoryType::EType inventoryType, U32 nextOWnerPerms,
        U32 groupPerms, U32 everyonePerms, S32 expectedCost) :
    mTransactionId(transactId),
    mAssetType(assetType),
    mName(name),
    mDescription(description),
    mCompressionInfo(compressionInfo),
    mDestinationFolderType(destinationType),
    mInventoryType(inventoryType),
    mNextOwnerPerms(nextOWnerPerms),
    mGroupPerms(groupPerms),
    mEveryonePerms(everyonePerms),
    mExpectedUploadCost(expectedCost),
    mFolderId(LLUUID::null),
    mItemId(LLUUID::null),
    mAssetId(LLAssetID::null)
{ }


LLResourceUploadInfo::LLResourceUploadInfo(std::string name, 
        std::string description, S32 compressionInfo, 
        LLFolderType::EType destinationType, LLInventoryType::EType inventoryType, 
        U32 nextOWnerPerms, U32 groupPerms, U32 everyonePerms, S32 expectedCost):
    mName(name),
    mDescription(description),
    mCompressionInfo(compressionInfo),
    mDestinationFolderType(destinationType),
    mInventoryType(inventoryType),
    mNextOwnerPerms(nextOWnerPerms),
    mGroupPerms(groupPerms),
    mEveryonePerms(everyonePerms),
    mExpectedUploadCost(expectedCost),
    mTransactionId(),
    mAssetType(LLAssetType::AT_NONE),
    mFolderId(LLUUID::null),
    mItemId(LLUUID::null),
    mAssetId(LLAssetID::null)
{ 
    mTransactionId.generate();
}

LLResourceUploadInfo::LLResourceUploadInfo(LLAssetID assetId, LLAssetType::EType assetType, std::string name) :
    mAssetId(assetId),
    mAssetType(assetType),
    mName(name),
    mDescription(),
    mCompressionInfo(0),
    mDestinationFolderType(LLFolderType::FT_NONE),
    mInventoryType(LLInventoryType::IT_NONE),
    mNextOwnerPerms(0),
    mGroupPerms(0),
    mEveryonePerms(0),
    mExpectedUploadCost(0),
    mTransactionId(),
    mFolderId(LLUUID::null),
    mItemId(LLUUID::null)
{
}

LLSD LLResourceUploadInfo::prepareUpload()
{
    if (mAssetId.isNull())
        generateNewAssetId();

    incrementUploadStats();
    assignDefaults();

    return LLSD().with("success", LLSD::Boolean(true));
}

std::string LLResourceUploadInfo::getAssetTypeString() const
{
    return LLAssetType::lookup(mAssetType);
}

std::string LLResourceUploadInfo::getInventoryTypeString() const
{
    return LLInventoryType::lookup(mInventoryType);
}

LLSD LLResourceUploadInfo::generatePostBody()
{
    LLSD body;

    body["folder_id"] = mFolderId;
    body["asset_type"] = getAssetTypeString();
    body["inventory_type"] = getInventoryTypeString();
    body["name"] = mName;
    body["description"] = mDescription;
    body["next_owner_mask"] = LLSD::Integer(mNextOwnerPerms);
    body["group_mask"] = LLSD::Integer(mGroupPerms);
    body["everyone_mask"] = LLSD::Integer(mEveryonePerms);

    return body;

}

void LLResourceUploadInfo::logPreparedUpload()
{
    LL_INFOS() << "*** Uploading: " << std::endl <<
        "Type: " << LLAssetType::lookup(mAssetType) << std::endl <<
        "UUID: " << mAssetId.asString() << std::endl <<
        "Name: " << mName << std::endl <<
        "Desc: " << mDescription << std::endl <<
        "Expected Upload Cost: " << mExpectedUploadCost << std::endl <<
        "Folder: " << mFolderId << std::endl <<
        "Asset Type: " << LLAssetType::lookup(mAssetType) << LL_ENDL;
}

S32 LLResourceUploadInfo::getEconomyUploadCost()
{
    // Update L$ and ownership credit information
    // since it probably changed on the server
    if (getAssetType() == LLAssetType::AT_TEXTURE ||
        getAssetType() == LLAssetType::AT_SOUND ||
        getAssetType() == LLAssetType::AT_ANIMATION ||
        getAssetType() == LLAssetType::AT_MESH)
    {
        return LLGlobalEconomy::instance().getPriceUpload();
    }

    return 0;
}


LLUUID LLResourceUploadInfo::finishUpload(LLSD &result)
{
    if (getFolderId().isNull())
    {
        return LLUUID::null;
    }

    U32 permsEveryone = PERM_NONE;
    U32 permsGroup = PERM_NONE;
    U32 permsNextOwner = PERM_ALL;

    if (result.has("new_next_owner_mask"))
    {
        // The server provided creation perms so use them.
        // Do not assume we got the perms we asked for in
        // since the server may not have granted them all.
        permsEveryone = result["new_everyone_mask"].asInteger();
        permsGroup = result["new_group_mask"].asInteger();
        permsNextOwner = result["new_next_owner_mask"].asInteger();
    }
    else
    {
        // The server doesn't provide creation perms
        // so use old assumption-based perms.
        if (getAssetTypeString() != "snapshot")
        {
            permsNextOwner = PERM_MOVE | PERM_TRANSFER;
        }
    }

    LLPermissions new_perms;
    new_perms.init(
        gAgent.getID(),
        gAgent.getID(),
        LLUUID::null,
        LLUUID::null);

    new_perms.initMasks(
        PERM_ALL,
        PERM_ALL,
        permsEveryone,
        permsGroup,
        permsNextOwner);

    U32 flagsInventoryItem = 0;
    if (result.has("inventory_flags"))
    {
        flagsInventoryItem = static_cast<U32>(result["inventory_flags"].asInteger());
        if (flagsInventoryItem != 0)
        {
            LL_INFOS() << "inventory_item_flags " << flagsInventoryItem << LL_ENDL;
        }
    }
    S32 creationDate = time_corrected();

    LLUUID serverInventoryItem = result["new_inventory_item"].asUUID();
    LLUUID serverAssetId = result["new_asset"].asUUID();

    LLPointer<LLViewerInventoryItem> item = new LLViewerInventoryItem(
        serverInventoryItem,
        getFolderId(),
        new_perms,
        serverAssetId,
        getAssetType(),
        getInventoryType(),
        getName(),
        getDescription(),
        LLSaleInfo::DEFAULT,
        flagsInventoryItem,
        creationDate);

    gInventory.updateItem(item);
    gInventory.notifyObservers();

    return serverInventoryItem;
}


LLAssetID LLResourceUploadInfo::generateNewAssetId()
{
    if (gDisconnected)
    {
        LLAssetID rv;

        rv.setNull();
        return rv;
    }
    mAssetId = mTransactionId.makeAssetID(gAgent.getSecureSessionID());

    return mAssetId;
}

void LLResourceUploadInfo::incrementUploadStats() const
{
    if (LLAssetType::AT_SOUND == mAssetType)
    {
        add(LLStatViewer::UPLOAD_SOUND, 1);
    }
    else if (LLAssetType::AT_TEXTURE == mAssetType)
    {
        add(LLStatViewer::UPLOAD_TEXTURE, 1);
    }
    else if (LLAssetType::AT_ANIMATION == mAssetType)
    {
        add(LLStatViewer::ANIMATION_UPLOADS, 1);
    }
}

void LLResourceUploadInfo::assignDefaults()
{
    if (LLInventoryType::IT_NONE == mInventoryType)
    {
        mInventoryType = LLInventoryType::defaultForAssetType(mAssetType);
    }
    LLStringUtil::stripNonprintable(mName);
    LLStringUtil::stripNonprintable(mDescription);

    if (mName.empty())
    {
        mName = "(No Name)";
    }
    if (mDescription.empty())
    {
        mDescription = "(No Description)";
    }

    mFolderId = gInventory.findUserDefinedCategoryUUIDForType(
        (mDestinationFolderType == LLFolderType::FT_NONE) ?
        (LLFolderType::EType)mAssetType : mDestinationFolderType);
}

std::string LLResourceUploadInfo::getDisplayName() const
{
    return (mName.empty()) ? mAssetId.asString() : mName;
};

//=========================================================================
LLNewFileResourceUploadInfo::LLNewFileResourceUploadInfo(
    std::string fileName,
    std::string name,
    std::string description,
    S32 compressionInfo,
    LLFolderType::EType destinationType,
    LLInventoryType::EType inventoryType,
    U32 nextOWnerPerms,
    U32 groupPerms,
    U32 everyonePerms,
    S32 expectedCost) :
    LLResourceUploadInfo(name, description, compressionInfo,
    destinationType, inventoryType,
    nextOWnerPerms, groupPerms, everyonePerms, expectedCost),
    mFileName(fileName)
{
}

LLSD LLNewFileResourceUploadInfo::prepareUpload()
{
    if (getAssetId().isNull())
        generateNewAssetId();

    LLSD result = exportTempFile();
    if (result.has("error"))
        return result;

    return LLResourceUploadInfo::prepareUpload();
}

LLSD LLNewFileResourceUploadInfo::exportTempFile()
{
    std::string filename = gDirUtilp->getTempFilename();

    std::string exten = gDirUtilp->getExtension(getFileName());
    U32 codec = LLImageBase::getCodecFromExtension(exten);

    LLAssetType::EType assetType = LLAssetType::AT_NONE;
    std::string errorMessage;
    std::string errorLabel;

    bool error = false;

    if (exten.empty())
    {
        std::string shortName = gDirUtilp->getBaseFileName(filename);

        // No extension
        errorMessage = llformat(
            "No file extension for the file: '%s'\nPlease make sure the file has a correct file extension",
            shortName.c_str());
        errorLabel = "NoFileExtension";
        error = true;
    }
    else if (codec != IMG_CODEC_INVALID)
    {
        // It's an image file, the upload procedure is the same for all
        assetType = LLAssetType::AT_TEXTURE;
        if (!LLViewerTextureList::createUploadFile(getFileName(), filename, codec))
        {
            errorMessage = llformat("Problem with file %s:\n\n%s\n",
                getFileName().c_str(), LLImage::getLastError().c_str());
            errorLabel = "ProblemWithFile";
            error = true;
        }
    }
    else if (exten == "wav")
    {
        assetType = LLAssetType::AT_SOUND;  // tag it as audio
        S32 encodeResult = 0;

        LL_INFOS() << "Attempting to encode wav as an ogg file" << LL_ENDL;

        encodeResult = encode_vorbis_file(getFileName(), filename);

        if (LLVORBISENC_NOERR != encodeResult)
        {
            switch (encodeResult)
            {
            case LLVORBISENC_DEST_OPEN_ERR:
                errorMessage = llformat("Couldn't open temporary compressed sound file for writing: %s\n", filename.c_str());
                errorLabel = "CannotOpenTemporarySoundFile";
                break;

            default:
                errorMessage = llformat("Unknown vorbis encode failure on: %s\n", getFileName().c_str());
                errorLabel = "UnknownVorbisEncodeFailure";
                break;
            }
            error = true;
        }
    }
    else if (exten == "bvh")
    {
        errorMessage = llformat("We do not currently support bulk upload of animation files\n");
        errorLabel = "DoNotSupportBulkAnimationUpload";
        error = true;
    }
    else if (exten == "anim")
    {
        assetType = LLAssetType::AT_ANIMATION;
        filename = getFileName();
    }
    else
    {
        // Unknown extension
        errorMessage = llformat(LLTrans::getString("UnknownFileExtension").c_str(), exten.c_str());
        errorLabel = "ErrorMessage";
        error = TRUE;;
    }

    if (error)
    {
        LLSD errorResult(LLSD::emptyMap());

        errorResult["error"] = LLSD::Binary(true);
        errorResult["message"] = errorMessage;
        errorResult["label"] = errorLabel;
        return errorResult;
    }

    setAssetType(assetType);

    // copy this file into the vfs for upload
    S32 file_size;
    LLAPRFile infile;
    infile.open(filename, LL_APR_RB, NULL, &file_size);
    if (infile.getFileHandle())
    {
        LLVFile file(gVFS, getAssetId(), assetType, LLVFile::WRITE);

        file.setMaxSize(file_size);

        const S32 buf_size = 65536;
        U8 copy_buf[buf_size];
        while ((file_size = infile.read(copy_buf, buf_size)))
        {
            file.write(copy_buf, file_size);
        }
    }
    else
    {
        errorMessage = llformat("Unable to access output file: %s", filename.c_str());
        LLSD errorResult(LLSD::emptyMap());

        errorResult["error"] = LLSD::Binary(true);
        errorResult["message"] = errorMessage;
        return errorResult;
    }

    return LLSD();

}

//=========================================================================
LLBufferedAssetUploadInfo::LLBufferedAssetUploadInfo(LLUUID itemId, LLAssetType::EType assetType, std::string buffer, invnUploadFinish_f finish) :
    LLResourceUploadInfo(std::string(), std::string(), 0, LLFolderType::FT_NONE, LLInventoryType::IT_NONE,
        0, 0, 0, 0),
    mTaskUpload(false),
    mTaskId(LLUUID::null),
    mContents(buffer),
    mInvnFinishFn(finish),
    mTaskFinishFn(NULL),
    mStoredToVFS(false)
{
    setItemId(itemId);
    setAssetType(assetType);
}

LLBufferedAssetUploadInfo::LLBufferedAssetUploadInfo(LLUUID itemId, LLPointer<LLImageFormatted> image, invnUploadFinish_f finish) :
    LLResourceUploadInfo(std::string(), std::string(), 0, LLFolderType::FT_NONE, LLInventoryType::IT_NONE,
        0, 0, 0, 0),
    mTaskUpload(false),
    mTaskId(LLUUID::null),
    mContents(),
    mInvnFinishFn(finish),
    mTaskFinishFn(NULL),
    mStoredToVFS(false)
{
    setItemId(itemId);

    EImageCodec codec = static_cast<EImageCodec>(image->getCodec());

    switch (codec)
    {
    case IMG_CODEC_JPEG:
        setAssetType(LLAssetType::AT_IMAGE_JPEG);
        LL_INFOS() << "Upload Asset type set to JPEG." << LL_ENDL;
        break;
    case IMG_CODEC_TGA:
        setAssetType(LLAssetType::AT_IMAGE_TGA);
        LL_INFOS() << "Upload Asset type set to TGA." << LL_ENDL;
        break;
    default:
        LL_WARNS() << "Unknown codec to asset type transition. Codec=" << (int)codec << "." << LL_ENDL;
        break;
    }

    size_t imageSize = image->getDataSize();
    mContents.reserve(imageSize);
    mContents.assign((char *)image->getData(), imageSize);
}

LLBufferedAssetUploadInfo::LLBufferedAssetUploadInfo(LLUUID taskId, LLUUID itemId, LLAssetType::EType assetType, std::string buffer, taskUploadFinish_f finish) :
    LLResourceUploadInfo(std::string(), std::string(), 0, LLFolderType::FT_NONE, LLInventoryType::IT_NONE,
        0, 0, 0, 0),
    mTaskUpload(true),
    mTaskId(taskId),
    mContents(buffer),
    mInvnFinishFn(NULL),
    mTaskFinishFn(finish),
    mStoredToVFS(false)
{
    setItemId(itemId);
    setAssetType(assetType);
}

LLSD LLBufferedAssetUploadInfo::prepareUpload()
{
    if (getAssetId().isNull())
        generateNewAssetId();

    LLVFile file(gVFS, getAssetId(), getAssetType(), LLVFile::APPEND);

    S32 size = mContents.length() + 1;
    file.setMaxSize(size);
    file.write((U8*)mContents.c_str(), size);

    mStoredToVFS = true;

    return LLSD().with("success", LLSD::Boolean(true));
}

LLSD LLBufferedAssetUploadInfo::generatePostBody()
{
    LLSD body;

    if (!getTaskId().isNull())
    {
        body["task_id"] = getTaskId();
    }
    body["item_id"] = getItemId();

    return body;
}

LLUUID LLBufferedAssetUploadInfo::finishUpload(LLSD &result)
{
    LLUUID newAssetId = result["new_asset"].asUUID();
    LLUUID itemId = getItemId();

    if (mStoredToVFS)
    {
        LLAssetType::EType assetType(getAssetType());
        gVFS->renameFile(getAssetId(), assetType, newAssetId, assetType);
    }

    if (mTaskUpload)
    {
        LLUUID taskId = getTaskId();

        dialog_refresh_all();

        if (mTaskFinishFn)
        {
            mTaskFinishFn(itemId, taskId, newAssetId, result);
        }
    }
    else
    {
        LLUUID newItemId(LLUUID::null);

        if (itemId.notNull())
        {
            LLViewerInventoryItem* item = (LLViewerInventoryItem*)gInventory.getItem(itemId);
            if (!item)
            {
                LL_WARNS() << "Inventory item for " << getDisplayName() << " is no longer in agent inventory." << LL_ENDL;
                return newAssetId;
            }

            // Update viewer inventory item
            LLPointer<LLViewerInventoryItem> newItem = new LLViewerInventoryItem(item);
            newItem->setAssetUUID(newAssetId);

            gInventory.updateItem(newItem);

            newItemId = newItem->getUUID();
            LL_INFOS() << "Inventory item " << item->getName() << " saved into " << newAssetId.asString() << LL_ENDL;
        }

        if (mInvnFinishFn)
        {
            mInvnFinishFn(itemId, newAssetId, newItemId, result);
        }
        gInventory.notifyObservers();
    }

    return newAssetId;
}

//=========================================================================

LLScriptAssetUpload::LLScriptAssetUpload(LLUUID itemId, std::string buffer, invnUploadFinish_f finish):
    LLBufferedAssetUploadInfo(itemId, LLAssetType::AT_LSL_TEXT, buffer, finish),
    mExerienceId(),
    mTargetType(LSL2),
    mIsRunning(false)
{
}

LLScriptAssetUpload::LLScriptAssetUpload(LLUUID taskId, LLUUID itemId, TargetType_t targetType,
        bool isRunning, LLUUID exerienceId, std::string buffer, taskUploadFinish_f finish):
    LLBufferedAssetUploadInfo(taskId, itemId, LLAssetType::AT_LSL_TEXT, buffer, finish),
    mExerienceId(exerienceId),
    mTargetType(targetType),
    mIsRunning(isRunning)
{
}

LLSD LLScriptAssetUpload::generatePostBody()
{
    LLSD body;

    if (getTaskId().isNull())
    {
        body["item_id"] = getItemId();
        body["target"] = "lsl2";
    }
    else
    {
        body["task_id"] = getTaskId();
        body["item_id"] = getItemId();
        body["is_script_running"] = getIsRunning();
        body["target"] = (getTargetType() == MONO) ? "mono" : "lsl2";
        body["experience"] = getExerienceId();
    }

    return body;
}

//=========================================================================
/*static*/
LLUUID LLViewerAssetUpload::EnqueueInventoryUpload(const std::string &url, const LLResourceUploadInfo::ptr_t &uploadInfo)
{
    std::string procName("LLViewerAssetUpload::AssetInventoryUploadCoproc(");
    
    LLUUID queueId = LLCoprocedureManager::instance().enqueueCoprocedure("Upload", 
        procName + LLAssetType::lookup(uploadInfo->getAssetType()) + ")",
        boost::bind(&LLViewerAssetUpload::AssetInventoryUploadCoproc, _1, _2, url, uploadInfo));

    return queueId;
}

//=========================================================================
/*static*/
void LLViewerAssetUpload::AssetInventoryUploadCoproc(LLCoreHttpUtil::HttpCoroutineAdapter::ptr_t &httpAdapter, 
    const LLUUID &id, std::string url, LLResourceUploadInfo::ptr_t uploadInfo)
{
    LLCore::HttpRequest::ptr_t httpRequest(new LLCore::HttpRequest);
    LLCore::HttpOptions::ptr_t httpOptions(new LLCore::HttpOptions);
    httpOptions->setTimeout(LL_ASSET_UPLOAD_TIMEOUT_SEC);

    LLSD result = uploadInfo->prepareUpload();
    uploadInfo->logPreparedUpload();

    if (result.has("error"))
    {
        HandleUploadError(LLCore::HttpStatus(499), result, uploadInfo);
        return;
    }

    llcoro::suspend();

    if (uploadInfo->showUploadDialog())
    {
        std::string uploadMessage = "Uploading...\n\n";
        uploadMessage.append(uploadInfo->getDisplayName());
        LLUploadDialog::modalUploadDialog(uploadMessage);
    }

    LLSD body = uploadInfo->generatePostBody();

    result = httpAdapter->postAndSuspend(httpRequest, url, body, httpOptions);

    LLSD httpResults = result[LLCoreHttpUtil::HttpCoroutineAdapter::HTTP_RESULTS];
    LLCore::HttpStatus status = LLCoreHttpUtil::HttpCoroutineAdapter::getStatusFromLLSD(httpResults);

    if ((!status) || (result.has("error")))
    {
        HandleUploadError(status, result, uploadInfo);
        if (uploadInfo->showUploadDialog())
            LLUploadDialog::modalUploadFinished();
        return;
    }

    std::string uploader = result["uploader"].asString();

    bool success = false;
    if (!uploader.empty() && uploadInfo->getAssetId().notNull())
    {
        result = httpAdapter->postFileAndSuspend(httpRequest, uploader, uploadInfo->getAssetId(), uploadInfo->getAssetType(), httpOptions);
        httpResults = result[LLCoreHttpUtil::HttpCoroutineAdapter::HTTP_RESULTS];
        status = LLCoreHttpUtil::HttpCoroutineAdapter::getStatusFromLLSD(httpResults);

        std::string ulstate = result["state"].asString();

        if ((!status) || (ulstate != "complete"))
        {
            HandleUploadError(status, result, uploadInfo);
            if (uploadInfo->showUploadDialog())
                LLUploadDialog::modalUploadFinished();
            return;
        }

        S32 uploadPrice = result["upload_price"].asInteger();//uploadInfo->getEconomyUploadCost();

        if (uploadPrice > 0)
        {
            // this upload costed us L$, update our balance
            // and display something saying that it cost L$
            LLStatusBar::sendMoneyBalanceRequest();

            LLSD args;
            args["AMOUNT"] = llformat("%d", uploadPrice);
            LLNotificationsUtil::add("UploadPayment", args);
        }
    }
    else
    {
        LL_WARNS() << "No upload url provided.  Nothing uploaded, responding with previous result." << LL_ENDL;
    }
    LLUUID serverInventoryItem = uploadInfo->finishUpload(result);

    if (uploadInfo->showInventoryPanel())
    {
        if (serverInventoryItem.notNull())
        {
            success = true;

            // Show the preview panel for textures and sounds to let
            // user know that the image (or snapshot) arrived intact.
            LLInventoryPanel* panel = LLInventoryPanel::getActiveInventoryPanel();
            if (panel)
            {
                LLFocusableElement* focus = gFocusMgr.getKeyboardFocus();
                panel->setSelection(serverInventoryItem, TAKE_FOCUS_NO);

                // restore keyboard focus
                gFocusMgr.setKeyboardFocus(focus);
            }
        }
        else
        {
            LL_WARNS() << "Can't find a folder to put it in" << LL_ENDL;
        }
    }

    // remove the "Uploading..." message
    if (uploadInfo->showUploadDialog())
        LLUploadDialog::modalUploadFinished();

    // Let the Snapshot floater know we have finished uploading a snapshot to inventory
    LLFloater* floater_snapshot = LLFloaterReg::findInstance("snapshot");
    if (uploadInfo->getAssetType() == LLAssetType::AT_TEXTURE && floater_snapshot && floater_snapshot->isShown())
    {
        floater_snapshot->notify(LLSD().with("set-finished", LLSD().with("ok", success).with("msg", "inventory")));
    }
    LLFloater* floater_outfit_snapshot = LLFloaterReg::findInstance("outfit_snapshot");
    if (uploadInfo->getAssetType() == LLAssetType::AT_TEXTURE && floater_outfit_snapshot && floater_outfit_snapshot->isShown())
    {
        floater_outfit_snapshot->notify(LLSD().with("set-finished", LLSD().with("ok", success).with("msg", "inventory")));
    }
}

//=========================================================================
/*static*/
void LLViewerAssetUpload::HandleUploadError(LLCore::HttpStatus status, LLSD &result, LLResourceUploadInfo::ptr_t &uploadInfo)
{
    std::string reason;
    std::string label("CannotUploadReason");

    LL_WARNS() << ll_pretty_print_sd(result) << LL_ENDL;

    if (result.has("label"))
    {
        label = result["label"].asString();
    }

    if (result.has("message"))
    {
        reason = result["message"].asString();
    }
    else
    {
        if (status.getType() == 499)
        {
            reason = "The server is experiencing unexpected difficulties.";
        }
        else
        {
            reason = "Error in upload request.  Please visit "
                "http://secondlife.com/support for help fixing this problem.";
        }
    }

    LLSD args;
    if(label == "ErrorMessage")
    {
        args["ERROR_MESSAGE"] = reason;
    }
    else
    {
        args["FILE"] = uploadInfo->getDisplayName();
        args["REASON"] = reason;
    }

    LLNotificationsUtil::add(label, args);

    // unfreeze script preview
    if (uploadInfo->getAssetType() == LLAssetType::AT_LSL_TEXT)
    {
        LLPreviewLSL* preview = LLFloaterReg::findTypedInstance<LLPreviewLSL>("preview_script", 
            uploadInfo->getItemId());
        if (preview)
        {
            LLSD errors;
            errors.append(LLTrans::getString("UploadFailed") + reason);
            preview->callbackLSLCompileFailed(errors);
        }
    }

    // Let the Snapshot floater know we have failed uploading.
    LLFloater* floater_snapshot = LLFloaterReg::findInstance("snapshot");
    if (uploadInfo->getAssetType() == LLAssetType::AT_TEXTURE && floater_snapshot && floater_snapshot->isShown())
    {
        floater_snapshot->notify(LLSD().with("set-finished", LLSD().with("ok", false).with("msg", "inventory")));
    }
    LLFloater* floater_outfit_snapshot = LLFloaterReg::findInstance("outfit_snapshot");
    if (uploadInfo->getAssetType() == LLAssetType::AT_TEXTURE && floater_outfit_snapshot && floater_outfit_snapshot->isShown())
    {
        floater_outfit_snapshot->notify(LLSD().with("set-finished", LLSD().with("ok", false).with("msg", "inventory")));
    }
}